NFQueue / Scapy Man in Middle

时间:2019-06-28 14:55:51

标签: python http scapy packet

我正在尝试在网页的中间攻击中构造一个人(即HTTP流量)。我通过使用连接到以太网的Linux机器和通过其WiFi热点连接到Linux盒的客户端来进行此操作。

到目前为止,我所做的是从IPTables Linux防火墙中使用NFQueue将FORWARD链上的所有TCP数据包路由到NFQueue队列,该队列由Python脚本拾取并处理这些规则。我能够从HTTP响应数据包中读取数据,但是每当我尝试修改它们并将它们传递回(接受数据包)时,都会遇到有关字符串的错误:

Exception AttributeError: "'str' object has no attribute 'build_padding'" in 'netfilterqueue.global_callback' ignored

我的代码在这里,其中包含我尝试过的无效的内容。值得注意的是,我正在使用scapy的第三方扩展名scapy_http,它可能会干扰事物,并且我使用的网页没有被gzip压缩,因为它弄乱了事物也一样我正在使用的测试网页为here

#scapy
from scapy.all import *

#nfqueue import
from netfilterqueue import NetfilterQueue

#scapy http extension, not really needed
import scapy_http.http

#failed gzip decoding, also tried some other stuff
#import gzip

def print_and_accept(packet):
    #convert nfqueue datatype to scapy-compatible
    pkt = IP(packet.get_payload())

    #is this an HTTP response?
    if pkt[TCP].sport == 80:
        #legacy trial that doesn't work
        #data = packet.get_data()
        print('HTTP Packet Found')

        #check what's in the payload
        stringLoad = str(pkt[TCP].payload)

        #deleted because printing stuff out clogs output
        #print(stringLoad)

        #we only want to modify a specific packet:
        if "<title>Acids and Bases: Use of the pKa Table</title>" in stringLoad:
            print('Target Found')

            #strings kind of don't work, I think this is a me problem
            #stringLoad.replace('>Acids and Bases: Use of the pK<sub>a</sub>', 'This page has been modified: a random ')
            #pkt[TCP].payload = stringLoad


            #https://stackoverflow.com/questions/27293924/change-tcp-payload-with-nfqueue-scapy
            payload_before = len(pkt[TCP].payload)

            # I suspect this line is a problem: the string assigns,
            # but maybe under the hood scapy doesn't like that very much
            pkt[TCP].payload = str(pkt[TCP].payload).replace("Discussion", "This page has been modified")

            #recalculate length
            payload_after = len(pkt[TCP].payload)

            payload_dif = payload_after - payload_before

            pkt[IP].len = pkt[IP].len + payload_dif

            #recalculate checksum
            del pkt[TCP].chksum
            del pkt[IP].chksum
            del pkt.chksum


            print('Packet Modified')
            #redudant
            #print(stringLoad)

            #this throws an error (I think)
            print(str(pkt[TCP].payload))
            #no clue if this works or not yet
            #goal here is to reassign modified packet to original parameter
            packet.set_payload(str(pkt))

            #this was also throwing the error, so tried to move away from it
            #print(pkt.show2())

        #bunch of legacy code that didn't work
        #print(GET_print(pkt))
        #print(pkt.show())
        #decompressed_data = zlib.decompress(str(pkt[TCP].payload), 16 + zlib.MAX_WBITS)
        #print(decompressed_data)
        #print(str(gzip.decompress(pkt[TCP].payload)))
        # print(pkt.getlayer(Raw).load)

        #print('HTTP Contents Shown')

    packet.accept()

def GET_print(packet1):
    ret = "***************************************GET PACKET****************************************************\n"
    ret += "\n".join(packet1.sprintf("{Raw:%Raw.load%}\n").split(r"\r\n"))
    ret += "*****************************************************************************************************\n"
    return ret

print('Test: Modify a very specific target')
print('Program Starting')
nfqueue = NetfilterQueue()
nfqueue.bind(1, print_and_accept)
try:
    print('Packet Interface Starting')
    nfqueue.run()
except KeyboardInterrupt:
    print('\nProgram Ending')

nfqueue.unbind()

如果无法阅读或格式错误,请提前道歉; Python不是我经常使用的语言。任何帮助将不胜感激!

0 个答案:

没有答案