def test_dispatched_https(): server = SocketServer.TCPServer(('', 0), multi.Dispatcher) server.cfg = ModuleConfig('dispatcher.conf').cfg thread.start_new_thread(server.serve_forever, ()) resp = urllib2.urlopen("https://127.0.0.1:{0}/anything/blah.html".format( server.socket.getsockname()[1])).read() assert "Netsink" in resp
def __init__(self, name, port, handler, socktype, config): self.name = name self.port = port self.socktype = socktype self.server = None globalconf = Config() if socktype.upper() == 'UDP': self.server = SocketServer.ThreadingUDPServer(('', port), handler) elif socktype.upper() == 'TCP': self.server = SocketServer.ThreadingTCPServer(('', port), handler) elif socktype.upper() == 'SSL': if not get_data_file(globalconf.certfile) or not get_data_file( globalconf.keyfile): log.warn("Cannot find certfile: %s or keyfile: %s for ssl", globalconf.certfile, globalconf.keyfile) else: self.server = SocketServer.ThreadingTCPServer(('', port), handler) self.server.socket = ssl.wrap_socket( self.server.socket, keyfile=get_data_file(globalconf.keyfile), certfile=get_data_file(globalconf.certfile), server_side=True) else: log.warn("Unsupported or invalid socket type: %s for config '%s'", socktype, name) if self.server: self.server.cfg = ModuleConfig(config).cfg
def dispatch(self, data): from netsink.modules import registry for x in self.handlers: if x.match(data): log.info("Packet data matches '%s' - dispatching", str(x)) if inspect.isclass(x): # instantiate and copy attributes for name, cls in registry.items(): if x == cls: self.server.cfg = ModuleConfig("%s.conf" % name).cfg handler = x(self.request, self.client_address, self.server) # copy back as handler may have wrapped/modified # the connection.. eg. upgrading to ssl/tls self.connection = handler.connection self.request = handler.connection # keep reference for stateful use if needed later # in same conversation self.handlers.insert(0, handler) else: # existing instance, just copy over latest attributes # in case they have changed since last used x.request = self.request x.connection = self.connection x.client_address = self.client_address x.rfile = self.rfile x.wfile = self.wfile x.handle() return # default for no match.. just try to consume? self.connection.recv(2048) log.info("Could not find handler to match traffic, consuming. %s", repr(data))
def test_sslhandler(): server = SocketServer.TCPServer(('', 0), sslwrap.SSLHandler) server.cfg = ModuleConfig('ssl.conf').cfg thread.start_new_thread(server.serve_forever, ()) client = ssl.wrap_socket(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) client.connect(('127.0.0.1', server.socket.getsockname()[1])) assert client.ssl_version >= 2
def test_no_match(): server = SocketServer.TCPServer(('', 0), multi.Dispatcher) server.cfg = ModuleConfig('dispatcher.conf').cfg thread.start_new_thread(server.serve_forever, ()) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('127.0.0.1', server.socket.getsockname()[1])) # server should just consume unknown client traffic s.send('asdhakdfhkajfafdhjagsdfjjhsadfhjagsd1234234123412342134asddf' * 10) s.close()
def main(): """Script entry point. """ initlogging() log.setLevel(logging.DEBUG) l = startlisteners(Config()) if Config().redirection: redirection(ModuleConfig("redirection.conf"), l) wait()
def test_ftp_login(): server = SocketServer.TCPServer(('', 0), ftp.FTPHandler) server.cfg = ModuleConfig('ftp.conf').cfg thread.start_new_thread(server.serve_forever, ()) client = ftplib.FTP() client.connect('127.0.0.1', server.socket.getsockname()[1]) client.login('user1', 'mysecret') assert 'README.txt' in client.nlst() client.quit()
def test_iplookup_raw(): server = SocketServer.TCPServer(('', 0), http.HTTPHandler) server.cfg = ModuleConfig('http.conf').cfg thread.start_new_thread(server.serve_forever, ()) headers = {"Host": 'checkip.dyndns.org'} req = urllib2.Request("http://127.0.0.1:{0}/plain".format( server.socket.getsockname()[1]), headers=headers) resp = urllib2.urlopen(req).read() assert resp.startswith("11.22.33.44")
def test_iplookup(): server = SocketServer.TCPServer(('', 0), http.HTTPHandler) server.cfg = ModuleConfig('http.conf').cfg thread.start_new_thread(server.serve_forever, ()) headers = {"User-Agent": 'Google-Bot', "Host": 'ipgoat.com'} req = urllib2.Request("http://127.0.0.1:{0}".format( server.socket.getsockname()[1]), headers=headers) resp = urllib2.urlopen(req).read() assert "11.22.33.44" in resp
def test_ftp_upload(): server = SocketServer.TCPServer(('', 0), ftp.FTPHandler) server.cfg = ModuleConfig('ftp.conf').cfg thread.start_new_thread(server.serve_forever, ()) client = ftplib.FTP() client.connect('127.0.0.1', server.socket.getsockname()[1]) client.login() with open(get_data_file("ftproot/README.txt")) as tmp: client.storbinary('STOR testing.txt', tmp) client.quit()
def test_ftp_download(): server = SocketServer.TCPServer(('', 0), ftp.FTPHandler) server.cfg = ModuleConfig('ftp.conf').cfg thread.start_new_thread(server.serve_forever, ()) client = ftplib.FTP() client.connect('127.0.0.1', server.socket.getsockname()[1]) client.login() def check_content(content): assert 'Nothing to see here' in content client.retrbinary('RETR README.txt', check_content) client.quit()
def test_server_initiated(): server = SocketServer.TCPServer(('', 0), multi.Dispatcher) server.cfg = ModuleConfig('dispatcher.conf').cfg thread.start_new_thread(server.serve_forever, ()) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('127.0.0.1', server.socket.getsockname()[1])) # client is expecting server to initiate comms # check that server eventually gives up and sends # something back in an attempt to elicit something from client resp = s.recv(2048) assert 'netsink' in resp s.close()
def test_irc(): server = SocketServer.TCPServer(('', 0), ircserver.IRCHandler) server.cfg = ModuleConfig('irc.conf').cfg thread.start_new_thread(server.serve_forever, ()) client = irc.client.IRC() conn = client.server().connect('127.0.0.1', server.socket.getsockname()[1], 'nickname') conn.join("#testchan", key="12345") conn.privmsg("#testchan", "ready for tasking") # drain response messages for _ in range(6): client.process_once(0.015) conn.close()
def test_smtp(): server = SocketServer.TCPServer(('', 0), smtp.SMTPHandler) server.cfg = ModuleConfig('smtp.conf').cfg thread.start_new_thread(server.serve_forever, ()) client = smtplib.SMTP('127.0.0.1', server.socket.getsockname()[1]) msg = MIMEText('Message Body') msg['To'] = email.utils.formataddr(('Recipient', '*****@*****.**')) msg['From'] = email.utils.formataddr(('Author', '*****@*****.**')) msg['Subject'] = 'Netsink Test Message' # returns dictionary of failed recipients assert not client.sendmail('*****@*****.**', ['*****@*****.**'], msg.as_string()) client.quit()
def test_small_data(): server = SocketServer.TCPServer(('', 0), multi.Dispatcher) server.cfg = ModuleConfig('dispatcher.conf').cfg thread.start_new_thread(server.serve_forever, ()) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('127.0.0.1', server.socket.getsockname()[1])) # multiple sends but still less than needed for pattern matching s.send('asdfk') s.send('sasd') s.send('00') # check that server eventually gives up on the small data and sends # something back in an attempt to elicit more comms resp = s.recv(2048) assert 'netsink' in resp s.close()