Exemplo n.º 1
0
    def run(self):
        while self.is_running:
            for data in iter(self.inbuffer.get, None):
                
                # translation d'adresses niveau 2
                if self.num == 0:
                    data = data[:6]+netlib.format_mac(self.outMac)+data[12:] # switch MAC
                elif self.num == 1:
                    data = netlib.format_mac(self.outMac)+data[6:] # switch MAC

                # translation d'adresses niveau 3 + calcul de checksums
                self.type3 = netlib.get_type3(data[12:14])
                
                if self.type3 == "0806": # si ARP
                    data = netlib.arp_rewriting(data,self.outMac,self.outIp,self.num)
                    # optimisation : si preformat lors de init, moins de taff dans netlib
                    
                elif self.type3 == "0800": # si IPv4
                    ipsrc = data[26:30]
                    ipdst = data[30:34]
                    # optimisation : passer le format_ip dans l'init
                    if self.num == 0:
                        if ipsrc == netlib.format_ip(self.inIp):
                            ipsrc = netlib.format_ip(self.outIp)
                    elif self.num == 1:
                        if ipdst == netlib.format_ip(self.inIp):
                            ipdst = netlib.format_ip(self.outIp)
                    data = netlib.ip_rewriting(data,ipsrc,ipdst) # nat + checksum
                    
                    if netlib.udp_or_tcp(data[23]): # si udp ou tcp
                        data = netlib.tcpudp_rewriting(data) # checksum niveau4
                        
                self.otbuffer.put(data)
        return 
Exemplo n.º 2
0
    def run(self):
        while self.is_running:
            for data in iter(self.inbuffer.get, None):

                # translation d'adresses niveau 2
                if self.num == 0:
                    data = data[:6] + netlib.format_mac(
                        self.outMac) + data[12:]  # switch MAC
                elif self.num == 1:
                    data = netlib.format_mac(
                        self.outMac) + data[6:]  # switch MAC

                # translation d'adresses niveau 3 + calcul de checksums
                self.type3 = netlib.get_type3(data[12:14])

                if self.type3 == "0806":  # si ARP
                    data = netlib.arp_rewriting(data, self.outMac, self.outIp,
                                                self.num)
                    # optimisation : si preformat lors de init, moins de taff dans netlib

                elif self.type3 == "0800":  # si IPv4
                    ipsrc = data[26:30]
                    ipdst = data[30:34]
                    # optimisation : passer le format_ip dans l'init
                    if self.num == 0:
                        if ipsrc == netlib.format_ip(self.inIp):
                            ipsrc = netlib.format_ip(self.outIp)
                    elif self.num == 1:
                        if ipdst == netlib.format_ip(self.inIp):
                            ipdst = netlib.format_ip(self.outIp)
                    data = netlib.ip_rewriting(data, ipsrc,
                                               ipdst)  # nat + checksum

                    if netlib.udp_or_tcp(data[23]):  # si udp ou tcp
                        data = netlib.tcpudp_rewriting(
                            data)  # checksum niveau4

                self.otbuffer.put(data)
        return
Exemplo n.º 3
0
    def run(self):

        while self.is_running:
            for data in iter(self.inbuffer.get, None):

                # recognising protocols
                self.type3 = netlib.get_type3(data[12:14])
                if self.type3 == "0800":  # what about ipv6 ? what about other protocols ?
                    try:
                        self.type4 = netlib.get_type4(
                            data[23]
                        )  # get type4 for ipv4 VS get type4 for ipv6 ?
                    except Exception:
                        self.type4 = ""
                        pass

                # doin' stegano ? filtering step
                if self.check_steg(
                        data
                ):  # a optimiser en envoyant que les datas necessaires

                    # BINPACK
                    data = steglib.binpack(data)

                    # UNPAQUET
                    paquet = [
                    ]  # will contain list of dicts(name, position, size, workable, value)

                    # unpacket level 2
                    header, data = packetparser.unPaquet(
                        data, "Ethernet")  # quid not Ethernet ?
                    paquet += [header]

                    # unpacket level 3
                    try:
                        header, data = packetparser.unPaquet(
                            data,
                            {v: k
                             for k, v in stegconf.NIV3_DICT.items()
                             }[self.type3])
                        paquet += [header]
                    except Exception as e:
                        print "ACHTUNG ! unPaquet level 3 for " + str(
                            self.type3) + ": " + str(e)
                        pass

                    # unpacket level 4
                    if self.type4 != "":
                        try:
                            header, data = packetparser.unPaquet(
                                data,
                                {v: k
                                 for k, v in stegconf.NIV4_DICT.items()
                                 }[self.type4])
                            paquet += [header]
                        except Exception as e:
                            print "ACHTUNG ! unPaquet level 4 for " + str(
                                self.type4) + ": " + str(e)
                            pass

                    # GET WORKABLE BITS
                    tab = [
                    ]  # will contain ''.join(getWorkableBits(header)), a str(list of workable bits)
                    if F_NIV3_STEG and len(paquet) > 1:
                        tab += [''.join(steglib.getWorkableBits(paquet[1]))
                                ]  # manipulera un header
                    if F_NIV4_STEG and len(paquet) > 2:
                        tab += [''.join(steglib.getWorkableBits(paquet[2]))
                                ]  # manipulera un header

                    totalen = sum([len(t) for t in tab])

                    # INSERTION / EXTRACTION
                    if totalen > 6:  # enough space for protocol and data ?
                        if self.num == 0:  # insertion
                            print "INSERTION START"

                            # maxsize according to protocol, max 31 (3*8+7) bits plus 6 (2+3+1) bits of protocol
                            localbuff = list()

                            # data waiting to be steganographied
                            newlen = min(totalen, 31 + 6)
                            if not self.craftbuffer.empty():
                                while (len(localbuff) < (newlen - 6)
                                       and not (self.craftbuffer.empty())):
                                    localbuff.append(self.craftbuffer.get())

                                S = int(len(localbuff) / 8.0)
                                O = len(localbuff) - S * 8

                                temptab = ''.join(tab)
                                while len(localbuff) < (totalen - 6):
                                    localbuff.append(
                                        str(temptab[len(
                                            localbuff)]))  # too slow ?

                                localbuff = list(bin(S)[2:].zfill(2)) + list(
                                    bin(O)[2:].zfill(3)) + localbuff  # len+5

                                localbuff.append(
                                    str(
                                        steglib.xor_complement1(
                                            map(int,
                                                localbuff))))  # ajout du flag

                                # localbuff a ete charge au maximum disponible, size, offset, padding et flag inclus

                            else:  # no data waiting
                                localbuff = list(''.join(tab)[:-1])

                                localbuff.insert(
                                    random.randint(0, len(localbuff)),
                                    str(
                                        int(not (steglib.xor_complement1(
                                            map(int, localbuff)))))
                                )  # insertion du flag

                            # SET WORKABLE BITS
                            if F_NIV3_STEG and len(
                                    paquet) > 1:  # implies len(tab)>0
                                paquet[1] = steglib.setWorkableBits(
                                    paquet[1], localbuff[:len(tab[0])])
                            if F_NIV4_STEG and len(
                                    paquet) > 2:  # implies len(tab)>1
                                paquet[2] = steglib.setWorkableBits(
                                    paquet[2],
                                    localbuff[len(tab[0]):len(tab[0]) +
                                              len(tab[1])])

                        elif self.num == 1:  # extraction
                            print "EXTRACTION START"
                            if steglib.xor_check(map(int, list(
                                    ''.join(tab)))):  # flag check
                                localbuff = list(''.join(tab)[:-1])
                                dsize = (int(''.join(localbuff[:2]), 2) *
                                         8) + int(
                                             ''.join(localbuff[2:5]),
                                             2)  # S+O (number of bytes+offset)
                                localbuff = localbuff[5:dsize + 5]
                                while len(localbuff) > 0:
                                    self.craftbuffer.put(localbuff.pop(0))

                    # REPAQUET
                    bindata = packetparser.repaquet(paquet, data)

                    # BINUNPACK
                    data = steglib.binunpack(bindata)

                #print "HANDING THE PACKET OVER"
                self.otbuffer.put(data)
        return
Exemplo n.º 4
0
    def run(self):
        
        while self.is_running:
            for data in iter(self.inbuffer.get, None):

                # recognising protocols
                self.type3 = netlib.get_type3(data[12:14])
                if self.type3 == "0800": # what about ipv6 ? what about other protocols ?
                    try:
                        self.type4 = netlib.get_type4(data[23]) # get type4 for ipv4 VS get type4 for ipv6 ?
                    except Exception:
                        self.type4 = ""
                        pass

                # doin' stegano ? filtering step
                if self.check_steg(data) : # a optimiser en envoyant que les datas necessaires
                    
                    # BINPACK
                    data = steglib.binpack(data)

                    # UNPAQUET
                    paquet = [] # will contain list of dicts(name, position, size, workable, value)
                    
                    # unpacket level 2
                    header,data = packetparser.unPaquet(data,"Ethernet") # quid not Ethernet ?
                    paquet += [header]
                    
                    # unpacket level 3
                    try:
                        header,data = packetparser.unPaquet(data,{v: k for k,v in stegconf.NIV3_DICT.items()}[self.type3])
                        paquet += [header]
                    except Exception as e:
                        print "ACHTUNG ! unPaquet level 3 for "+str(self.type3)+": "+str(e)
                        pass
                    
                    # unpacket level 4
                    if self.type4 != "":
                        try:
                            header, data = packetparser.unPaquet(data,{v: k for k,v in stegconf.NIV4_DICT.items()}[self.type4])
                            paquet += [header]
                        except Exception as e:
                            print "ACHTUNG ! unPaquet level 4 for "+str(self.type4)+": "+str(e)
                            pass

                    # GET WORKABLE BITS
                    tab = [] # will contain ''.join(getWorkableBits(header)), a str(list of workable bits)
                    if F_NIV3_STEG and len(paquet)>1:
                        tab += [''.join(steglib.getWorkableBits(paquet[1]))] # manipulera un header
                    if F_NIV4_STEG and len(paquet)>2:
                        tab += [''.join(steglib.getWorkableBits(paquet[2]))] # manipulera un header
                    
                    totalen = sum([len(t) for t in tab])

                    # INSERTION / EXTRACTION
                    if totalen>6: # enough space for protocol and data ?
                        if self.num == 0 : # insertion
                            print "INSERTION START"
                            
                            # maxsize according to protocol, max 31 (3*8+7) bits plus 6 (2+3+1) bits of protocol
                            localbuff = list()
                            
                            # data waiting to be steganographied
                            newlen = min(totalen,31+6)
                            if not self.craftbuffer.empty():
                                while (len(localbuff)<(newlen-6) and not (self.craftbuffer.empty())):
                                       localbuff.append(self.craftbuffer.get())

                                S = int(len(localbuff)/8.0)
                                O = len(localbuff) - S*8

                                temptab = ''.join(tab)
                                while len(localbuff)<(totalen-6):
                                    localbuff.append(str(temptab[len(localbuff)])) # too slow ?

                                localbuff = list(bin(S)[2:].zfill(2))+list(bin(O)[2:].zfill(3))+localbuff # len+5
                                
                                localbuff.append(str(steglib.xor_complement1(map(int, localbuff)))) # ajout du flag
                                
                                # localbuff a ete charge au maximum disponible, size, offset, padding et flag inclus
                                
                            else: # no data waiting
                                localbuff = list(''.join(tab)[:-1])
                                
                                localbuff.insert(random.randint(0,len(localbuff)),str(int(not(steglib.xor_complement1(map(int,localbuff)))))) # insertion du flag

                            # SET WORKABLE BITS
                            if F_NIV3_STEG and len(paquet)>1: # implies len(tab)>0
                                paquet[1] = steglib.setWorkableBits(paquet[1],localbuff[:len(tab[0])])
                            if F_NIV4_STEG and len(paquet)>2: # implies len(tab)>1
                                paquet[2] = steglib.setWorkableBits(paquet[2],localbuff[len(tab[0]):len(tab[0])+len(tab[1])])
                            
                        elif self.num == 1 : # extraction
                            print "EXTRACTION START"
                            if steglib.xor_check(map(int,list(''.join(tab)))): # flag check
                                localbuff = list(''.join(tab)[:-1])
                                dsize = (int(''.join(localbuff[:2]),2)*8)+int(''.join(localbuff[2:5]),2) # S+O (number of bytes+offset)
                                localbuff = localbuff[5:dsize+5]
                                while len(localbuff)>0:
                                    self.craftbuffer.put(localbuff.pop(0))

                    # REPAQUET
                    bindata = packetparser.repaquet(paquet,data)

                    # BINUNPACK
                    data = steglib.binunpack(bindata)

                #print "HANDING THE PACKET OVER"
                self.otbuffer.put(data)
        return