Skip to content

Commit

Permalink
Rm py2 hacks (thinkst#208)
Browse files Browse the repository at this point in the history
* Rm py2 checks and hacks

* Rm old test case
  • Loading branch information
jayjb authored Jul 27, 2022
1 parent 9082b3e commit b18181b
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/opencanary_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10"]
os: ["ubuntu-18.04", "ubuntu-20.04", "macos-10.15", "macos-11"]
os: ["ubuntu-18.04", "ubuntu-20.04", "macos-11"]
fail-fast: false
runs-on: ${{ matrix.os }}
steps:
Expand Down
4 changes: 0 additions & 4 deletions bin/honeycred
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
from __future__ import print_function
import sys


Expand All @@ -10,6 +9,3 @@ if len(sys.argv) != 2:
exit(1)

print(cryptcontext.encrypt(sys.argv[1]))



8 changes: 2 additions & 6 deletions bin/opencanaryd
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ if [ "${cmd}" == "--start" ]; then
elif [ "${cmd}" == "--dev" ]; then
sudo -E "${DIR}/twistd" -noy "${DIR}/opencanary.tac"
elif [ "${cmd}" == "--usermodule" ]; then
usermodconf=$(python -c "from __future__ import print_function; from pkg_resources import resource_filename; print(resource_filename('opencanary', 'data/settings-usermodule.json'))")
usermodconf=$(python -c "from pkg_resources import resource_filename; print(resource_filename('opencanary', 'data/settings-usermodule.json'))")

if [ -f opencanary.conf ]; then
if ! diff -q opencanary.conf "${usermodconf}" 2>&1 >/dev/null; then
Expand All @@ -57,11 +57,7 @@ elif [ "${cmd}" == "--copyconfig" ]; then
echo "A config file already exists at /etc/opencanaryd/opencanary.conf, please move it first"
exit 1
fi
if [[ -f `which python` ]] ; then
defaultconf=$(python -c "from __future__ import print_function; from pkg_resources import resource_filename; print(resource_filename('opencanary', 'data/settings.json'))")
else
defaultconf=$(python3 -c "from pkg_resources import resource_filename; print(resource_filename('opencanary', 'data/settings.json'))")
fi
defaultconf=$(python3 -c "from pkg_resources import resource_filename; print(resource_filename('opencanary', 'data/settings.json'))")
sudo -E mkdir -p /etc/opencanaryd
sudo -E cp "${defaultconf}" /etc/opencanaryd/opencanary.conf
echo -e "[*] A sample config file is ready /etc/opencanaryd/opencanary.conf\n"
Expand Down
20 changes: 0 additions & 20 deletions opencanary/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
from six import iteritems
import os, sys, json, copy, socket, itertools, string, subprocess
from os.path import expanduser
Expand All @@ -7,24 +6,6 @@
SAMPLE_SETTINGS = resource_filename(__name__, 'data/settings.json')
SETTINGS = 'opencanary.conf'

PY3 = sys.version_info > (3,)

# Only check unicode on Python 2, In Python 3 unicode is the default and we can just return the input.
if sys.version_info[0] < 3:
def byteify(input):
if isinstance(input, dict):
return {byteify(key): byteify(value)
for key, value in iteritems(input)}
elif isinstance(input, list):
return [byteify(element) for element in input]
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
return input
else:
def byteify(input):
return input


def expand_vars(var):
"""Recursively replace environment variables in a dictionary, list or string with their respective values."""
Expand All @@ -51,7 +32,6 @@ def __init__(self, configfile=SETTINGS):
with open(fname, "r") as f:
print("[-] Using config file: %s" % fname)
self.__config = json.load(f)
self.__config = byteify(self.__config)
self.__config = expand_vars(self.__config)
return
except IOError as e:
Expand Down
1 change: 0 additions & 1 deletion opencanary/logger.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
import simplejson as json
import logging.config
import socket
Expand Down
1 change: 0 additions & 1 deletion opencanary/modules/mssql.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
from opencanary.modules import CanaryService
from opencanary.config import ConfigException

Expand Down
19 changes: 5 additions & 14 deletions opencanary/modules/mysql.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from opencanary.modules import CanaryService
from opencanary.config import ConfigException, PY3
from opencanary.config import ConfigException

from twisted.protocols.policies import TimeoutMixin
from twisted.internet.protocol import Protocol
Expand Down Expand Up @@ -52,27 +52,18 @@ def parse_auth(data):

username = data[offset:i]
i += 1
if PY3:
plen = data[i]
else:
plen = struct.unpack('B', data[i])[0]
plen = data[i]
i+=1
if plen == 0:
return username, None
if PY3:
password="".join("{:02x}".format(c) for c in data[i:i+plen])
else:
password="".join("{:02x}".format(ord(c)) for c in data[i:i+plen])
password="".join("{:02x}".format(c) for c in data[i:i+plen])
return username, password

def consume_packet(self):
if len(self._buffer) < MySQL.HEADER_LEN:
return None, None
length = struct.unpack('<I', self._buffer[:3] + b'\x00')[0]
if PY3:
seq_id = self._buffer[3]
else:
seq_id = struct.unpack('<B', self._buffer[3])[0]
seq_id = self._buffer[3]

# enough buffer data to consume packet?
if len(self._buffer) < MySQL.HEADER_LEN + length:
Expand All @@ -85,7 +76,7 @@ def consume_packet(self):
return seq_id, payload

def server_greeting(self):
# struct.pack returns a byte string for py2 and py3
# struct.pack returns a byte string
_threadid = struct.pack('<I', self.threadid)
# TODO: randomize salts embedded here
data = b'\x0a' + self.factory.canaryservice.banner + b'\x00' + _threadid + b'\x25\x73\x36\x51\x74\x77\x75\x69\x00\xff\xf7\x08\x02\x00\x0f\x80\x15\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x2e\x47\x5c\x78\x67\x7a\x4f\x5b\x5c\x3e\x5c\x39\x00\x6d\x79\x73\x71\x6c\x5f\x6e\x61\x74\x69\x76\x65\x5f\x70\x61\x73\x73\x77\x6f\x72\x64\x00'
Expand Down
9 changes: 2 additions & 7 deletions opencanary/modules/ssh.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import print_function
from opencanary.modules import CanaryService
from opencanary.config import PY3

from opencanary.modules import CanaryService
import twisted
from twisted.cred import portal, checkers, credentials, error
from twisted.conch import error, avatar, interfaces as conchinterfaces
Expand Down Expand Up @@ -90,10 +88,7 @@ def auth_publickey(self, packet):
#convert blob into openssh key format
key = keys.Key.fromString(key_blob).toString('openssh')
except:
if not PY3:
key = "Invalid SSH Public Key Submitted: {key_blob}".format(key_blob=key_blob.encode('hex'))
else:
key = "Invalid SSH Public Key Submitted: {key_blob}".format(key_blob=key_blob.hex())
key = "Invalid SSH Public Key Submitted: {key_blob}".format(key_blob=key_blob.hex())
for keytype in [b'ecdsa-sha2-nistp256',b'ecdsa-sha2-nistp384',b'ecdsa-sha2-nistp521',b'ssh-ed25519']:
if keytype in key_blob:
key = '{keytype} {keydata}'.format(
Expand Down
30 changes: 8 additions & 22 deletions opencanary/modules/vnc.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from __future__ import print_function
from opencanary.modules import CanaryService

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

from opencanary.modules.des import des
from opencanary.config import PY3
import sys

import os
Expand Down Expand Up @@ -86,14 +84,8 @@ def _recv_auth(self,data=None):
if len(data) != 16:
raise ProtocolError()

if PY3:
logdata = {"VNC Server Challenge" : self.challenge.hex(),
"VNC Client Response": data.hex()
}
else:
logdata = {"VNC Server Challenge" : self.challenge.encode('hex'),
"VNC Client Response": data.encode('hex')
}
logdata = {"VNC Server Challenge" : self.challenge.hex(),
"VNC Client Response": data.hex()}

used_password = self._try_decrypt_response(response=data)
if used_password:
Expand Down Expand Up @@ -124,18 +116,12 @@ def _try_decrypt_response(self, response=None):
if len(pw) < 8:
pw+= '\x00'*(8-len(pw))

if not PY3:
#VNC use of DES requires password bits to be mirrored
pw = ''.join([chr(int('{:08b}'.format(ord(x))[::-1], 2))
for x in pw])
desbox = des(pw)
else:
pw = pw.encode('ascii')
# VNC use of DES requires password bits to be mirrored
values = bytearray()
for x in pw:
values.append(int('{:08b}'.format(x)[::-1], 2))
desbox = des(values)
pw = pw.encode('ascii')
# VNC use of DES requires password bits to be mirrored
values = bytearray()
for x in pw:
values.append(int('{:08b}'.format(x)[::-1], 2))
desbox = des(values)

decrypted_challenge = desbox.decrypt(response)
if decrypted_challenge == self.challenge:
Expand Down
13 changes: 1 addition & 12 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@
from setuptools import setup, find_packages
import sys

PY3 = sys.version_info > (3,)

# Only check unicode on Python 2, In Python 3 unicode is the default and we can just return the input.
if sys.version_info[0] < 3:
jinja_version = '2.10.1'
else:
jinja_version = '3.0.1'


def read(rel_path):
here = os.path.abspath(os.path.dirname(__file__))
Expand Down Expand Up @@ -39,14 +31,12 @@ def get_version(rel_path):
'PyPDF2==1.26.0',
'fpdf==1.7.2',
'passlib==1.7.1',
'Jinja2=={}'.format(jinja_version),
'Jinja2==3.0.1',
'ntlmlib==0.72',
'bcrypt==3.1.7',
'setuptools==44.0.0',
'hpfeeds==3.0.0']

if sys.version_info.major < 3:
requirements.append('wsgiref==0.1.2')

setup(
name='opencanary',
Expand Down Expand Up @@ -76,7 +66,6 @@ def get_version(rel_path):
"Operating System :: Unix",
"Operating System :: POSIX :: Linux",
"Operating System :: POSIX :: BSD :: FreeBSD",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
Expand Down

0 comments on commit b18181b

Please sign in to comment.