Skip to content

Commit 35e33e0

Browse files
committed
horizontal_portscan: use the new db keys
1 parent 1322cd9 commit 35e33e0

File tree

1 file changed

+26
-50
lines changed

1 file changed

+26
-50
lines changed

modules/network_discovery/horizontal_portscan.py

Lines changed: 26 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
IoCType,
1616
Direction,
1717
)
18+
from slips_files.core.structures.flow_attributes import State
1819

1920

2021
class HorizontalPortscan:
@@ -72,29 +73,6 @@ def get_twid_identifier(self, profileid: str, twid: str, dport) -> str:
7273

7374
return f"{profileid}:{twid}:dport:{dport}"
7475

75-
def get_packets_sent(self, dstips: dict) -> int:
76-
"""
77-
returns the total amount of packets sent to all dst IPs
78-
:param dstips: dict with info about in the following format
79-
{ dstip: {
80-
'pkts': src+dst packets sent to this dstip,
81-
'spkts': src packets sent to this dstip,
82-
'stime': timestamp of the first flow in the uid list,
83-
'uid': [uids of flows to this ip]
84-
}
85-
}
86-
"""
87-
pkts_sent = 0
88-
for dstip in dstips:
89-
if "spkts" not in dstips[dstip]:
90-
# In argus files there are no src pkts, only pkts.
91-
# So it is better to have the total pkts than
92-
# to have no packets count
93-
pkts_sent += int(dstips[dstip]["pkts"])
94-
else:
95-
pkts_sent += int(dstips[dstip]["spkts"])
96-
return pkts_sent
97-
9876
def are_dstips_greater_or_eq_minimum_dstips(self, dstips) -> bool:
9977
return dstips >= self.minimum_dstips_to_set_evidence
10078

@@ -193,48 +171,46 @@ def set_evidence_horizontal_portscan(self, evidence: dict):
193171
def is_valid_twid(twid: str) -> bool:
194172
return not (twid in ("", None) or "timewindow" not in twid)
195173

196-
def check(self, profileid: str, twid: str):
174+
def check(self, profileid: ProfileID, twid: TimeWindow):
197175
if not utils.are_scan_detection_modules_interested_in_this_ip(
198176
profileid.ip
199-
) or not self.is_valid_twid(twid):
177+
):
200178
return False
201179
# if you're portscaning a port that is open it's gonna be established
202180
# the amount of open ports we find is gonna be so small
203181
# theoretically this is incorrect bc we'll be ignoring
204182
# established evidence,
205183
# but usually open ports are very few compared to the whole range
206-
# so, practically this is correct to avoid FP
207-
state = "Not Established"
184+
# so, practically using not established only this is correct to
185+
# avoid FP
208186
for protocol in (Protocol.TCP, Protocol.UDP):
209-
dports: dict = self.get_not_estab_dst_ports(
210-
protocol, state, profileid, twid
211-
)
212-
213187
# For each port, see if the amount is over the threshold
214-
for dport in dports.keys():
215-
# PortScan Type 2. Direction OUT
216-
twid_identifier: str = self.get_twid_identifier(
217-
profileid, twid, dport
188+
for (
189+
dport,
190+
total_pkts,
191+
) in self.db.get_dstports_of_not_established_flows(
192+
profileid, twid, protocol
193+
):
194+
195+
amount_of_dstips: int = (
196+
self.db.get_amount_of_dstips_for_not_established_flows_on_port(
197+
profileid, twid, protocol, dport
198+
)
218199
)
219-
if not twid_identifier:
220-
continue
221-
222-
dstips: dict = dports[dport]["dstips"]
223-
amount_of_dips = len(dstips)
224-
225200
if self.check_if_enough_dstips_to_trigger_an_evidence(
226-
twid_identifier, amount_of_dips
201+
profileid, twid, dport, amount_of_dstips
227202
):
228203
evidence = {
229-
"protocol": protocol,
230-
"profileid": profileid,
231-
"twid": twid,
232-
"uids": self.get_uids(dstips),
204+
"protocol": protocol.name.lower(),
205+
"profileid": str(profileid),
206+
"twid": str(twid),
207+
"uids": [],
233208
"dport": dport,
234-
"pkts_sent": self.get_packets_sent(dstips),
235-
"timestamp": next(iter(dstips.values()))["stime"],
236-
"state": state,
237-
"amount_of_dips": amount_of_dips,
209+
"pkts_sent": total_pkts,
210+
"timestamp": "", # TODO,
211+
"state": State.NOT_EST.name.lower(),
212+
"amount_of_dips": amount_of_dstips,
238213
}
239214

240215
self.set_evidence_horizontal_portscan(evidence)
216+
return

0 commit comments

Comments
 (0)