33'''
44
55import dshell
6- import util
76import netflowout
87
98
@@ -19,9 +18,9 @@ def __init__(self, **kwargs):
1918 self .alerts = False
2019 self .file = None
2120 dshell .TCPDecoder .__init__ (self ,
22- name = 'country' ,
23- description = 'filter connections on geolocation (country code)' ,
24- longdescription = """
21+ name = 'country' ,
22+ description = 'filter connections on geolocation (country code)' ,
23+ longdescription = """
2524country: filter connections on geolocation (country code)
2625
2726Chainable decoder to filter TCP/UDP streams on geolocation data. If no
@@ -55,28 +54,30 @@ def __init__(self, **kwargs):
5554 decode -d country traffic.pcap -W USonly.pcap --country_code US
5655 decode -d country+followstream traffic.pcap --country_code US --country_notboth
5756""" ,
58- filter = "ip or ip6" ,
59- author = 'twp' ,
60- optiondict = {
61- 'code' : {'type' : 'string' , 'help' : 'two-char country code' },
62- 'neither' : {'action' : 'store_true' , 'help' : 'neither (client/server) is in specified country' },
63- 'both' : {'action' : 'store_true' , 'help' : 'both (client/server) ARE in specified country' },
64- 'notboth' : {'action' : 'store_true' , 'help' : 'specified country is not both client and server' },
65- 'alerts' : {'action' : 'store_true' }})
66- '''instantiate an decoder that will call back to us once the IP decoding is done'''
57+ filter = "ip or ip6" ,
58+ author = 'twp' ,
59+ optiondict = {
60+ 'code' : {'type' : 'string' , 'help' : 'two-char country code' },
61+ 'neither' : {'action' : 'store_true' , 'help' : 'neither (client/server) is in specified country' },
62+ 'both' : {'action' : 'store_true' , 'help' : 'both (client/server) ARE in specified country' },
63+ 'notboth' : {'action' : 'store_true' , 'help' : 'specified country is not both client and server' },
64+ 'alerts' : {'action' : 'store_true' }
65+ }
66+ )
67+ # instantiate a decoder that will call back to us once the IP decoding is done
6768 self .__decoder = dshell .IPDecoder ()
6869 self .out = netflowout .NetflowOutput ()
6970 self .chainable = True
7071
7172 def decode (self , * args ):
7273 if len (args ) is 3 :
73- pktlen , pktdata , ts = args # orig_len,packet,ts format (pylibpcap)
74- else : # ts,pktdata (pypcap)
74+ pktlen , pktdata , ts = args
75+ else :
7576 ts , pktdata = args
7677 pktlen = len (pktdata )
77- ''' do normal decoder stack to track session '''
78+ # do normal decoder stack to track session
7879 dshell .TCPDecoder .decode (self , pktlen , pktdata , ts )
79- ''' our hook to decode the ip/ip6 addrs, then dump the addrs and raw packet to our callback'''
80+ # our hook to decode the ip/ip6 addrs, then dump the addrs and raw packet to our callback
8081 self .__decoder .IPHandler = self .__callback # set private decoder to our callback
8182 self .__decoder .decode (pktlen , pktdata , ts , raw = pktdata )
8283
@@ -97,18 +98,18 @@ def connectionInitHandler(self, conn):
9798
9899 def __countryTest (self , conn ):
99100 # If no country code specified, pass all traffic through
100- if self . code == None or not len ( self .code ) :
101+ if not self .code :
101102 return True
102103 # check criteria
103104 if self .neither and conn .clientcountrycode != self .code and conn .servercountrycode != self .code :
104105 return 'neither ' + self .code
105106 if self .both and conn .clientcountrycode == self .code and conn .servercountrycode == self .code :
106107 return 'both ' + self .code
107- if self .notboth and (conn .clientcountrycode != self .code or conn .servercountrycode != self .code ):
108+ if self .notboth and (( conn .clientcountrycode == self .code ) ^ ( conn .servercountrycode == self .code ) ):
108109 return 'not both ' + self .code
109- if conn .clientcountrycode == self .code :
110+ if not self . both and conn .clientcountrycode == self .code :
110111 return 'client ' + self .code
111- if conn .servercountrycode == self .code :
112+ if not self . both and conn .servercountrycode == self .code :
112113 return 'server ' + self .code
113114 # no match
114115 return None
0 commit comments