Skip to content

Commit 7b847cd

Browse files
authored
Merge pull request #1207 from stratosphereips/alya/fix-analyzing_flows_when_-w_is_given
Fix clearing zeek files from the database when the web interface is started using -w
2 parents 4e2ca11 + afc7837 commit 7b847cd

File tree

8 files changed

+35
-23
lines changed

8 files changed

+35
-23
lines changed

managers/process_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def start_output_process(self, stderr, slips_logfile, stdout=""):
8686
verbose=self.main.args.verbose or 0,
8787
debug=self.main.args.debug,
8888
input_type=self.main.input_type,
89-
stop_daemon=self.main.args.stopdaemon,
89+
create_logfiles=False if self.main.args.stopdaemon else True,
9090
)
9191
self.slips_logfile = output_process.slips_logfile
9292
return output_process

managers/ui_manager.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ def check_if_webinterface_started(self):
2424
# to make sure this function is only executed once
2525
delattr(self, "webinterface_return_value")
2626
return
27+
2728
if not self.webinterface_return_value.get():
2829
# to make sure this function is only executed once
2930
delattr(self, "webinterface_return_value")
3031
return
3132

3233
self.main.print(
3334
f"Slips {green('web interface')} running on "
34-
f"http://localhost:{self.web_interface_port}/\n"
35+
f"http://localhost:{self.web_interface_port}/ "
36+
f"[PID {green(self.web_interface_pid)}]\n"
3537
f"The port will stay open after slips is done with the "
3638
f"analysis unless you manually kill it.\n"
3739
f"You need to kill it to be able to start the web interface "
@@ -70,6 +72,7 @@ def run_webinterface():
7072
)
7173

7274
self.main.db.store_pid("Web Interface", webinterface.pid)
75+
self.web_interface_pid = webinterface.pid
7376
# we'll assume that it started, and if not, the return value will
7477
# immediately change and this thread will
7578
# print an error

slips_files/core/database/database_manager.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,11 @@ def __init__(
4545
# the existing one
4646
self.sqlite = None
4747
if start_sqlite:
48-
self.sqlite = self.create_sqlite_db(output_dir)
48+
self.sqlite = SQLiteDB(self.logger, output_dir)
4949

5050
def print(self, *args, **kwargs):
5151
return self.printer.print(*args, **kwargs)
5252

53-
def create_sqlite_db(self, output_dir):
54-
return SQLiteDB(self.logger, output_dir)
55-
5653
@classmethod
5754
def read_configuration(cls):
5855
conf = ConfigParser()

slips_files/core/database/redis_db/database.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,17 @@ def _set_redis_options(cls):
154154
Updates the default slips options based on the -s param,
155155
writes the new configs to cls._conf_file
156156
"""
157+
# to fix redis.exceptions.ResponseError MISCONF Redis is
158+
# configured to save RDB snapshots
159+
# configure redis to stop writing to dump.rdb when an error
160+
# occurs without throwing errors in slips
157161
cls._options = {
158162
"daemonize": "yes",
159163
"stop-writes-on-bgsave-error": "no",
160164
"save": '""',
161165
"appendonly": "no",
162166
}
163-
167+
# -s for saving the db
164168
if "-s" not in sys.argv:
165169
return
166170

@@ -188,6 +192,8 @@ def _set_redis_options(cls):
188192
@classmethod
189193
def _read_configuration(cls):
190194
conf = ConfigParser()
195+
# Should we delete the previously stored data in the DB when we start?
196+
# By default False. Meaning we don't DELETE the DB by default.
191197
cls.deletePrevdb: bool = conf.delete_prev_db()
192198
cls.disabled_detections: List[str] = conf.disabled_detections()
193199
cls.width = conf.get_tw_width_as_float()
@@ -227,6 +233,11 @@ def init_redis_server(cls) -> Tuple[bool, str]:
227233
if not connected:
228234
return False, err
229235

236+
# these are the cases that we DO NOT flush the db when we
237+
# connect to it, because we need to use it
238+
# -d means Read an analysed file (rdb) from disk.
239+
# -S stop daemon
240+
# -cb clears the blocking chain
230241
if (
231242
cls.deletePrevdb
232243
and not (
@@ -237,6 +248,7 @@ def init_redis_server(cls) -> Tuple[bool, str]:
237248
# when stopping the daemon, don't flush bc we need to get
238249
# the PIDS to close slips files
239250
cls.r.flushdb()
251+
cls.r.delete(cls.constants.ZEEK_FILES)
240252

241253
# Set the memory limits of the output buffer,
242254
# For normal clients: no limits
@@ -245,12 +257,6 @@ def init_redis_server(cls) -> Tuple[bool, str]:
245257
cls.change_redis_limits(cls.r)
246258
cls.change_redis_limits(cls.rcache)
247259

248-
# to fix redis.exceptions.ResponseError MISCONF Redis is
249-
# configured to save RDB snapshots
250-
# configure redis to stop writing to dump.rdb when an error
251-
# occurs without throwing errors in slips
252-
# Even if the DB is not deleted. We need to delete some temp data
253-
cls.r.delete(cls.constants.ZEEK_FILES)
254260
return True, ""
255261
except RuntimeError as err:
256262
return False, str(err)

slips_files/core/database/sqlite_db/database.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ def __init__(self, logger: Output, output_dir: str):
3434

3535
def connect(self):
3636
"""
37-
Creates the db if it doesn't exist and connects to it
37+
Creates the db if it doesn't exist and connects to it.
38+
OR connects to the existing db if it's there.
3839
"""
3940
db_newly_created = False
4041
if not os.path.exists(self._flows_db):

slips_files/core/input.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ def get_earliest_line(self):
333333
"""
334334
# Now read lines in order. The line with the earliest timestamp first
335335
files_sorted_by_ts = sorted(self.file_time, key=self.file_time.get)
336+
336337
try:
337338
# get the file that has the earliest flow
338339
file_with_earliest_flow = files_sorted_by_ts[0]
@@ -344,7 +345,8 @@ def get_earliest_line(self):
344345
self.zeek_files = self.db.get_all_zeek_files()
345346
return False, False
346347

347-
# comes here if we're done with all conn.log flows and it's time to process other files
348+
# comes here if we're done with all conn.log flows and it's time to
349+
# process other files
348350
earliest_line = self.cache_lines[file_with_earliest_flow]
349351
return earliest_line, file_with_earliest_flow
350352

@@ -386,6 +388,7 @@ def read_zeek_files(self) -> int:
386388
# Delete this line from the cache and the time list
387389
del self.cache_lines[file_with_earliest_flow]
388390
del self.file_time[file_with_earliest_flow]
391+
389392
# Get the new list of files. Since new files may have been created by
390393
# Zeek while we were processing them.
391394
self.zeek_files = self.db.get_all_zeek_files()
@@ -432,7 +435,8 @@ def read_zeek_folder(self):
432435
self.bro_timeout = 10
433436
growing_zeek_dir: bool = self.db.is_growing_zeek_dir()
434437
if growing_zeek_dir:
435-
# slips is given a dir that is growing i.e zeek dir running on an interface
438+
# slips is given a dir that is growing i.e zeek dir running on an
439+
# interface
436440
# don't stop zeek or slips
437441
self.bro_timeout = float("inf")
438442

slips_files/core/output.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def __init__(
4646
stderr="output/errors.log",
4747
slips_logfile="output/slips.log",
4848
input_type=False,
49-
stop_daemon: bool = None,
49+
create_logfiles: bool = True,
5050
stdout="",
5151
):
5252
super().__init__()
@@ -56,15 +56,17 @@ def __init__(
5656
self.debug = debug
5757
self.stdout = stdout
5858
self.input_type = input_type
59-
self.stop_daemon = stop_daemon
6059
self.errors_logfile = stderr
6160
self.slips_logfile = slips_logfile
62-
# if we're using -S, no need to init all the logfiles
61+
62+
if self.verbose > 2:
63+
print(f"Verbosity: {self.verbose}. Debugging: {self.debug}")
64+
65+
# when we're using -S, no need to init all the logfiles
6366
# we just need an instance of this class to be able
6467
# to start the db from the daemon class
65-
if not stop_daemon:
68+
if create_logfiles:
6669
self._read_configuration()
67-
6870
self.create_logfile(self.errors_logfile)
6971
self.log_branch_info(self.errors_logfile)
7072
self.create_logfile(self.slips_logfile)
@@ -76,8 +78,6 @@ def __init__(
7678
utils.change_logfiles_ownership(
7779
self.slips_logfile, self.UID, self.GID
7880
)
79-
if self.verbose > 2:
80-
print(f"Verbosity: {self.verbose}. Debugging: {self.debug}")
8181

8282
def _read_configuration(self):
8383
conf = ConfigParser()

webinterface/database/database.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def get_db_manager_obj(self, port: int = False) -> Optional[DBManager]:
4747
stdout=os.path.join(output_dir, "slips.log"),
4848
stderr=os.path.join(output_dir, "errors.log"),
4949
slips_logfile=os.path.join(output_dir, "slips.log"),
50+
create_logfiles=False,
5051
)
5152
try:
5253
return DBManager(

0 commit comments

Comments
 (0)