I use NetfilterQueue and scapy to end the monitoring program and package spoofing.
But I'm just worried that the set_payload () method of the NetfilterQueue module does not seem to work for me.
Here is my source code.
from netfilterqueue import NetfilterQueue as nfqueue
from scapy.all import *
import os
import socket
import re
baidu_ip = socket.gethostbyname('www.baidu.com')
print "Got baidu ip: " + baidu_ip
iptablesr = 'iptables -t mangle -A POSTROUTING -p tcp -j NFQUEUE --queue-num 1'
print("Adding iptable rules :")
print(iptablesr)
os.system(iptablesr)
iptablesr = 'iptables -t mangle -A POSTROUTING -p udp -j NFQUEUE --queue-num 1'
print(iptablesr)
os.system(iptablesr)
def callback(packet):
modified = False
sca_pkt = IP(packet.get_payload())
try:
dns_lookup = re.search(r'DNS Qry "([\w\.]+)"', sca_pkt[DNS].summary()).group(1)
if re.search(r'baidu\.com', dns_lookup) != None:
sca_pkt[DNS].qd = DNSQR(qname='www.163.com')
sca_pkt[UDP].len = len(bytes(sca_pkt[UDP]))
sca_pkt[UDP].chksum = 0x0000
modified = True
except:
pass
if modified == True:
print 'debug sca_pkt: ' + sca_pkt.summary()
packet.set_payload(str(sca_pkt))
print 'debug packet: ' + IP(packet.get_payload()).summary()
packet.accept()
return
packet.accept()
def main():
q = nfqueue()
q.bind(1, callback)
try:
q.run()
except KeyboardInterrupt:
q.unbind()
print "Flushing iptables."
os.system('iptables -F')
os.system('iptables -F -t mangle')
if __name__ == '__main__':
main()
And this is the result that I got.
Got baidu ip: 180.97.33.108
Adding iptable rules :
iptables -t mangle -A POSTROUTING -p tcp -j NFQUEUE --queue-num 1
iptables -t mangle -A POSTROUTING -p udp -j NFQUEUE --queue-num 1
debug sca_pkt: IP / UDP / DNS Qry "www.163.com"
debug packet: IP / UDP / DNS Qry "baidu.com."
You can see from the debug information that set_payload () is not working. (I set the packet to the value shown in sca_pkt, but when I get the final value of the packet payload, it is still the original value)
And I just tried two versions of NetfilterQueue, including the pip version and https://github.com/kti/python-netfilterqueue.git .
So can someone help me? I'm just really interested about that.