def server(interface, port): server_stack = PyStack(interface) # Create a stack print '1' server_app = TCPApplication() # Create a TCPApplication server_stack.register_tcp_application(server_app) server_stack.run( doreactor=False ) # Run the stack to start listening using a thread to make it non-blocking server_app.bind(port, server_app, False) print '2' server_app.listen(3) # the maximum tcp clients to establish connections print '3' flow_one = server_app.accept( ) #will return the same tcp_application if newinstace is set to false in bind(port,app,newinstance) #print type(flow_one) print 'accept client request' while True: time.sleep(1) #flow_one.send_packet('ok') data = flow_one.fetch_data() if data != None: print data
class socket(TCPApplication): ''' Really basic implementation of socket based on Pystack ''' def __init__(self, *args): TCPApplication.__init__(self) self.stack = PyStack() self.stack.register_tcp_application(self) self.stack.run(False) def connect(self, (ip, port)): return self.lowerLayers["default"].connect(ip, port)
class socket(TCPApplication): ''' Really basic implementation of socket based on Pystack ''' def __init__(self, *args): TCPApplication.__init__(self) self.stack = PyStack() self.stack.register_tcp_application(self) self.stack.run(False) def connect(self, (ip, port)): return self.lowerLayers["default"].connect(ip, port)
def client(interface, server_ip, server_port, **kwargs): full_stack = PyStack(interface) #Create a stack client_app = TCPApplication() #Create a TCPApplication full_stack.register_tcp_application( client_app ) #Register the application to the stack which will manage to create the TCPSession etc. full_stack.run( doreactor=False ) #Run the stack to start listening using a thread to make it non-blocking if client_app.connect(server_ip, server_port): #Connect to the given server print 'connect to server' i = 0 while True: print 'send packet' i += 1 mpls_info = {} mpls_info["cos"] = 0L mpls_info["ttl"] = 64 mpls_info["s"] = 1L mpls_info["label"] = 1L kwargs["MPLS"] = mpls_info client_app.send_packet("{0}:changjiang!".format(i), **kwargs) #Send the request to the server time.sleep(1) client_app.close() print 'can not connect to server' full_stack.stop() #Stop the stack
kwargs["IP"]["flags"] = 2 if t1 or t2: t1 = t1 if t1 else 0 import time #t1 = time.time() t2 = t2 if t2 else 0 #t2 = 0 if kwargs["TCP"].has_key("options"): kwargs["TCP"]["options"].append(("Timestamp", (t1, t2))) else: kwargs["TCP"]["options"] = [("Timestamp", (t1, t2))] if res: kwargs["IP"]["flags"] = 2 return packet, kwargs if __name__ == "__main__": stack = PyStack() steganoapp = SteganoApplication() server = TCPApplication() stack.register_tcp_application(server) server.bind(7777, steganoapp, True) server.listen(5) stack.run(doreactor=True) #stack.stop() called when Ctrl+C
if __name__ == "__main__": app = QApplication(sys.argv) app.setApplicationName("SteganoChat") win = ChatWindow() stack = PyStack() stack.run(doreactor=False) steganoapp = SteganoApplication(win) #--Server-- #server = TCPApplication() #stack.register_tcp_application(server) #---------- #--Client-- stack.register_tcp_application(steganoapp) #---------- win.set_tcpapp(steganoapp) steganoapp.run() #--Server-- #server.bind(7777, steganoapp, True) #server.listen(1) # #---------- #--Client-- steganoapp.connect("192.168.1.38", 7777) #---------- app.exec_()
def hook_outgoing(self, packet, **kwargs): ''' Send data covertly, by using ISN, and IPID ''' if not self.streaming_finished: if kwargs["TCP"]["flags"] in (2, 18): value,_ = self.get_bytes(4) #seqNb can hold 4 bytes if value: kwargs["TCP"]["seq"] = value self.lowerLayers["default"].seqNo = value self.lowerLayers["default"].nextAck = value # !! A nicer way would have been to overwrite _send_SYN and _send_SYNACK methods value, res = self.get_bytes(2) if value: kwargs["IP"]["id"] = value if res: kwargs["IP"]["flags"] = 2 return packet, kwargs if __name__ =="__main__": stack = PyStack() steganoserver = SteganoApplication() stack.register_tcp_application(steganoserver) steganoserver.bind(7777, steganoserver, True) steganoserver.listen(5) stack.run(doreactor=True) #stack.stop() called when Ctrl+C
self.reception_finished = True def hook_outgoing(self, packet, **kwargs): if kwargs["TCP"]["flags"] == 2: self.firstseq = kwargs["TCP"]["seq"] self.ctr = int(bin(kwargs["TCP"]["seq"])[-16:], 2) return packet, kwargs if __name__ == "__main__": stack = PyStack() stack.run(doreactor=False) steganoclient = SteganoApplication() stack.register_tcp_application(steganoclient) if steganoclient.connect("192.168.1.37", 7777): steganoclient.send_packet("Hello") steganoclient.send_packet("my") steganoclient.send_packet("name") steganoclient.send_packet("is") steganoclient.send_packet("XXXX") steganoclient.send_packet("this") steganoclient.send_packet("is") steganoclient.send_packet("a") steganoclient.send_packet("test") steganoclient.send_packet("of") steganoclient.send_packet("steganography") steganoclient.send_packet("and")
from pystack.layers.tcp_application import TCPApplication from pystack.pystack import PyStack class EchoServer(TCPApplication): def packet_received(self, packet, **kwargs): #Just overwrite packetreceived self.send_packet( packet, **kwargs ) #Just call reply the same data received by calling send_packet if __name__ == "__main__": stack = PyStack( iface=sys.argv[1] if len(sys.argv) > 1 else None) #Create the stack echoserver = EchoServer() #Instanciate the TCPApplication stack.register_tcp_application( echoserver) #Register the TCPApplication on the stack echoserver.bind(8888, echoserver, False) #Bind the TCPApplication to wait for connections #8888 is the port, echoserver means that for every client an echoserver application will be used and False means # that the echoserver instance will not be forked for every client so they will all by managed by the same instance # A shortcut would have simply been: echoserver.bind(5555) because by default new client are mapped on the same app without creating new instance echoserver.listen(5) #Listen 5 clients at max stack.run(doreactor=True) #stack.stop() called when Ctrl+C
def loseConnection(self): pass def getvalue(self): pass if __name__ == "__main__": from pystack.pystack import PyStack import sys iface = sys.argv[1] if len(sys.argv) > 1 else None stack = PyStack(iface=iface) # Create a stack webserver = WebServer() # Create an instance of the TCPApplication stack.register_tcp_application(webserver) webserver.bind(80, app=webserver, newinstance=True) webserver.listen(5) stack.run(doreactor=True) # Run the stack """ #---- Solution where we create our stack by hand ---- from pystack.layers.ethernet import EthernetProtocol from pystack.layers.ip import IPProtocol from pystack.layers.arp import ARPProtocol from pystack.layers.tcp import TCPProtocol from pystack.layers.tcp_session import TCPSession interface = "wlan0" eth = EthernetProtocol(interface)
if __name__ == "__main__": app = QApplication(sys.argv) app.setApplicationName("SteganoChat") win = ChatWindow() stack = PyStack() stack.run(doreactor=False) steganoapp = SteganoApplication(win) #--Server-- #server = TCPApplication() #stack.register_tcp_application(server) #---------- #--Client-- stack.register_tcp_application(steganoapp) #---------- win.set_tcpapp(steganoapp) steganoapp.run() #--Server-- #server.bind(7777, steganoapp, True) #server.listen(1) # #---------- #--Client-- steganoapp.connect("192.168.1.38", 7777) #---------- app.exec_()
if kwargs["IP"]["flags"] == 2: #End of transfer print "Covert message: ", self.reassembled_stream self.reception_finished = True def hook_outgoing(self, packet, **kwargs): return packet, kwargs if __name__ =="__main__": stack = PyStack() stack.run(doreactor=False) steganoclient = SteganoApplication() stack.register_tcp_application(steganoclient) steganoclient.connect("192.168.1.38", 7777) steganoclient.send_packet("Hello") steganoclient.send_packet("world") steganoclient.send_packet("\n") steganoclient.send_packet("I") steganoclient.send_packet("try") steganoclient.send_packet("this.") steganoclient.send_packet("And this is") steganoclient.send_packet("a") steganoclient.send_packet("test") steganoclient.send_packet("of") steganoclient.send_packet("steganography") steganoclient.send_packet("and")
Copyright (c) 2012 Robin David PyStack is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version http://www.gnu.org/licenses/. ''' import sys from pystack.layers.tcp_application import TCPApplication from pystack.pystack import PyStack class EchoServer(TCPApplication): def packet_received(self, packet, **kwargs): #Just overwrite packetreceived self.send_packet(packet, **kwargs) #Just call reply the same data received by calling send_packet if __name__ =="__main__": stack = PyStack(iface=sys.argv[1] if len(sys.argv) > 1 else None) #Create the stack echoserver = EchoServer() #Instanciate the TCPApplication stack.register_tcp_application(echoserver) #Register the TCPApplication on the stack echoserver.bind(8888, echoserver, False) #Bind the TCPApplication to wait for connections #8888 is the port, echoserver means that for every client an echoserver application will be used and False means # that the echoserver instance will not be forked for every client so they will all by managed by the same instance # A shortcut would have simply been: echoserver.bind(5555) because by default new client are mapped on the same app without creating new instance echoserver.listen(5) #Listen 5 clients at max stack.run(doreactor=True) #stack.stop() called when Ctrl+C
def loseConnection(self): pass def getvalue(self): pass if __name__ =="__main__": from pystack.pystack import PyStack import sys iface = sys.argv[1] if len(sys.argv) > 1 else None stack = PyStack(iface=iface) sshserver = SSHServer() stack.register_tcp_application(sshserver) sshserver.bind(22, sshserver, newinstance=True) #Or echoserver.bind(5555) because by default new client are mapped on the same app without creating new instance sshserver.listen(5) stack.run(doreactor=True) #stack.stop() called when Ctrl+C ''' #---- Solution where we create our stack by hand ---- from pystack.layers.ethernet import EthernetProtocol from pystack.layers.ip import IPProtocol from pystack.layers.arp import ARPProtocol from pystack.layers.tcp import TCPProtocol from pystack.layers.tcp_session import TCPSession interface = "eth0"