Click here to Skip to main content
14,975,862 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
[+] Request >>
Exception TypeError: "'module' object has no attribute '__getitem__'" in 'netfilterqueue.global_callback' ignored

Exception IndexError: IndexError('Layer [TCP] not found',) in 'netfilterqueue.global_callback' ignored

These two errors come while executing my python code given:

What I have tried:

#!/usr/bin/env python
import netfilterqueue
import scapy.all as scapy
import re

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

def process_packet(packet):
scapy_packet = scapy.IP(packet.get_payload())
if scapy_packet.haslayer(scapy.Raw):
if scapy_packet[scapy.TCP].dport == 80:
print("[+] Request >>")
modified_load = re.sub("Accept-Encoding:.*?\\r\\n", "", scapy_packet[scapy.Raw].load)
new_packet = set_load(scapy_packet, modified_load)
packet.set_payload(str(new_packet))
elif scapy_packet[scapy.TCP].sport == 80:
print("[+] Response >>")
print(scapy_packet.show())

packet.accept()

queue = netfilterqueue.NetfilterQueue()
queue.bind(0, process_packet)

queue.run()
Posted
Updated 16-Dec-20 5:06am

hi I would change your if statement to:

if scapy.Raw in scapy_packet and scapy.TCP in scapy_packet:

instead of:

if scapy_packet.haslayer(scapy.Raw):
   
Compare this with yours though its the same code but I changed some variables.
Make sure your are testing it on http(port 80) not https(port443) and be mindful of your indents too

import netfilterqueue
import scapy.all as scapy
import re


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

def process_packet(packet):
    scapy_packet = scapy.IP(packet.get_payload())
    if scapy_packet.haslayer(scapy.Raw):
        if scapy_packet[scapy.TCP].dport == 80:
            print('Request..')
            mod_load = re.sub('Accept-Encoding:.*?\\r\\n','',scapy_packet[scapy.Raw].load)
            new_pack = set_load(scapy_packet, mod_load)
            packet.set_payload(str(new_pack))
        elif scapy_packet[scapy.TCP].sport == 80:
                print('Response...')
                print(scapy_packet.show())



    packet.accept()


queue = netfilterqueue.NetfilterQueue()
queue.bind(0, process_packet)
queue.run()
   
v3
Comments
Dave Kreskowiak 16-Dec-20 11:55am
   
Look at the date on the question. I seriously doubt the OP is still looking for an answer two years later.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900