Skip to content

Commit

Permalink
Fix/297 (#302)
Browse files Browse the repository at this point in the history
Fixed some Codacy-related bugs. Additionally, this should fix issue #297
  • Loading branch information
lilchurro authored Feb 22, 2019
1 parent ba36728 commit 4c47b96
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 22 deletions.
1 change: 0 additions & 1 deletion DeviceClassifier/OneLayer/train_OneLayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
('models/OneLayerModel' by default).
'''
import argparse
import sys

from poseidonml.config import get_config
from poseidonml.Model import Model
Expand Down
1 change: 0 additions & 1 deletion DeviceClassifier/RandomForest/train_RandomForest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
('models/RandomForestModel' by default).
'''
import argparse
import sys

from poseidonml.config import get_config
from poseidonml.Model import Model
Expand Down
8 changes: 4 additions & 4 deletions utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def update_data(
update_list = ast.literal_eval(
updates[b'timestamps'].decode('ascii'))
self.logger.debug('Got previous updates from %s', source_mac)
except Exception as e:
except:
self.logger.debug('No previous updates found for %s', source_mac)
update_list = []

Expand All @@ -288,8 +288,8 @@ def update_data(
try:
self.r.hmset(source_mac, redis_times)
self.r.sadd('mac_addresses', source_mac)
except Exception as e:
self.logger.debug('Could not store update time')
except (ConnectionError, TimeoutError) as e:
self.logger.debug('Could not store update time because: %s', str(e))

return key

Expand Down Expand Up @@ -351,5 +351,5 @@ def get_config(self):
#self.batch_size = config['batch size']
except Exception as e: # pragma: no cover
self.logger.error(
"unable to read 'opts/config.json' properly because: %s", str(e))
"Unable to read 'opts/config.json' properly because: %s", str(e))
return
10 changes: 3 additions & 7 deletions utils/featurizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,9 @@ def extract_features(session_dict, capture_source=None, max_port=None):
'''

# Get featurization info from config
try:
config = get_config()
address_type = config['source identifier']
if max_port is None:
max_port = config['max port']
except Exception as e:
address_type = 'MAC'
config = get_config()
address_type = config.get('source identifier', 'MAC')
max_port = config.get('max port', max_port) if not max_port else None

# If the capture source isn't specified, default to the most used address
if capture_source is None:
Expand Down
10 changes: 6 additions & 4 deletions utils/pcap_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,8 @@ def packet_size(packet):
size = packet[1][32:36]
try:
size = int(size, 16)
except Exception as e:
except ValueError:
size = 0

return size


Expand Down Expand Up @@ -300,8 +299,11 @@ def clean_session_dict(sessions, source_address=None):
def clean_dict(sessions, source_address):
cleaned_sessions = OrderedDict()
for key, packets in sessions.items():
address_1, port_1 = get_ip_port(key[0])
address_2, port_2 = get_ip_port(key[1])
# TODO: Removing port_1 and port_2 (i.e., returned val [1])
# due to unuse, but I'm a little surprised we aren't using
# this... O_o
address_1 = get_ip_port(key[0])[0]
address_2 = get_ip_port(key[1])[0]

first_packet = sessions[key][0][1]
source_mac, destination_mac = extract_macs(first_packet)
Expand Down
10 changes: 5 additions & 5 deletions utils/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def packetizer(path):

# Read get the pcap info with tcpdump
FNULL = open(os.devnull, 'w')
# TODO: yikes @ the shell=True + unvalidated user input
proc = subprocess.Popen(
'tcpdump -nn -tttt -xx -r' + path,
shell=True,
Expand Down Expand Up @@ -166,11 +167,10 @@ def sessionizer(path, duration=None, threshold_time=None):

# Get threshold time from config
if threshold_time is None:
try:
config = get_config()
threshold_time = config['session threshold']
except Exception as e:
threshold_time = 120
# TODO: error-check threshold_time, if < 0
# error-check all config parameters, as well.
config = get_config()
threshold_time = config.get('session threshold', 120)

for head, packet in packet_dict.items():
time = head[0]
Expand Down

0 comments on commit 4c47b96

Please sign in to comment.