Created
July 4, 2020 09:28
-
-
Save olliencc/1aade5b127281a3b0015eb6a0e8552ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################################################## | |
# | |
# Thinkst Canary user module | |
# to turn into a high interactive honeypot | |
# https://canary.tools/ | |
# | |
# Ingrediants used: | |
# - WSL | |
# - Developer documentation - https://canary.tools/help/user-modules | |
# - Opencanary for development - https://github.com/thinkst/opencanary/ | |
# | |
# | |
# | |
######################################################################## | |
INCIDENT_NAME = "CANARYPROXY" | |
VERSION = "0.1" | |
MODULE_DESCRIPTION = "CANARYPROXY" | |
AUTHOR = "Ollie Whitehouse" | |
AUTHOR_EMAIL = "[email protected]" | |
from opencanary.modules import CanaryService | |
from twisted.internet import ssl,reactor, protocol, defer | |
from twisted.internet.protocol import Protocol | |
from twisted.internet.protocol import Factory | |
from twisted.internet.protocol import ClientCreator | |
from twisted.internet.protocol import ClientFactory | |
from twisted.application import internet | |
from pprint import pprint | |
from inspect import getmembers | |
# Proxy client | |
class ProxyClientProtocol(protocol.Protocol): | |
def connectionMade(self): | |
self.cli_queue = self.factory.cli_queue | |
self.cli_queue.get().addCallback(self.serverDataReceived) | |
def serverDataReceived(self, chunk): | |
if chunk is False: | |
self.cli_queue = None | |
self.factory.continueTrying = False | |
self.transport.loseConnection() | |
elif self.cli_queue: | |
self.transport.write(chunk) | |
self.cli_queue.get().addCallback(self.serverDataReceived) | |
else: | |
self.factory.cli_queue.put(chunk) | |
def dataReceived(self, chunk): | |
self.factory.srv_queue.put(chunk) | |
def connectionLost(self, why): | |
if self.cli_queue: | |
self.cli_queue = None | |
# Proxy client Twisted Factory | |
class ProxyClientFactory(protocol.ReconnectingClientFactory): | |
maxDelay = 10 | |
continueTrying = True | |
protocol = ProxyClientProtocol | |
def __init__(self, srv_queue, cli_queue): | |
self.srv_queue = srv_queue | |
self.cli_queue = cli_queue | |
# Proxy server protocol handler class | |
class ProxyServer(protocol.Protocol): | |
def __init__(self): | |
#self.factory = factory | |
self.srv_queue = defer.DeferredQueue() | |
self.cli_queue = defer.DeferredQueue() | |
self.srv_queue.get().addCallback(self.clientDataReceived) | |
self.factoryclient = ProxyClientFactory(self.srv_queue, self.cli_queue) | |
#reactor.connectTCP(server, port, factoryclient) | |
reactor.connectTCP(self.clienthost, self.clientport, self.factoryclient) | |
def connectionMade(self): | |
logdata = {"DESCRIPTION": INCIDENT_NAME, "Proxy" : "Myproxy"} | |
self.factory.canaryservice.log(logdata, transport=self.transport) | |
def clientDataReceived(self, chunk): | |
self.transport.write(chunk) | |
self.srv_queue.get().addCallback(self.clientDataReceived) | |
def dataReceived(self, chunk): | |
self.cli_queue.put(chunk) | |
def connectionLost(self, why): | |
self.cli_queue.put(False) | |
# Main class used as the entry point | |
class canaryproxy(Factory, CanaryService): | |
NAME = 'canaryproxy' | |
# Constructor | |
def __init__(self, config=None, logger=None): | |
CanaryService.__init__(self, config=config, logger=logger) | |
# Our configuration | |
self.serverport = config.getVal('canaryproxy.port', default=823) | |
self.clienthost = config.getVal('canaryproxy.chost', default="towel.blinkenlights.nl") | |
self.clientport = config.getVal('canaryproxy.cport', default=23) | |
self.proxydesc = config.getVal('canaryproxy.desc', default="Telnet Starwars Proxy") | |
self.listen_addr = config.getVal('device.listen_addr', default='') | |
# Log type | |
self.logtype = logger.LOG_USER_2 | |
# This returns the service it wants | |
def getService(self): | |
# Build our protocol factory | |
f = protocol.ServerFactory() | |
f.canaryservice=self | |
f.logger=self.logger | |
f.protocol=ProxyServer # this is our protocol handler class defined above | |
# Return the server object | |
return internet.TCPServer(self.serverport, f, interface=self.listen_addr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment