Shadow User
Green Team
Python:
#!/usr/bin/env python2
#-*- coding: utf-8 -*-
import socket
import struct
#
buff_size = 1500
#
def get_ip_header(data):
tmp = struct.unpack('!BBHHHBBH4s4s', data[0:20])
ip_header = {}
#
ip_header['version'] = tmp[0] >> 4
ip_header['ihl'] = (tmp[0] & 0xF) * 4
ip_header['tos'] = tmp[1]
ip_header['size'] = tmp[2]
ip_header['id'] = tmp[3]
ip_header['flags'] = tmp[4] >> 13
ip_header['offset'] = tmp[4] & 0x1FFF
ip_header['ttl'] = tmp[5]
ip_header['protocol'] = tmp[6]
ip_header['crc'] = tmp[7]
ip_header['src'] = socket.inet_ntoa(tmp[8])
ip_header['dst'] = socket.inet_ntoa(tmp[9])
#
return ip_header
#
#
def get_tcp_header(data):
tmp = struct.unpack('!HHLLBBHHH', data[0:20])
tcp_header = {}
#
tcp_header['src'] = tmp[0]
tcp_header['dst'] = tmp[1]
tcp_header['sn'] = tmp[2]
tcp_header['an'] = tmp[3]
tcp_header['size'] = (tmp[4] >> 4) * 4
tcp_header['flags'] = tmp[5]
tcp_header['window'] = tmp[6]
tcp_header['crc'] = tmp[7]
tcp_header['urg'] = tmp[8]
#
return tcp_header
#
#
def get_tcp_flags(tcp_header):
tmp = tcp_header['flags']
flags = {}
#
flags['URG'] = bool(tmp & 0x20)
flags['ACK'] = bool(tmp & 0x10)
flags['PSH'] = bool(tmp & 0x8)
flags['RST'] = bool(tmp & 0x4)
flags['SYN'] = bool(tmp & 0x2)
flags['FIN'] = bool(tmp & 0x1)
#
return flags
#
#
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
#
except socket.error: print 'Fail! Socket not created!'
else:
tmp_ip_id = None
#
while True:
try:
data = sock.recv(buff_size)
#
ip_hdr = get_ip_header(data)
tcp_hdr = get_tcp_header(data[ip_hdr['ihl']:ip_hdr['size']])
#
flags = get_tcp_flags(tcp_hdr)
flags = filter(lambda key: flags[key], flags)
flags = map(lambda tmp: ' ' + tmp, flags)
#
if ip_hdr['id'] != tmp_ip_id:
tmp_ip_id = ip_hdr['id']
#
print 'IP_HDR: ID:{} IHL:{} TL:{} SRC:{} DST:{} TTL:{} TOS:{} OFF:{}'.format(
ip_hdr['id'],
ip_hdr['ihl'],
ip_hdr['size'],
ip_hdr['src'],
ip_hdr['dst'],
ip_hdr['ttl'],
ip_hdr['tos'],
ip_hdr['offset']
)
print 'TCP_HDR: SRC:{} DST:{} SN:{} AN:{} OFF:{} WIN:{} FLS:\"{}\"\n'.format(
tcp_hdr['src'],
tcp_hdr['dst'],
tcp_hdr['sn'],
tcp_hdr['an'],
tcp_hdr['size'],
tcp_hdr['window'],
''.join(flags)
)
except KeyboardInterrupt:
print 'Interrupt...'
sock.close()
exit()
#
except Exception as msg:
print 'Error: {}'.format(msg)
sock.close()
exit()
Последнее редактирование: