Skip to content

Commit 5d3d48e

Browse files
authored
Merge pull request #114 from dev195/master
Bug fixes to Makefile and country filter
2 parents 34a5a7a + 6f15c39 commit 5d3d48e

2 files changed

Lines changed: 29 additions & 26 deletions

File tree

Makefile

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ all: rc dshell
55
dshell: rc initpy pydoc
66

77
rc:
8-
# Generating .dshellrc and dshell files
8+
# Generating .dshellrc and dshell files
99
python $(PWD)/bin/generate-dshellrc.py $(PWD)
1010
chmod 755 $(PWD)/dshell
1111
chmod 755 $(PWD)/dshell-decode
@@ -16,17 +16,19 @@ initpy:
1616
find $(PWD)/decoders -type d -not -path \*.svn\* -print -exec touch {}/__init__.py \;
1717

1818
pydoc:
19-
(cd $(PWD)/doc && ./generate-doc.sh $(PWD) )
19+
(cd $(PWD)/doc && ./generate-doc.sh $(PWD) )
2020

21-
clean: clean_pyc
21+
clean: clean_pyc clean_ln
2222

2323
distclean: clean clean_py clean_pydoc clean_rc
24-
24+
2525
clean_rc:
2626
rm -fv $(PWD)/dshell
2727
rm -fv $(PWD)/dshell-decode
2828
rm -fv $(PWD)/.dshellrc
29-
rm -fv $(PWD)/bin/decode
29+
30+
clean_ln:
31+
rm -fv $(PWD)/bin/decode
3032

3133
clean_py:
3234
find $(PWD)/decoders -name '__init__.py' -exec rm -v {} \;

decoders/filter/country.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
'''
44

55
import dshell
6-
import util
76
import 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="""
2524
country: filter connections on geolocation (country code)
2625
2726
Chainable 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

Comments
 (0)