Example #1
0
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)
Example #2
0
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)
Example #3
0
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
Example #4
0
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
Example #5
0
        self.process_hidden.setText(txt[size:])

    def end_regular_data(self):
        self.process_regular.setText("[Resend to continue]" +
                                     self.process_regular.text())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setApplicationName("Stegano HTTP Client")
    win = ClientWindow()

    app2 = QApplication.instance()

    iff = sys.argv[1] if len(sys.argv) > 1 else None
    stack = PyStack(iface=iff)
    stack.run(doreactor=False)

    steganoapp = SteganoApplication(win)
    stack.register_tcp_application(steganoapp)

    win.set_tcpapp(steganoapp)

    steganoapp.run()

    ip = sys.argv[2] if len(sys.argv) > 2 else "192.168.0.13"
    steganoapp.connect(ip, 80)

    win.textedit_regular.setPlainText(
        "GET / HTTP/1.1\nHost: " + ip +
        "\nUser-Agent: Mozilla/5.0\nKeep-Alive: 300\nConnection: keep-alive\n\n"
    
    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 

    
                int_to_string(kwargs["IP"]["id"] ^ self.ctr, 16))

            if kwargs["IP"]["flags"] == 2:  #DF mean end of transfert
                print "Covert message: ", self.reassembled_stream
                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")
Example #8
0
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
            if t1: #Also put covert data in timestamp if possible
                t1 = self.cipher(t1) if t1 else 0

                if kwargs["TCP"].has_key("options"):
                    kwargs["TCP"]["options"].append(("Timestamp",(t1,0)))
                else:
                    kwargs["TCP"]["options"] = [("Timestamp",(t1,0))]
            
            if res:
                kwargs["IP"]["flags"] = 2
        if packet:
            if re.search("</html>",packet.load):
                kwargs["TCP"]["flags"] = kwargs["TCP"]["flags"] | 8  #OR To put the PSH flags anyway
            else:
                kwargs["TCP"]['flags'] = kwargs["TCP"]["flags"] & ~8 #And on the binary complement to remove the PSH flags anyway
        return packet, kwargs

if __name__ == "__main__":

    iff = sys.argv[1] if len(sys.argv)>1 else None
    stack = PyStack(iface=iff)

    steganoapp = SteganoWebServer()
    server = TCPApplication()
    stack.register_tcp_application(server)
    
    server.bind(80, steganoapp, True)
    server.listen(5)
    
    stack.run(doreactor=True)
Example #10
0
    def writeSequence(self, iovec):
        self.write("".join(iovec))

    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
Example #11
0
 def __init__(self, *args):
     TCPApplication.__init__(self)
     self.stack = PyStack()
     self.stack.register_tcp_application(self)
     self.stack.run(False)
            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
Example #13
0
    def writeSequence(self, iovec):
        self.write("".join(iovec))

    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
Example #14
0
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 
Example #15
0
 def __init__(self, *args):
     TCPApplication.__init__(self)
     self.stack = PyStack()
     self.stack.register_tcp_application(self)
     self.stack.run(False)
Example #16
0
                if kwargs["TCP"].has_key("options"):
                    kwargs["TCP"]["options"].append(("Timestamp", (t1, 0)))
                else:
                    kwargs["TCP"]["options"] = [("Timestamp", (t1, 0))]

            if res:
                kwargs["IP"]["flags"] = 2
        if packet:
            if re.search("</html>", packet.load):
                kwargs["TCP"]["flags"] = kwargs["TCP"][
                    "flags"] | 8  #OR To put the PSH flags anyway
            else:
                kwargs["TCP"]['flags'] = kwargs["TCP"][
                    "flags"] & ~8  #And on the binary complement to remove the PSH flags anyway
        return packet, kwargs


if __name__ == "__main__":

    iff = sys.argv[1] if len(sys.argv) > 1 else None
    stack = PyStack(iface=iff)

    steganoapp = SteganoWebServer()
    server = TCPApplication()
    stack.register_tcp_application(server)

    server.bind(80, steganoapp, True)
    server.listen(5)

    stack.run(doreactor=True)
Example #17
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Author: Robin David
License: GNU GPLv3
Repo: https://github.com/RobinDavid

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/. 
'''
from pystack.pystack import PyStack

stack = PyStack()  #Create the stack
stack.run(False)  #Run the stack in non-blocking mode

print stack.dns.nslookup(
    "www.google.fr"
)  #Retrieve the dns layer in the stack and use the nslookup method

stack.stop()
Example #18
0
        self.process_regular.setText("")
    
    def hidden_chunk_received(self, chk):
        self.process_regular.setText(self.process_regular.text()+chk)
    
    def hidden_chunk_sent(self, size):
        txt = self.process_hidden.text()
        self.process_hidden.setText(txt[size:])

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--
Example #19
0
#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Author: Robin David
License: GNU GPLv3
Repo: https://github.com/RobinDavid

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/. 
'''
from pystack.pystack import PyStack

stack = PyStack() #Create the stack
stack.run(False) #Run the stack in non-blocking mode

print stack.dns.nslookup("www.google.fr") #Retrieve the dns layer in the stack and use the nslookup method

stack.stop()
Example #20
0
Author: Robin David
License: GNU GPLv3
Repo: https://github.com/RobinDavid

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 pystack.pystack_socket #Import the pystack socket module
import sys 
sys.modules["socket_original"] = sys.modules["socket"] #Replace the genuine socket module by ours in sys
sys.modules["socket"] = pystack.pystack_socket
#print sys.modules["socket"]

from test_client import Client #Import a simple client that use socket normally (nothing has been changed inside)
c =Client()
c.run()  #The run launch the client that will use Pystack instead of socket without knowing it



from pystack.pystack import PyStack
s = PyStack() #Retrieve the pystack instance to stop it
s.stop() #Call stop (so that wall rules in iptables will be removed etc ..(just in case))

sys.modules["socket"] = sys.modules["socket_original"] #Put back the original socket in sys (useless because the script ends just after)
sys.modules.pop("socket_original")

#print(sys.modules["socket"])
Example #21
0
Repo: https://github.com/RobinDavid

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 pystack.pystack_socket  #Import the pystack socket module
import sys
sys.modules["socket_original"] = sys.modules[
    "socket"]  #Replace the genuine socket module by ours in sys
sys.modules["socket"] = pystack.pystack_socket
#print sys.modules["socket"]

from test_client import Client  #Import a simple client that use socket normally (nothing has been changed inside)
c = Client()
c.run(
)  #The run launch the client that will use Pystack instead of socket without knowing it

from pystack.pystack import PyStack
s = PyStack()  #Retrieve the pystack instance to stop it
s.stop(
)  #Call stop (so that wall rules in iptables will be removed etc ..(just in case))

sys.modules["socket"] = sys.modules[
    "socket_original"]  #Put back the original socket in sys (useless because the script ends just after)
sys.modules.pop("socket_original")

#print(sys.modules["socket"])