Skip to content

Commit 6f059e0

Browse files
committed
Changed the is_msg_intended_for
1 parent 32a066f commit 6f059e0

25 files changed

+470
-417
lines changed

conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pytest
66
import os, sys, inspect
77
from multiprocessing import Queue
8-
8+
import uuid
99

1010
# add parent dir to path for imports to work
1111
current_dir = os.path.dirname(
@@ -47,7 +47,7 @@ def profilerQueue():
4747
@pytest.fixture
4848
def database(outputQueue):
4949
from slips_files.core.database.database import __database__
50-
__database__.start(1234)
50+
__database__.start(str(uuid.uuid4()))
5151
__database__.outputqueue = outputQueue
5252
__database__.print = do_nothing
5353
return __database__

process_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def shutdown_gracefully(self):
293293
if message and message['data'] in ('stop_process', 'stop_slips'):
294294
continue
295295

296-
if utils.is_msg_intended_for(message, 'finished_modules'):
296+
if __database__.is_msg_intended_for(message, 'finished_modules'):
297297
# all modules must reply with their names in this channel after
298298
# receiving the stop_process msg
299299
# to confirm that all processing is done and we can safely exit now

redis_manager.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,28 @@ def close_all_instance(self):
108108
unique_ports = set()
109109
# close all ports in logfile
110110
for _id in self.open_servers_IDs:
111-
self.flush_redis_id(_id=_id, port = self.open_servers_IDs[_id])
111+
# self.flush_redis_id(_id=_id, port = self.open_servers_IDs[_id])
112112
unique_ports.add(self.open_servers_IDs[_id])
113113

114114
# Kill default port
115-
115+
116116
for port in unique_ports:
117117
# Skip cache db which is in port 6379
118+
r = redis.StrictRedis(
119+
host='localhost',
120+
port=self.open_servers_IDs[_id],
121+
db=0,
122+
charset='utf-8',
123+
socket_keepalive=True,
124+
decode_responses=True,
125+
retry_on_timeout=True,
126+
health_check_interval=20,
127+
)
128+
r.flushdb()
129+
118130
if str(port) == '6379':
119131
continue
132+
120133
pid = self.get_pid_of_redis_server(port = port)
121134
self.kill_redis_server(pid)
122135

slips.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ def sig_handler(sig, frame):
744744
message = self.c1.get_message(timeout=0.01)
745745
if (
746746
message
747-
and utils.is_msg_intended_for(message, 'finished_modules')
747+
and __database__.is_msg_intended_for(message, 'finished_modules')
748748
and message['data'] == 'stop_slips'
749749
):
750750
self.proc_man.shutdown_gracefully()

slips_files/common/slips_utils.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -343,19 +343,6 @@ def get_hash_from_file(self, filename):
343343
fb = f.read(BLOCK_SIZE)
344344
return file_hash.hexdigest()
345345

346-
def is_msg_intended_for(self, message, channel):
347-
"""
348-
Function to check
349-
1. If the given message is intended for this channel
350-
2. The msg has valid data
351-
"""
352-
353-
return (
354-
message
355-
and type(message['data']) == str
356-
and message['data'] != 'stop_process'
357-
and message['channel'] == channel
358-
)
359346

360347
def get_branch_info(self):
361348
"""

slips_files/core/database/_profile_flow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,7 @@ def get_dns_resolution(self, ip):
13501350
If not resolved, returns {}
13511351
this function is called for every IP in the timeline of kalipso
13521352
"""
1353-
if ip_info := self.r.hget('DNSresolution', ip):
1353+
if ip_info := self.r.hget(self.prefix + self.separator + 'DNSresolution', ip):
13541354
ip_info = json.loads(ip_info)
13551355
# return a dict with 'ts' 'uid' 'domains' about this IP
13561356
return ip_info

slips_files/core/database/database.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,14 +358,14 @@ def addProfile(self, profileid, starttime, duration):
358358
"""
359359
try:
360360
# make sure we don't add public ips if the user specified a home_network
361-
if self.r.sismember(self.prefix + self.separator + 'profiles', str(profileid)):
361+
if self.r.sismember(self.prefix + self.separator + 'profiles', self.prefix + self.separator + str(profileid)):
362362
# we already have this profile
363363
return False
364364
# execlude ips outside of local network is it's set in slips.conf
365365
if not self.should_add(profileid):
366366
return False
367367
# Add the profile to the index. The index is called prefix + separator + 'profiles'
368-
self.r.sadd(self.prefix + self.separator + 'profiles', str(profileid))
368+
self.r.sadd(self.prefix + self.separator + 'profiles', self.prefix + self.separator + str(profileid))
369369
# Create the hashmap with the profileid. The hasmap of each profile is named with the profileid
370370
# Add the start time of profile
371371
self.r.hset(self.prefix + self.separator + str(profileid), 'starttime', starttime)
@@ -725,7 +725,7 @@ def getProfileIdFromIP(self, daddr_as_obj):
725725
"""Receive an IP and we want the profileid"""
726726
try:
727727
profileid = f'profile{self.separator}{str(daddr_as_obj)}'
728-
if data := self.r.sismember('profiles', profileid):
728+
if data := self.r.sismember(self.prefix + self.separator + 'profiles', self.prefix + self.separator + str(profileid)):
729729
return profileid
730730
return False
731731
except redis.exceptions.ResponseError as inst:
@@ -799,7 +799,7 @@ def getT2ForProfileTW(self, profileid, twid, tupleid, tuple_key: str):
799799

800800
def has_profile(self, profileid):
801801
"""Check if we have the given profile"""
802-
return self.r.sismember(self.prefix + self.separator + 'profiles', profileid) if profileid else False
802+
return self.r.sismember(self.prefix + self.separator + 'profiles', self.prefix + self.separator + str(profileid)) if profileid else False
803803

804804
def getProfilesLen(self):
805805
"""Return the amount of profiles. Redis should be faster than python to do this count"""
@@ -2607,4 +2607,17 @@ def store_std_file(self, **kwargs):
26072607
def get_stdfile(self, file_type):
26082608
return self.r.get(self.prefix + self.separator + str(file_type))
26092609

2610+
def is_msg_intended_for(self, message, channel):
2611+
"""
2612+
Function to check
2613+
1. If the given message is intended for this channel
2614+
2. The msg has valid data
2615+
"""
2616+
2617+
return (
2618+
message
2619+
and type(message['data']) == str
2620+
and message['data'] != 'stop_process'
2621+
and message['channel'] == self.prefix + self.separator + str(channel)
2622+
)
26102623
__database__ = Database()

slips_files/core/evidenceProcess.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ def run(self):
576576
while True:
577577
try:
578578
message = __database__.get_message(self.c1)
579-
if utils.is_msg_intended_for(message, 'evidence_added'):
579+
if __database__.is_msg_intended_for(message, 'evidence_added'):
580580
# Data sent in the channel as a json dict, it needs to be deserialized first
581581
data = json.loads(message['data'])
582582
profileid = data.get('profileid')
@@ -742,7 +742,7 @@ def run(self):
742742
)
743743

744744
message = __database__.get_message(self.c2)
745-
if utils.is_msg_intended_for(message, 'new_blame'):
745+
if __database__.is_msg_intended_for(message, 'new_blame'):
746746
data = message['data']
747747
try:
748748
data = json.loads(data)

slips_files/core/inputProcess.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ def remove_old_zeek_files(self):
711711
msg = __database__.get_message(self.c1)
712712
if msg and msg['data'] == 'stop_process':
713713
return True
714-
if utils.is_msg_intended_for(msg, 'remove_old_files'):
714+
if __database__.is_msg_intended_for(msg, 'remove_old_files'):
715715
# this channel receives renamed zeek log files, we can safely delete them and close their handle
716716
changed_files = json.loads(msg['data'])
717717

slips_files/core/profilerProcess.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2461,7 +2461,7 @@ def run(self):
24612461
if message and message['data'] == 'stop_process':
24622462
self.shutdown_gracefully()
24632463
return True
2464-
if utils.is_msg_intended_for(message, 'reload_whitelist'):
2464+
if __database__.is_msg_intended_for(message, 'reload_whitelist'):
24652465
# if whitelist.conf is edited using pycharm
24662466
# a msg will be sent to this channel on every keypress, because pycharm saves file automatically
24672467
# otherwise this channel will get a msg only when whitelist.conf is modified and saved to disk

tests/integration_tests/test_config_files.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,18 @@ def check_for_text(txt, output_dir):
7878
return False
7979

8080
@pytest.mark.parametrize(
81-
'pcap_path, expected_profiles, output_dir, redis_port',
81+
'pcap_path, expected_profiles, output_dir, prefix',
8282
[
8383
(
8484
'dataset/test7-malicious.pcap',
8585
290,
8686
'test_configuration_file/',
87-
6667,
87+
'2f168df6-c2a9-4a0a-935a-b04fe92e43b7',
8888
)
8989
],
9090
)
9191
def test_conf_file(
92-
pcap_path, expected_profiles, output_dir, redis_port
92+
pcap_path, expected_profiles, output_dir, prefix
9393
):
9494
"""
9595
In this test we're using tests/test.conf
@@ -101,14 +101,16 @@ def test_conf_file(
101101
f'-f {pcap_path} ' \
102102
f'-o {output_dir} ' \
103103
f'-c tests/integration_tests/test.conf ' \
104-
f'-P {redis_port} ' \
104+
f'-uid {prefix} ' \
105105
f'> {output_file} 2>&1'
106106
# this function returns when slips is done
107107
os.system(command)
108108

109109
assert has_errors(output_dir) is False
110110

111-
database = connect_to_redis(redis_port)
111+
database = connect_to_redis(6379)
112+
database.setPrefix(prefix)
113+
112114
profiles = int(database.getProfilesLen())
113115
# expected_profiles is more than 50 because we're using direction = all
114116
assert profiles > expected_profiles
@@ -146,18 +148,18 @@ def test_conf_file(
146148

147149

148150
@pytest.mark.parametrize(
149-
'pcap_path, expected_profiles, output_dir, redis_port',
151+
'pcap_path, expected_profiles, output_dir, prefix',
150152
[
151153
(
152154
'dataset/test8-malicious.pcap',
153155
1,
154156
'pcap_test_conf2/',
155-
6668,
157+
'5eade174-9e34-431b-86c7-4569e55a723d',
156158
)
157159
],
158160
)
159161
def test_conf_file2(
160-
pcap_path, expected_profiles, output_dir, redis_port
162+
pcap_path, expected_profiles, output_dir, prefix
161163
):
162164
"""
163165
In this test we're using tests/test2.conf
@@ -170,15 +172,15 @@ def test_conf_file2(
170172
f'-f {pcap_path} ' \
171173
f'-o {output_dir} ' \
172174
f'-c tests/integration_tests/test2.conf ' \
173-
f'-P {redis_port} ' \
175+
f'-uid {prefix} ' \
174176
f'> {output_file} 2>&1'
175177
# this function returns when slips is done
176178
os.system(command)
177179

178180
assert has_errors(output_dir) is False
179181

180-
database = connect_to_redis(redis_port)
181-
182+
database = connect_to_redis(6379)
183+
database.setPrefix(prefix)
182184
# test 1 homenet ip
183185
# the only profile we should have is the one in home_network parameter
184186
profiles = int(database.getProfilesLen())

0 commit comments

Comments
 (0)