@@ -144,11 +144,13 @@ def init(self):
144144```
145145
146146``` python
147- def pre_main (self ):
148- """
149- Initializations that run only once before the main() function runs in a loop
150- """
151- utils.drop_root_privs()
147+ def pre_main (
148+ self
149+ ):
150+ """
151+ Initializations that run only once before the main() function runs in a loop
152+ """
153+ utils.drop_root_privs_permanently()
152154
153155```
154156
@@ -367,147 +369,147 @@ from slips_files.common.flow_classifier import FlowClassifier
367369from slips_files.core.structures.evidence import
368370
369371(
370- Evidence,
371- ProfileID,
372- TimeWindow,
373- Victim,
374- Attacker,
375- ThreatLevel,
376- EvidenceType,
377- IoCType,
378- Direction,
379- )
372+ Evidence,
373+ ProfileID,
374+ TimeWindow,
375+ Victim,
376+ Attacker,
377+ ThreatLevel,
378+ EvidenceType,
379+ IoCType,
380+ Direction,
381+ )
380382from slips_files.common.parsers.config_parser import ConfigParser
381383from slips_files.common.slips_utils import utils
382384from slips_files.common.abstracts.imodule import IModule
383385
384386
385387class LocalConnectionDetector (
386- IModule
387- ):
388- # Name: short name of the module. Do not use spaces
389- name = ' local_connection_detector'
390- description = ' detects connections to other devices in your local network'
391- authors = [' Template Author' ]
392-
393-
394- def init (
395- self
396- ):
397- # To which channels do you want to subscribe? When a message
398- # arrives on the channel the module will receive a msg
399-
400- # You can find the full list of channels at
401- # slips_files/core/database/redis_db/database.py
402- self .c1 = self .db.subscribe(
403- ' new_flow'
404- )
405- self .channels = {
406- ' new_flow' : self .c1,
407- }
408- # to be able to convert flows from dict format to objects
409- self .classifier = FlowClassifier()
410-
411-
412- def pre_main (
413- self
414- ):
415- """
388+ IModule
389+ ):
390+ # Name: short name of the module. Do not use spaces
391+ name = ' local_connection_detector'
392+ description = ' detects connections to other devices in your local network'
393+ authors = [' Template Author' ]
394+
395+
396+ def init (
397+ self
398+ ):
399+ # To which channels do you want to subscribe? When a message
400+ # arrives on the channel the module will receive a msg
401+
402+ # You can find the full list of channels at
403+ # slips_files/core/database/redis_db/database.py
404+ self .c1 = self .db.subscribe(
405+ ' new_flow'
406+ )
407+ self .channels = {
408+ ' new_flow' : self .c1,
409+ }
410+ # to be able to convert flows from dict format to objects
411+ self .classifier = FlowClassifier()
412+
413+
414+ def pre_main (
415+ self
416+ ):
417+ """
416418 Initializations that run only once before the main() function runs in a loop
417419 """
418- utils.drop_root_privs ()
419-
420-
421- def main (
422- self
423- ):
424- """ Main loop function"""
425- if msg := self .get_msg(
426- ' new_flow'
427- ):
428- msg = json.loads(
429- msg[' data' ]
430- )
431- # convert the given dict flow to a flow object
432- flow = self .classifier.convert_to_flow_obj(
433- msg[" flow" ]
434- )
435- saddr = flow.saddr
436- daddr = flow.daddr
437- timestamp = flow.starttime
438- srcip_obj = ipaddress.ip_address(
439- saddr
440- )
441- dstip_obj = ipaddress.ip_address(
442- daddr
443- )
444- if srcip_obj.is_private and dstip_obj.is_private:
445- # on a scale of 0 to 1, how confident you are of this evidence
446- confidence = 0.8
447- # how dangerous is this evidence? info, low, medium, high, critical?
448- threat_level = ThreatLevel.HIGH
449-
450- # the name of your evidence, you can put any descriptive string here
451- # this is the type we just created
452- evidence_type = EvidenceType.CONNECTION_TO_LOCAL_DEVICE
453- # which ip is the attacker here?
454- attacker = Attacker(
455- direction = Direction.SRC ,
456- # who's the attacker the src or the dst?
457- attacker_type = IoCType.IP ,
458- # is it an IP? is it a domain? etc.
459- value = saddr
460- # the actual ip/domain/url of the attacker, in our case, this is the IP
461- )
462- victim = Victim(
463- direction = Direction.SRC ,
464- ioc_type = IoCType.IP ,
465- value = daddr,
466- )
467- # describe the evidence
468- description = f ' A connection to a local device { daddr} '
469- # the current profile is the source ip,
470- # this comes in the msg received in the channel
471- # the profile this evidence should be in, should be the profile of the attacker
472- # because this is evidence that this profile is attacker others right?
473- profile = ProfileID(
474- ip = saddr
475- )
476- # Profiles are split into timewindows, each timewindow is 1h,
477- # this if of the timewindwo comes in the msg received in the channel
478- twid_number = int (
479- msg[' twid' ].replace(
480- " timewindow" ,
481- ' '
482- )
483- )
484- timewindow = TimeWindow(
485- number = twid_number
486- )
487- # list of uids of the flows that are part of this evidence
488- uid_list = [flow.uid]
489- # no use the above info to create the evidence obj
490- evidence = Evidence(
491- evidence_type = evidence_type,
492- attacker = attacker,
493- threat_level = threat_level,
494- description = description,
495- victim = victim,
496- profile = profile,
497- timewindow = timewindow,
498- uid = uid_list,
499- # when did this evidence happen? use the
500- # flow's ts detected by zeek
501- # this comes in the msg received in the channel
502- timestamp = timestamp,
503- confidence = confidence
504- )
505- self .db.set_evidence(
506- evidence
507- )
508- self .print(
509- " Done setting evidence!!!"
510- )
420+ utils.drop_root_privs_permanently ()
421+
422+
423+ def main (
424+ self
425+ ):
426+ """ Main loop function"""
427+ if msg := self .get_msg(
428+ ' new_flow'
429+ ):
430+ msg = json.loads(
431+ msg[' data' ]
432+ )
433+ # convert the given dict flow to a flow object
434+ flow = self .classifier.convert_to_flow_obj(
435+ msg[" flow" ]
436+ )
437+ saddr = flow.saddr
438+ daddr = flow.daddr
439+ timestamp = flow.starttime
440+ srcip_obj = ipaddress.ip_address(
441+ saddr
442+ )
443+ dstip_obj = ipaddress.ip_address(
444+ daddr
445+ )
446+ if srcip_obj.is_private and dstip_obj.is_private:
447+ # on a scale of 0 to 1, how confident you are of this evidence
448+ confidence = 0.8
449+ # how dangerous is this evidence? info, low, medium, high, critical?
450+ threat_level = ThreatLevel.HIGH
451+
452+ # the name of your evidence, you can put any descriptive string here
453+ # this is the type we just created
454+ evidence_type = EvidenceType.CONNECTION_TO_LOCAL_DEVICE
455+ # which ip is the attacker here?
456+ attacker = Attacker(
457+ direction = Direction.SRC ,
458+ # who's the attacker the src or the dst?
459+ attacker_type = IoCType.IP ,
460+ # is it an IP? is it a domain? etc.
461+ value = saddr
462+ # the actual ip/domain/url of the attacker, in our case, this is the IP
463+ )
464+ victim = Victim(
465+ direction = Direction.SRC ,
466+ ioc_type = IoCType.IP ,
467+ value = daddr,
468+ )
469+ # describe the evidence
470+ description = f ' A connection to a local device { daddr} '
471+ # the current profile is the source ip,
472+ # this comes in the msg received in the channel
473+ # the profile this evidence should be in, should be the profile of the attacker
474+ # because this is evidence that this profile is attacker others right?
475+ profile = ProfileID(
476+ ip = saddr
477+ )
478+ # Profiles are split into timewindows, each timewindow is 1h,
479+ # this if of the timewindwo comes in the msg received in the channel
480+ twid_number = int (
481+ msg[' twid' ].replace(
482+ " timewindow" ,
483+ ' '
484+ )
485+ )
486+ timewindow = TimeWindow(
487+ number = twid_number
488+ )
489+ # list of uids of the flows that are part of this evidence
490+ uid_list = [flow.uid]
491+ # no use the above info to create the evidence obj
492+ evidence = Evidence(
493+ evidence_type = evidence_type,
494+ attacker = attacker,
495+ threat_level = threat_level,
496+ description = description,
497+ victim = victim,
498+ profile = profile,
499+ timewindow = timewindow,
500+ uid = uid_list,
501+ # when did this evidence happen? use the
502+ # flow's ts detected by zeek
503+ # this comes in the msg received in the channel
504+ timestamp = timestamp,
505+ confidence = confidence
506+ )
507+ self .db.set_evidence(
508+ evidence
509+ )
510+ self .print(
511+ " Done setting evidence!!!"
512+ )
511513```
512514
513515All good, you can find your evidence now in alerts.json and alerts.log of the output directory.
@@ -531,7 +533,7 @@ Now here's the ```pre_main()``` function, all initializations like dropping root
531533should be done here
532534
533535``` python
534- utils.drop_root_privs ()
536+ utils.drop_root_privs_permanently ()
535537 ```
536538the above line is responsible for dropping root privileges,
537539so if slips starts with sudo and the module doesn't need the root permissions, we drop them.
0 commit comments