Skip to content

Commit

Permalink
Fixes fo flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
jayjb committed Aug 14, 2023
1 parent 7f8eb25 commit 653f6f6
Show file tree
Hide file tree
Showing 24 changed files with 108 additions and 286 deletions.
4 changes: 2 additions & 2 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[flake8]
ignore = E501, W503
exclude = .git,__pycache__,docs/conf.py,build,dist
ignore = E501, W503, E203
exclude = .git,__pycache__,docs/conf.py,build,dist,opencanary/modules/des.py
max-complexity = 10
min_python_version = 3.9
34 changes: 14 additions & 20 deletions bin/opencanary.tac
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import traceback

# import warnings
# warnings.filterwarnings("ignore", category=DeprecationWarning)
def warn(*args, **kwargs):
pass


import warnings

warnings.warn = warn
import sys
from twisted.application import service
from twisted.application import internet
from twisted.internet.protocol import Factory
from pkg_resources import iter_entry_points

from opencanary.config import config, is_docker
Expand All @@ -33,6 +23,14 @@ from opencanary.modules.redis import CanaryRedis
from opencanary.modules.tcpbanner import CanaryTCPBanner
from opencanary.modules.rdp import CanaryRDP


def warn(*args, **kwargs):
pass


warnings.warn = warn


# from opencanary.modules.example0 import CanaryExample0
# from opencanary.modules.example1 import CanaryExample1

Expand Down Expand Up @@ -69,8 +67,6 @@ if config.moduleEnabled("snmp"):
pass

# NB: imports below depend on inotify, only available on linux
import sys

if sys.platform.startswith("linux"):
from opencanary.modules.samba import CanarySamba

Expand All @@ -87,10 +83,10 @@ if sys.platform.startswith("linux"):
logger = getLogger(config)


def start_mod(application, klass):
def start_mod(application, klass): # noqa: C901
try:
obj = klass(config=config, logger=logger)
except Exception as e:
except Exception:
err = "Failed to instantiate instance of class %s in %s. %s" % (
klass.__name__,
klass.__module__,
Expand All @@ -108,7 +104,7 @@ def start_mod(application, klass):
)
logMsg({"logdata": msg})

except Exception as e:
except Exception:
err = "Failed to run startYourEngines on %s in %s. %s" % (
klass.__name__,
klass.__module__,
Expand All @@ -127,7 +123,7 @@ def start_mod(application, klass):
klass.__module__,
)
logMsg({"logdata": msg})
except Exception as e:
except Exception:
err = "Failed to add service from class %s in %s. %s" % (
klass.__name__,
klass.__module__,
Expand All @@ -144,8 +140,6 @@ def start_mod(application, klass):

def logMsg(msg):
data = {}
# data['src_host'] = device_name
# data['dst_host'] = node_id
data["logdata"] = {"msg": msg}
logger.log(data, retry=False)

Expand All @@ -161,7 +155,7 @@ for ep in iter_entry_points(ENTRYPOINT):
try:
klass = ep.load(require=False)
start_modules.append(klass)
except Exception as e:
except Exception:
err = "Failed to load class from the entrypoint: %s. %s" % (
str(ep),
traceback.format_exc(),
Expand Down
5 changes: 2 additions & 3 deletions build_scripts/generate_macOS_launchctl_service_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from functools import partial
from os import chmod, pardir, path
from os.path import dirname, join, realpath
from shutil import copyfile
from subprocess import CalledProcessError, check_output

from pkg_resources import resource_filename
Expand Down Expand Up @@ -78,7 +77,7 @@
parser.add_argument(
"--canary",
action="append",
help=f"enable canary service in the generated opencanary.conf file "
help="enable canary service in the generated opencanary.conf file "
+ "(can be supplied more than once)",
choices=canaries,
dest="canaries",
Expand All @@ -96,7 +95,7 @@

# File builders
build_launchctl_dir_path = partial(join, LAUNCHCTL_DIR)
build_logfile_name = lambda log_name: join(
build_logfile_name = lambda log_name: join( # noqa: E731
args.log_output_dir, f"opencanary.{log_name}.log"
)

Expand Down
11 changes: 8 additions & 3 deletions opencanary/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from six import iteritems
import os, sys, json, copy, socket, itertools, string, subprocess
import os
import sys
import json
import itertools
import string
import subprocess
from os.path import expanduser
from pkg_resources import resource_filename
from pathlib import Path
Expand Down Expand Up @@ -80,7 +85,7 @@ def getVal(self, key, default=None):
return default
raise e

def setValues(self, params):
def setValues(self, params): # noqa: C901
"""Set all the valid values in params and return a list of errors for invalid"""

# silently ensure that node_id and mac are not modified via web
Expand Down Expand Up @@ -159,7 +164,7 @@ def setVal(self, key, val):
if e.key == key:
raise e

def valid(self, key, val):
def valid(self, key, val): # noqa: C901
"""
Test an the validity of an individual setting
Raise config error message on failure.
Expand Down
2 changes: 1 addition & 1 deletion opencanary/iphelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def check_ip(ip, network_range):
ripInt = ip2int(rangeIP)
ipInt = ip2int(ip)
result = not ((ipInt ^ ripInt) & 0xFFFFFFFF << (32 - rangeMask))
except:
except: # noqa: E722
result = False

return result
10 changes: 5 additions & 5 deletions opencanary/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from twisted.internet import reactor
import requests

from opencanary.iphelper import *
from opencanary.iphelper import check_ip


class Singleton(type):
Expand All @@ -25,7 +25,7 @@ def __call__(cls, *args, **kwargs):
def getLogger(config):
try:
d = config.getVal("logger")
except Exception as e:
except Exception:
print("Error: config does not have 'logger' section", file=sys.stderr)
exit(1)

Expand Down Expand Up @@ -171,14 +171,14 @@ def log(self, logdata, retry=True):
notify = True
if "src_host" in logdata:
for ip in self.ip_ignorelist:
if check_ip(logdata["src_host"], ip) == True:
if check_ip(logdata["src_host"], ip) is True:
notify = False
break

if "logtype" in logdata and logdata["logtype"] in self.logtype_ignorelist:
notify = False

if notify == True:
if notify is True:
self.logger.warn(json.dumps(logdata, sort_keys=True))


Expand Down Expand Up @@ -239,7 +239,7 @@ def emit(self, record):
try:
msg = self.format(record)
self.hpc.publish(self.channels, msg)
except:
except: # noqa: E722
print("Error on publishing to server")


Expand Down
7 changes: 3 additions & 4 deletions opencanary/modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from twisted.internet.protocol import Factory
from twisted.internet.protocol import DatagramProtocol

from opencanary.honeycred import *
from opencanary.honeycred import buildHoneyCredHook

# Monkey-patch-replace Twisted Protocol with CanaryProtocol class
from twisted.internet import protocol
Expand Down Expand Up @@ -110,12 +110,11 @@ def getService(self):
raise Exception(err)


if sys.platform.startswith("linux"):
if sys.platform.startswith("linux"): # noqa: C901
from twisted.python import filepath
from twisted.internet import inotify
from twisted.python._inotify import INotifyError
from twisted.internet.inotify import IN_CREATE
import datetime
import os

class FileSystemWatcher(object):
Expand All @@ -132,7 +131,7 @@ def reopenFiles(self, skipToEnd=True):
self.f = open(self.path)
if skipToEnd:
self.f.seek(0, 2)
except IOError as e:
except IOError:
self.f = None

self.notifier.startReading()
Expand Down
1 change: 0 additions & 1 deletion opencanary/modules/example0.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from twisted.internet.protocol import Protocol
from twisted.internet.protocol import Factory
from twisted.application import internet


class Example0Protocol(Protocol):
Expand Down
1 change: 0 additions & 1 deletion opencanary/modules/example1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from twisted.internet.protocol import Protocol
from twisted.internet.protocol import Factory
from twisted.application import internet


class Example1Protocol(Protocol):
Expand Down
4 changes: 1 addition & 3 deletions opencanary/modules/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from twisted.application import internet
from twisted.web.server import Site, GzipEncoderFactory
from twisted.web.resource import Resource, EncodingResourceWrapper, ForbiddenResource
from twisted.web.resource import Resource, EncodingResourceWrapper
from twisted.web.util import Redirect
from twisted.web import static

Expand Down Expand Up @@ -76,8 +76,6 @@ def render(self, request):

def render_GET(self, request, loginFailed=False):
if not loginFailed:
us = request.transport.getHost()
peer = request.transport.getPeer()
useragent = request.getHeader("user-agent")
if not useragent:
useragent = "<not supplied>"
Expand Down
21 changes: 3 additions & 18 deletions opencanary/modules/httpproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,9 @@

from base64 import b64decode

try:
import urlparse
except ImportError:
import urllib.parse as urlparse

try:
from urllib import quote # Python 2.X
except ImportError:
from urllib.parse import quote # Python 3+
from twisted.application import internet
from twisted.internet.protocol import ServerFactory
from twisted.application.internet import TCPServer
from twisted.internet.protocol import ClientFactory
from twisted.internet import protocol

from twisted.web.http import HTTPClient, Request, HTTPChannel
from twisted.web.http import Request, HTTPChannel
from twisted.web import http
from twisted.internet import reactor

from jinja2 import Template

Expand Down Expand Up @@ -86,7 +71,7 @@ def logAuth(self):
if atype == "Basic":
try:
username, password = b64decode(token).split(":")
except:
except: # noqa: E722
pass
elif atype == "NTLM":
# b64decode returns bytes not str in python2
Expand Down Expand Up @@ -158,7 +143,7 @@ def __init__(self, config=None, logger=None):
try:
with open(authfilename, "r") as f:
self.auth_template = Template(f.read())
except:
except: # noqa: E722
self.auth_template = Template("")

def getService(self):
Expand Down
6 changes: 1 addition & 5 deletions opencanary/modules/https.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import re
from datetime import datetime, timedelta
from pathlib import Path

Expand All @@ -8,17 +7,14 @@
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.x509.oid import NameOID
from twisted.application import internet
from twisted.web import static
from twisted.web.resource import Resource, EncodingResourceWrapper
from twisted.web.resource import EncodingResourceWrapper
from twisted.web.server import Site, GzipEncoderFactory
from twisted.web.util import Redirect
from twisted.internet.ssl import DefaultOpenSSLContextFactory

from opencanary.modules import CanaryService
from opencanary.modules.http import (
BasicLogin,
CanaryHTTP,
Error,
RedirectCustomHeaders,
StaticNoDirListing,
)
Expand Down
8 changes: 2 additions & 6 deletions opencanary/modules/mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
from ntlmlib.messages import ChallengeResponse, TargetInfo

import struct
import re
import collections


# Monkeypatch bug in ntmllib
if getattr(TargetInfo, "getData", None) is None:

Expand Down Expand Up @@ -178,7 +176,7 @@ def parseLogin7(data):
hlen = struct.calcsize(hfmt)
try:
htuple = struct.unpack(hfmt, data[:hlen])
except Exception as e:
except Exception:
return None

def decodePassChar(c):
Expand All @@ -203,7 +201,7 @@ def decodePassChar(c):
if field == "Password":
_fdata = "".join(map(decodePassChar, _fdata))
loginData[field] = _fdata.decode("utf-16")
except Exception as e:
except Exception:
pass

field = "SSPI"
Expand Down Expand Up @@ -306,8 +304,6 @@ def buildChallengeToken():
# template = c.get_data()
old = b"NTLMSSP\x00\x02\x00\x00\x00\x1e\x00\x1e\x008\x00\x00\x00\x15\xc2\x8a\xe26Ph\x8a\xae\x84R\xbe@\xd5qt\xe4\x00\x00\x00\xe4\x00\xe4\x00V\x00\x00\x00\x06\x02\xf0#\x00\x00\x00\x0fW\x00I\x00N\x002\x00K\x001\x002\x00-\x00D\x00O\x00M\x00A\x00I\x00N\x00S\x00\x02\x00\x1e\x00W\x00I\x00N\x002\x00K\x001\x002\x00-\x00D\x00O\x00M\x00A\x00I\x00N\x00S\x00\x01\x00\x1e\x00W\x00I\x00N\x002\x00K\x001\x002\x00-\x00D\x00O\x00M\x00A\x00I\x00N\x00S\x00\x04\x00D\x00w\x00i\x00n\x002\x00k\x001\x002\x00-\x00d\x00o\x00m\x00a\x00i\x00n\x00s\x00r\x00v\x00.\x00c\x00o\x00r\x00p\x00.\x00t\x00h\x00i\x00n\x00k\x00s\x00t\x00.\x00c\x00o\x00m\x00\x03\x00D\x00w\x00i\x00n\x002\x00k\x001\x002\x00-\x00d\x00o\x00m\x00a\x00i\x00n\x00s\x00r\x00v\x00.\x00c\x00o\x00r\x00p\x00.\x00t\x00h\x00i\x00n\x00k\x00s\x00t\x00.\x00c\x00o\x00m\x00\x07\x00\x08\x00\xa2\x9e\xda\x91\x1f\xbb\xd0\x01\x00\x00\x00\x00\x06\x02\xf0#\x00\x00\x00\x0fy\x00o\x00y\x00o\x00m\x00a\x00\x00\x00\x00\x00"

template = b"NTLMSSP\x00\x02\x00\x00\x00\x1e\x00\x1e\x008\x00\x00\x00\x15\xc2\x8a\xe26Ph\x8a\xae\x84R\xbe@\xd5qt\xe4\x00\x00\x00\x06\x02\xf0#\x00\x00\x00\x0fW\x00I\x00N\x002\x00K\x001\x002\x00-\x00D\x00O\x00M\x00A\x00I\x00N\x00S\x00\x07\x00\x08\x00\xa2\x9e\xda\x91\x1f\xbb\xd0\x01\x00\x00\x00\x00"

payload = spnegoheader + old
return b"\xed" + struct.pack("<H", len(payload)) + payload

Expand Down
Loading

0 comments on commit 653f6f6

Please sign in to comment.