Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
IDS PSE
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Daniel Yang
IDS PSE
Commits
2ceeef06
Commit
2ceeef06
authored
4 months ago
by
Daniel Yang
Browse files
Options
Downloads
Patches
Plain Diff
Cleaned up the tests
parent
292529a5
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
code/src/packet_capturing.py
+1
-2
1 addition, 2 deletions
code/src/packet_capturing.py
code/test/test_packet_capturing.py
+14
-22
14 additions, 22 deletions
code/test/test_packet_capturing.py
with
15 additions
and
24 deletions
code/src/packet_capturing.py
+
1
−
2
View file @
2ceeef06
...
...
@@ -106,8 +106,7 @@ def get_dict():
return
syn_counts
def
dict_clear
():
global
syn_counts
syn_counts
=
defaultdict
(
int
)
syn_counts
.
clear
()
def
main
():
print
(
"
Starting packet capture...
"
)
...
...
This diff is collapsed.
Click to expand it.
code/test/test_packet_capturing.py
+
14
−
22
View file @
2ceeef06
import
unittest
from
scapy.layers.inet
import
IP
,
TCP
from
unittest.mock
import
patch
from
unittest.mock
import
patch
,
MagicMock
from
scapy.layers.l2
import
Ether
from
src.packet_capturing
import
packet_handler
,
get_dict
,
dict_clear
EXPECTED_EMPTY_DICT
=
"
Expected empty dict
"
EXPECTED_VALUE_DICT
=
"
Expected packet
'
s IP in the dict
"
from
src.packet_capturing
import
packet_handler
,
get_dict
,
dict_clear
,
syn_flood_detection
,
port_check
,
ip_spoofing
,
\
syn_fin
,
null_packet
class
TestPacketCapturing
(
unittest
.
TestCase
):
...
...
@@ -18,55 +16,44 @@ class TestPacketCapturing(unittest.TestCase):
# Create a dummy packet
packet
=
IP
(
src
=
"
100.84.6.141
"
,
dst
=
"
192.168.1.1
"
)
/
TCP
(
dport
=
80
,
sport
=
12345
,
flags
=
"
S
"
)
# Show detailed information about the package
#
print(packet.show())
print
(
packet
.
show
())
# Mockito checks if print was called
with
patch
(
'
builtins.print
'
)
as
mock_print
:
packet_handler
(
packet
)
mock_print
.
assert_any_call
(
"
Packet captured: 100.84.6.141 -> 192.168.1.1
"
)
self
.
assertEqual
({
'
100.84.6.141
'
:
1
},
get_dict
()
,
EXPECTED_VALUE_DICT
)
def
test_ip_spoofing
(
self
):
# Starts with 169.254, it's a suspicious packet
packet
=
IP
(
src
=
"
169.254.6.141
"
,
dst
=
"
192.168.1.1
"
)
with
patch
(
'
builtins.print
'
)
as
mock_print
:
packet_handler
(
packet
)
ip_spoofing
(
0
,
packet
[
IP
].
src
)
mock_print
.
assert_any_call
(
"
Possible IP spoofing using private networks detected.
"
)
# Has no TCP flags, therefore dict must be empty
self
.
assertEqual
({},
get_dict
(),
EXPECTED_EMPTY_DICT
)
def
test_syn_fin
(
self
):
# Has S and F flags, which is also known as SYN-FIN, which is a malicious packet signature
# Ether addresses are placeholders
packet
=
IP
(
src
=
"
100.84.6.141
"
,
dst
=
"
192.168.1.1
"
)
/
TCP
(
dport
=
80
,
flags
=
"
SF
"
)
/
Ether
(
src
=
"
00:11:22:33:44:55
"
,
dst
=
"
ff:ff:ff:ff:ff:ff
"
)
with
patch
(
'
builtins.print
'
)
as
mock_print
:
packet_handler
(
packet
)
syn_fin
(
packet
)
mock_print
.
assert_any_call
(
"
Malicious packet detected: SYN-FIN combination.
"
)
self
.
assertEqual
({},
get_dict
(),
EXPECTED_EMPTY_DICT
)
def
test_null_packet
(
self
):
packet
=
IP
(
src
=
"
100.84.6.141
"
,
dst
=
"
192.168.1.1
"
)
/
TCP
(
dport
=
80
,
flags
=
0
)
/
Ether
(
src
=
"
00:11:22:33:44:55
"
,
dst
=
"
ff:ff:ff:ff:ff:ff
"
)
with
patch
(
'
builtins.print
'
)
as
mock_print
:
packet
_handler
(
packet
)
null_
packet
(
packet
)
mock_print
.
assert_any_call
(
"
Malicious null packet found.
"
)
self
.
assertEqual
(
get_dict
(),
{},
EXPECTED_EMPTY_DICT
)
def
test_port_dest_check
(
self
):
packet
=
IP
(
src
=
"
100.84.6.141
"
,
dst
=
"
192.168.1.1
"
)
/
TCP
(
dport
=
0
,
sport
=
200
,
flags
=
"
F
"
)
/
Ether
(
src
=
"
00:11:22:33:44:55
"
,
dst
=
"
ff:ff:ff:ff:ff:ff
"
)
with
patch
(
'
builtins.print
'
)
as
mock_print
:
p
acket_handler
(
packet
)
p
ort_check
(
packet
)
mock_print
.
assert_any_call
(
"
Illegal packet with source or destination port 0.
"
)
self
.
assertEqual
(
get_dict
(),
{},
EXPECTED_EMPTY_DICT
)
def
test_port_src_check
(
self
):
packet
=
IP
(
src
=
"
100.84.6.141
"
,
dst
=
"
192.168.1.1
"
)
/
TCP
(
dport
=
80
,
sport
=
0
,
flags
=
"
F
"
)
/
Ether
(
src
=
"
00:11:22:33:44:55
"
,
dst
=
"
ff:ff:ff:ff:ff:ff
"
)
...
...
@@ -74,7 +61,12 @@ class TestPacketCapturing(unittest.TestCase):
packet_handler
(
packet
)
mock_print
.
assert_any_call
(
"
Illegal packet with source or destination port 0.
"
)
self
.
assertEqual
(
get_dict
(),
{},
EXPECTED_EMPTY_DICT
)
def
test_syn_flood_detection
(
self
):
packet
=
IP
(
src
=
"
100.84.6.141
"
,
dst
=
"
192.168.1.1
"
)
/
TCP
(
dport
=
80
,
sport
=
0
,
flags
=
"
S
"
)
for
i
in
range
(
101
):
syn_flood_detection
(
packet
)
self
.
assertEqual
({
"
100.84.6.141
"
:
101
},
get_dict
(),
"
Expected the packet
'
s IP with 101 entries
"
)
if
__name__
==
'
__main__
'
:
unittest
.
main
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment