Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory profile #388

Merged
merged 85 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
87c52b6
Merge pull request #375 from stratosphereips/develop
AlyaGomaa Jun 30, 2023
1b5d4a8
Update README.md
verovaleros Jul 11, 2023
5408718
Squashed commit of the following:
danieltherealyang Jul 13, 2023
77451b2
done with dev mode
danieltherealyang Jul 13, 2023
38eb0e2
singleprocess done, need to add autospawned TUI so doesn't block process
danieltherealyang Jul 20, 2023
352a2e9
prototype multiprocess profiling structure (untested)
danieltherealyang Jul 24, 2023
2f0ae56
changed Dict Type
danieltherealyang Jul 24, 2023
1ab81ac
stdout to pipe file fixed
danieltherealyang Jul 25, 2023
36263ac
non blocking single process
danieltherealyang Jul 20, 2023
701e422
signal handling and global pid->process mapping
danieltherealyang Jul 25, 2023
1b8b6e2
proc mapping finished
danieltherealyang Jul 26, 2023
f968977
moved mutex locking to start and end tracker
danieltherealyang Jul 26, 2023
3a0de45
communication works, need to find out why tracer isn't starting on si…
danieltherealyang Jul 26, 2023
e37be2c
memory profiler done
danieltherealyang Jul 27, 2023
1e8e976
Added notes to memory_profiler_example
danieltherealyang Jul 27, 2023
7d2fe86
testing print statements
danieltherealyang Aug 2, 2023
eda609c
removed custom join and terminate
danieltherealyang Aug 2, 2023
0429dad
wrote documentation
danieltherealyang Aug 2, 2023
80adab2
Added notes on multiprocess live profiling
danieltherealyang Aug 12, 2023
401945f
memory profiler works correctly with multiprocessing but modules gett…
danieltherealyang Aug 13, 2023
653c398
Summary: fixed inheritance issues preventing modules from getting pro…
danieltherealyang Aug 17, 2023
4cd6468
commented out multiproc_test in slips.py, text from multiprocessPatch…
danieltherealyang Aug 18, 2023
e76bd3d
made changes
danieltherealyang Aug 18, 2023
b29cfbd
removed print
danieltherealyang Aug 18, 2023
15fa16c
made small change
danieltherealyang Aug 22, 2023
e5c3c6d
add logic to calc aid
AlyaGomaa Aug 25, 2023
d696fd6
get aid instead of cid in sqlite db
AlyaGomaa Aug 25, 2023
eb944f5
Merge pull request #384 from stratosphereips/verovaleros-patch-README
AlyaGomaa Aug 28, 2023
e9664aa
Merge pull request #390 from stratosphereips/alya/calc-aid
AlyaGomaa Aug 28, 2023
05e7754
use transactions and try max 2 times to re-execute failed queries bef…
AlyaGomaa Aug 29, 2023
857736e
Merge pull request #391 from stratosphereips/alya-fix-malformed-sqlite
AlyaGomaa Aug 29, 2023
31167a5
evidence: pass all alert details to sqlite
AlyaGomaa Aug 30, 2023
2db32a7
sqlite: store all alerts details in the alerts table
AlyaGomaa Aug 30, 2023
9eb6bbe
sqlite: store twid start and end date in the alerts table
AlyaGomaa Aug 30, 2023
c5fc002
sqlite: init the trial var to keep track of failed sql queries [skip ci]
AlyaGomaa Aug 30, 2023
26ec1eb
sqlite: print sqlite db errors to errors.log
AlyaGomaa Aug 30, 2023
d74f07a
sqlite: store alert time in unix format in alerts table
AlyaGomaa Aug 30, 2023
7dd87ef
Merge pull request #392 from stratosphereips/alya-add-sqlite-alerts-t…
AlyaGomaa Aug 30, 2023
f36318c
fix unable to get daddr from conn.log flow in flowalerts (quick-fix) …
AlyaGomaa Aug 31, 2023
b3b68c4
sqlite: make it possible to have multiple instances of the sqlite cl…
AlyaGomaa Sep 4, 2023
8038295
start a new instance of sqlite for each module in slips
AlyaGomaa Sep 4, 2023
f207f65
start a new instance of sqlite for each Core file in slips
AlyaGomaa Sep 4, 2023
1c9045b
sqlite: create the db if it doesn't exist, or connect to it if it does
AlyaGomaa Sep 4, 2023
16986a1
make sure the ts used in calculating aid has microseconds
AlyaGomaa Sep 5, 2023
3f5526f
Merge pull request #394 from stratosphereips/alya/assert_microseconds…
AlyaGomaa Sep 5, 2023
cdfb448
change how we instantiate dbmanager to be able to mock the sqlite db
AlyaGomaa Sep 6, 2023
99fdeb3
change the creation of all modules in the unit tests to mock sqlite a…
AlyaGomaa Sep 6, 2023
10d634b
use mock_rdb instead of generic moc_db in all unit tests
AlyaGomaa Sep 6, 2023
0f8e88d
don't connect manually to redis in the test_add_flow_to_profile test
AlyaGomaa Sep 6, 2023
b44b585
Merge pull request #393 from stratosphereips/alya/create_many_sqlite_…
AlyaGomaa Sep 6, 2023
e34a186
don't treat dbmanager as a singelton
AlyaGomaa Sep 7, 2023
af07694
close the db in the destructor of each class and core file
AlyaGomaa Sep 7, 2023
791638d
make sure the aid stored in the flows.sqlite db is string not bytes
AlyaGomaa Sep 8, 2023
ce9bf55
Merge pull request #395 from stratosphereips/alya/fix_sharing_sqlite_…
AlyaGomaa Sep 11, 2023
57f1386
use a lock even when creating tables
AlyaGomaa Sep 12, 2023
6a42ee3
wait 5 seconds before next trial when db is locked
AlyaGomaa Sep 12, 2023
2d5a138
commit even on SELECT
AlyaGomaa Sep 12, 2023
d1a9132
db: wait for the db to beunlocked 20s instead of the default 5s
AlyaGomaa Sep 12, 2023
d963514
Merge pull request #396 from stratosphereips/alya/use_lock_even_when_…
AlyaGomaa Sep 13, 2023
4da65a9
Squashed commit of the following:
danieltherealyang Jul 13, 2023
230ccd9
done with dev mode
danieltherealyang Jul 13, 2023
d4a485c
singleprocess done, need to add autospawned TUI so doesn't block process
danieltherealyang Jul 20, 2023
4727834
prototype multiprocess profiling structure (untested)
danieltherealyang Jul 24, 2023
eb59aa6
changed Dict Type
danieltherealyang Jul 24, 2023
3eba22f
stdout to pipe file fixed
danieltherealyang Jul 25, 2023
fb96463
non blocking single process
danieltherealyang Jul 20, 2023
af5d04d
signal handling and global pid->process mapping
danieltherealyang Jul 25, 2023
8b47542
proc mapping finished
danieltherealyang Jul 26, 2023
b3ade23
moved mutex locking to start and end tracker
danieltherealyang Jul 26, 2023
dd4cd25
communication works, need to find out why tracer isn't starting on si…
danieltherealyang Jul 26, 2023
8e423cd
memory profiler done
danieltherealyang Jul 27, 2023
6dcb63e
Added notes to memory_profiler_example
danieltherealyang Jul 27, 2023
3140a1d
testing print statements
danieltherealyang Aug 2, 2023
4995eed
removed custom join and terminate
danieltherealyang Aug 2, 2023
b9da555
wrote documentation
danieltherealyang Aug 2, 2023
13435b5
Added notes on multiprocess live profiling
danieltherealyang Aug 12, 2023
ec0ba70
memory profiler works correctly with multiprocessing but modules gett…
danieltherealyang Aug 13, 2023
723acc6
Summary: fixed inheritance issues preventing modules from getting pro…
danieltherealyang Aug 17, 2023
42303cb
commented out multiproc_test in slips.py, text from multiprocessPatch…
danieltherealyang Aug 18, 2023
9ce6349
made changes
danieltherealyang Aug 18, 2023
3c339b5
removed print
danieltherealyang Aug 18, 2023
1ff3aa7
made small change
danieltherealyang Aug 22, 2023
2bb153c
Merge remote-tracking branch 'danieltherealyang/memory-profile' into …
AlyaGomaa Sep 15, 2023
947be4f
add docs for memory and CPU profiler [skip ci]
AlyaGomaa Sep 15, 2023
cd73bc7
disable memory profiler by default
AlyaGomaa Sep 15, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Slips v1.0.6
# Slips: Behavioral Machine Learning-Based Intrusion Prevention System


Slips is a behavioral intrusion prevention system that uses machine learning to detect malicious behaviors in network traffic. Slips focus on targeted attacks, detection of command and control channels, and providing a good visualization for the analyst. It can analyze network traffic in real-time, network captures such as pcap files, and network flows produced by Suricata, Zeek/Bro, and Argus. Slips processes the input data, analyzes it, and highlights suspicious behavior that needs the analyst's attention.
Slips is a powerful endpoint behavioral intrusion prevention and detection system that uses machine learning to detect malicious behaviors in network traffic. Slips can work with network traffic in real-time, pcap files, and network flows from popular tools like Suricata, Zeek/Bro, and Argus. Slips threat detection is based on a combination of machine learning models trained to detect malicious behaviors, 40+ threat intelligence feeds and expert heuristics. Slips gathers evidence of malicious behavior and uses extensively trained thresholds to trigger alerts when enough evidence is accumulated.

<img src="https://raw.githubusercontent.com/stratosphereips/StratosphereLinuxIPS/develop/docs/images/slips.gif" width="850px"
title="Slips in action.">
Expand Down
13 changes: 12 additions & 1 deletion config/slips.conf
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,19 @@ cpu_profiler_output_limit = 20
# set the wait time between sampling sequences in seconds (live mode only)
cpu_profiler_sampling_interval = 20

# [12] Memory Profiling

# enable memory profiling [yes,no]
memory_profiler_enable = no

# set profiling mode [dev,live]
memory_profiler_mode = live

# profile all subprocesses [yes,no]
memory_profiler_multiprocess = yes

####################
# [12] enable or disable p2p for slips
# [13] enable or disable p2p for slips
[P2P]

# create p2p.log with additional info about peer communications? yes or no
Expand Down
2 changes: 1 addition & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


@pytest.fixture
def mock_db():
def mock_rdb():
# Create a mock version of the database object
with patch('slips_files.core.database.database_manager.DBManager') as mock:
yield mock.return_value
Expand Down
11 changes: 11 additions & 0 deletions docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -1045,8 +1045,19 @@ Options to enable cpu profiling can be found under the [Profiling] section of th
```cpu_profiler_output_limit``` is set to an integer value and only affects the live mode profiling. This option sets the limit on the number of processes output for live mode profiling updates.
```cpu_profiler_sampling_interval``` is set to an integer value and only affects the live mode profiling. This option sets the duration in seconds of live mode sampling intervals. It is recommended to set this option greater than 10 seconds otherwise there won't be much useful information captured during sampling.

### Memory Profiling
Memory profiling can be found in ```slips_files/common/memory_profiler.py```

Just like CPU profiling, it also has supports live and development mode.
Set ```memory_profiler_enable``` to ```yes``` to enable this feature.
Set ```memory_profiler_mode``` to ```live``` to use live mode or ```dev``` to use development mode profiling.

#### Live Mode
This mode shows memory usage stats during the runtime of the program.
```memory_profiler_multiprocess``` controls whether live mode tracks all processes or only the main process. If set to no, the program will wait for you to connect from a different terminal using the command ```memray live <port_number>```, where port_number is 5000 by default. After connection, the program will continue with its run and the terminal that is connected will receive a feed of the memory statistics. If set to yes, the redis channel "memory_profile" can be used to set pid of the process to be tracked. Only a single process can be tracked at a time. The interface is cumbersome to use from the command line so multiprocess live profiling is intended to be used primarily from the web interface.

#### Development Mode
When enabled, the profiler will output the profile data into the output directory. The data will be in the ```memoryprofile``` directory of the output directory of the run. Each process during the run of the program will have an associated binary file. Each of the generated binaries will automatically be converted to viewable html files, with each process converted to a flamegraph and table format. All generated files will be denoted by their PID.

---

Expand Down
Binary file added docs/images/alternate_mem_profiler_testing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/cpu-profiler-config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/cpu-profiler-live-mode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/cpu-profiler-live-results.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/cpu-profiler-starting.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/cpu-profiler-termination.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/cpu=profiler-results.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/flamegraph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/live-mem-profiler.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/mem-profiler-ending.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/mem-profiler-running.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/mem-profiler-starting.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/mem-profiler-table-view.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/memory_profiler_interface.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/memory_profiler_structure.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/running-vizviewer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/testing_live_cpu_profiler.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/testinig_mem_profiler_live_mode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
473 changes: 473 additions & 0 deletions docs/profiling_slips.md

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions modules/flowalerts/flowalerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,9 @@ def check_data_upload(self, sbytes, daddr, uid, profileid, twid):
"""
Set evidence when 1 flow is sending >= the flow_upload_threshold bytes
"""


if (
self.is_ignored_ip_data_upload(daddr)
not daddr
or self.is_ignored_ip_data_upload(daddr)
or not sbytes
):
return False
Expand Down
11 changes: 6 additions & 5 deletions process_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def __init__(self, main):

def start_output_process(self, current_stdout, stderr, slips_logfile):
output_process = OutputProcess(
self.main.db,
self.main.output_queue,
self.main.args.output,
self.main.redis_port,
self.termination_event,
verbose=self.main.args.verbose,
debug=self.main.args.debug,
Expand All @@ -46,9 +46,9 @@ def start_output_process(self, current_stdout, stderr, slips_logfile):

def start_profiler_process(self):
profiler_process = ProfilerProcess(
self.main.db,
self.main.output_queue,
self.main.args.output,
self.main.redis_port,
self.termination_event,
profiler_queue=self.profiler_queue,
)
Expand All @@ -64,9 +64,9 @@ def start_profiler_process(self):

def start_evidence_process(self):
evidence_process = EvidenceProcess(
self.main.db,
self.main.output_queue,
self.main.args.output,
self.main.redis_port,
self.termination_event,
)
evidence_process.start()
Expand All @@ -81,9 +81,9 @@ def start_evidence_process(self):

def start_input_process(self):
input_process = InputProcess(
self.main.db,
self.main.output_queue,
self.main.args.output,
self.main.redis_port,
self.termination_event,
profiler_queue=self.profiler_queue,
input_type=self.main.input_type,
Expand Down Expand Up @@ -229,7 +229,8 @@ def load_modules(self):
module_class = modules_to_call[module_name]["obj"]
module = module_class(
self.main.output_queue,
self.main.db,
self.main.args.output,
self.main.redis_port,
self.termination_event,
)
module.start()
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ termcolor
communityid
viztracer
yappi
pytest-sugar
memray
57 changes: 56 additions & 1 deletion slips.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import multiprocessing
from slips_files.common.imports import *
from slips_files.common.cpu_profiler import CPUProfiler
from slips_files.common.memory_profiler import MemoryProfiler
from exclusiveprocess import Lock, CannotAcquireLock
from redis_manager import RedisManager
from metadata_manager import MetadataManager
Expand Down Expand Up @@ -124,6 +125,51 @@ def cpu_profiler_release(self):
if self.cpuProfilerEnabled and not self.cpuProfilerMultiprocess:
self.cpuProfiler.stop()
self.cpuProfiler.print()

def memory_profiler_init(self):
self.memoryProfilerEnabled = slips.conf.get_memory_profiler_enable() == "yes"
memoryProfilerMode = slips.conf.get_memory_profiler_mode()
memoryProfilerMultiprocess = slips.conf.get_memory_profiler_multiprocess() == "yes"
if self.memoryProfilerEnabled:
output_dir = os.path.join(slips.args.output,'memoryprofile/')
if not os.path.exists(output_dir):
os.makedirs(output_dir)
output_file = os.path.join(output_dir, 'memory_profile.bin')
self.memoryProfiler = MemoryProfiler(output_file, db=self.db, mode=memoryProfilerMode, multiprocess=memoryProfilerMultiprocess)
self.memoryProfiler.start()

def memory_profiler_release(self):
if self.memoryProfilerEnabled:
self.memoryProfiler.stop()

def memory_profiler_multiproc_test(self):
def target_function():
print("Target function started")
time.sleep(5)

def mem_function():
print("Mem function started")
while True:
time.sleep(1)
array = []
for i in range(1000000):
array.append(i)
processes = []
num_processes = 3

for _ in range(num_processes):
process = multiprocessing.Process(target=target_function if _%2 else mem_function)
process.start()
processes.append(process)

# Message passing
self.db.publish("memory_profile", processes[1].pid) # successful
# subprocess.Popen(["memray", "live", "1234"])
time.sleep(5) # target_function will timeout and tracker will be cleared
self.db.publish("memory_profile", processes[0].pid) # end but maybe don't start
time.sleep(5) # mem_function will get tracker started
self.db.publish("memory_profile", processes[0].pid) # start successfully
input()

def get_slips_version(self):
version_file = 'VERSION'
Expand Down Expand Up @@ -175,7 +221,10 @@ def update_local_TI_files(self):
# so this function will only be allowed to run from 1 slips instance.
with Lock(name="slips_ports_and_orgs"):
# pass a dummy termination event for update manager to update orgs and ports info
update_manager = UpdateManager(self.output_queue, self.db, multiprocessing.Event())
update_manager = UpdateManager(self.output_queue,
self.args.output,
self.redis_port,
multiprocessing.Event())
update_manager.update_ports_info()
update_manager.update_org_files()
except CannotAcquireLock:
Expand Down Expand Up @@ -501,6 +550,10 @@ def start(self):
})

self.cpu_profiler_init()
self.memory_profiler_init()
# uncomment line to see that memory profiler works correctly
# Should print out red text if working properly
# self.memory_profiler_multiproc_test()

# if stdout is redirected to a file,
# tell outputProcess.py to redirect it's output as well
Expand Down Expand Up @@ -705,5 +758,7 @@ def sig_handler(sig, frame):
daemon.start()
else:
# interactive mode
pass
slips.start()
slips.cpu_profiler_release()
slips.memory_profiler_release()
Loading