<s> '''<STR_LIT>''' <EOL> import sys <EOL> import dpkt <EOL> import struct <EOL> import pcap <EOL> import socket <EOL> import time <EOL> from Crypto . Random import random <EOL> from Crypto . Hash import SHA <EOL> from output import PCAPWriter <EOL> from util import getopts <EOL> def hashaddr ( addr , * extra ) : <EOL> global key , ip_range , ip_mask <EOL> sha = SHA . new ( key + addr ) <EOL> for e in extra : <EOL> sha . update ( str ( extra ) ) <EOL> return inttoip ( ( iptoint ( sha . digest ( ) [ <NUM_LIT:0> : len ( addr ) ] ) & ip_mask ) | ip_range ) <EOL> def mangleMAC ( addr ) : <EOL> global zero_mac <EOL> if zero_mac : <EOL> return "<STR_LIT>" <EOL> if addr in emap : <EOL> return emap [ addr ] <EOL> haddr = None <EOL> if addr == "<STR_LIT>" : <EOL> haddr = addr <EOL> if ord ( addr [ <NUM_LIT:0> ] ) & <NUM_LIT> : <EOL> haddr = addr <EOL> if not haddr : <EOL> haddr = hashaddr ( addr ) <EOL> haddr = chr ( ord ( haddr [ <NUM_LIT:0> ] ) & <NUM_LIT> | <NUM_LIT> ) + haddr [ <NUM_LIT:1> : <NUM_LIT:6> ] <EOL> emap [ addr ] = haddr <EOL> return haddr <EOL> def mangleIP ( addr , * ports ) : <EOL> global exclude , exclude_port , anon_all , by_flow <EOL> haddr = None <EOL> intip = iptoint ( addr ) <EOL> if len ( addr ) == <NUM_LIT:4> and intip >= <NUM_LIT> : <EOL> haddr = addr <EOL> ip = iptoa ( addr ) <EOL> if not anon_all and ( ip . startswith ( '<STR_LIT>' ) <EOL> or ip . startswith ( '<STR_LIT>' ) <EOL> or ip . startswith ( '<STR_LIT>' ) <EOL> or ip . startswith ( '<STR_LIT>' ) <EOL> or ip . startswith ( '<STR_LIT>' ) ) : <EOL> haddr = addr <EOL> for x in exclude : <EOL> if ip . startswith ( x ) : <EOL> haddr = addr <EOL> if ports and ports [ <NUM_LIT:0> ] in exclude_port : <EOL> haddr = addr <EOL> if not haddr : <EOL> if by_flow : <EOL> haddr = hashaddr ( addr , * ports ) <EOL> else : <EOL> haddr = hashaddr ( addr ) <EOL> return haddr <EOL> def mangleIPs ( src , dst , sport , dport ) : <EOL> if by_flow : <EOL> if ( src , sport , dst , dport ) in ipmap : <EOL> src , dst = ipmap [ ( src , sport , dst , dport ) ] <EOL> elif ( dst , dport , src , sport ) in ipmap : <EOL> dst , src = ipmap [ ( dst , dport , src , sport ) ] <EOL> else : <EOL> src , dst = ipmap . setdefault ( <EOL> ( src , sport , dst , dport ) , ( mangleIP ( src , sport , dport ) , mangleIP ( dst , dport , sport ) ) ) <EOL> else : <EOL> if src in ipmap : <EOL> src = ipmap [ src ] <EOL> else : <EOL> src = ipmap . setdefault ( src , mangleIP ( src , sport ) ) <EOL> if dst in ipmap : <EOL> dst = ipmap [ dst ] <EOL> else : <EOL> dst = ipmap . setdefault ( dst , mangleIP ( dst , dport ) ) <EOL> return src , dst <EOL> def mactoa ( addr ) : <EOL> return '<STR_LIT::>' . join ( [ '<STR_LIT>' % b for b in struct . unpack ( '<STR_LIT>' , addr ) ] ) <EOL> def iptoa ( addr ) : <EOL> if len ( addr ) is <NUM_LIT:16> : <EOL> return socket . inet_ntop ( socket . AF_INET6 , addr ) <EOL> else : <EOL> return socket . inet_ntop ( socket . AF_INET , addr ) <EOL> def iptoint ( addr ) : <EOL> if len ( addr ) is <NUM_LIT:16> : <EOL> ip = struct . unpack ( '<STR_LIT>' , addr ) <EOL> return ip [ <NUM_LIT:0> ] << <NUM_LIT> | ip [ <NUM_LIT:1> ] << <NUM_LIT:64> | ip [ <NUM_LIT:2> ] << <NUM_LIT:32> | ip [ <NUM_LIT:3> ] <EOL> else : <EOL> return struct . unpack ( '<STR_LIT>' , addr ) [ <NUM_LIT:0> ] <EOL> def inttoip ( l ) : <EOL> if l > <NUM_LIT> : <EOL> return struct . pack ( '<STR_LIT>' , l >> <NUM_LIT> , l >> <NUM_LIT:64> & <NUM_LIT> , l >> <NUM_LIT:32> & <NUM_LIT> , l & <NUM_LIT> ) <EOL> else : <EOL> return struct . pack ( '<STR_LIT>' , l ) <EOL> def pcap_handler ( ts , pktdata ) : <EOL> global init_ts , start_ts , replace_ts , by_flow , anon_mac , zero_mac <EOL> if not init_ts : <EOL> init_ts = ts <EOL> if replace_ts : <EOL> ts = start_ts + ( ts - init_ts ) <EOL> try : <EOL> pkt = dpkt . ethernet . Ethernet ( pktdata ) <EOL> if anon_mac or zero_mac : <EOL> pkt . src = mangleMAC ( pkt . src ) <EOL> pkt . dst = mangleMAC ( pkt . dst ) <EOL> if pkt . type == dpkt . ethernet . ETH_TYPE_IP : <EOL> try : <EOL> sport , dport = pkt . data . data . sport , pkt . data . data . dport <EOL> except : <EOL> sport = dport = None <EOL> pkt . data . src , pkt . data . dst = mangleIPs ( <EOL> pkt . data . src , pkt . data . dst , sport , dport ) <EOL> pktdata = str ( pkt ) <EOL> except Exception , e : <EOL> print e <EOL> out . write ( len ( pktdata ) , pktdata , ts ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> global key , init_ts , start_ts , replace_ts , by_flow , anon_mac , zero_mac , exclude , exclude_port , anon_all , ip_range , ip_mask <EOL> opts , args = getopts ( sys . argv [ <NUM_LIT:1> : ] , '<STR_LIT>' , [ <EOL> '<STR_LIT>' , '<STR_LIT:all>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in opts or '<STR_LIT>' in opts : <EOL> key = random . long_to_bytes ( random . getrandbits ( <NUM_LIT:64> ) , <NUM_LIT:8> ) <EOL> else : <EOL> key = '<STR_LIT>' <EOL> key = opts . get ( '<STR_LIT>' , opts . get ( '<STR_LIT>' , key ) ) <EOL> ip_range = opts . get ( '<STR_LIT>' , opts . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> ip_mask = <NUM_LIT:0> <EOL> ipr = '<STR_LIT>' <EOL> for o in map ( int , ip_range . split ( '<STR_LIT:.>' ) ) : <EOL> ipr += chr ( o ) <EOL> ip_mask <<= <NUM_LIT:8> <EOL> if not o : <EOL> ip_mask |= <NUM_LIT> <EOL> ip_range = iptoint ( ipr ) <EOL> replace_ts = '<STR_LIT>' in opts or '<STR_LIT>' in opts <EOL> by_flow = '<STR_LIT>' in opts or '<STR_LIT>' in opts <EOL> anon_mac = '<STR_LIT>' in opts or '<STR_LIT>' in opts <EOL> zero_mac = '<STR_LIT>' in opts or '<STR_LIT>' in opts <EOL> anon_all = '<STR_LIT>' in opts or '<STR_LIT>' in opts <EOL> start_ts = time . time ( ) <EOL> init_ts = None <EOL> exclude = opts . get ( '<STR_LIT>' , [ ] ) <EOL> exclude . extend ( opts . get ( '<STR_LIT>' , [ ] ) ) <EOL> exclude_port = map ( int , opts . get ( '<STR_LIT>' , [ ] ) ) <EOL> exclude_port . extend ( map ( int , opts . get ( '<STR_LIT>' , [ ] ) ) ) <EOL> emap = { } <EOL> ipmap = { } <EOL> if len ( args ) < <NUM_LIT:2> : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" . upper ( ) <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> out = PCAPWriter ( args [ - <NUM_LIT:1> ] ) <EOL> print '<STR_LIT>' <EOL> for f in args [ <NUM_LIT:0> : - <NUM_LIT:1> ] : <EOL> p = <NUM_LIT:0> <EOL> cap = pcap . pcap ( f ) <EOL> while cap . dispatch ( <NUM_LIT:1> , pcap_handler ) : <EOL> p += <NUM_LIT:1> <EOL> del cap <EOL> print '<STR_LIT>' % ( f , p ) <EOL> out . close ( ) <EOL> print "<STR_LIT>" <EOL> for ia , oa in sorted ( emap . items ( ) ) : <EOL> print '<STR_LIT>' % ( int ( not ia == oa ) , mactoa ( ia ) , mactoa ( oa ) ) <EOL> for ia , oa in sorted ( ipmap . items ( ) ) : <EOL> if by_flow : <EOL> sip , sp , dip , dp = ia <EOL> osip , odip = oa <EOL> print "<STR_LIT>" % ( int ( sip != osip or dip != odip ) , iptoa ( sip ) , sp , iptoa ( dip ) , dp , iptoa ( osip ) , sp , iptoa ( odip ) , dp ) <EOL> else : <EOL> print '<STR_LIT>' % ( int ( ia != oa ) , iptoa ( ia ) , iptoa ( oa ) ) </s>
