-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add to_dict.py * Add tests * Add tests * Update README.md with info about to_dict, to_list
- Loading branch information
Showing
6 changed files
with
146 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
__values = (int, float, str, bytes, bool, | ||
list, tuple, set, | ||
dict) | ||
|
||
|
||
def _layer2dict(frame): | ||
temp_dict = {} | ||
|
||
if not getattr(frame, 'fields_desc', None): | ||
return | ||
for _field in frame.fields_desc: | ||
value = getattr(frame, _field.name) | ||
if isinstance(value, type(None)): | ||
value = None | ||
elif not isinstance(value, __values): | ||
value = _layer2dict(value) | ||
temp_dict[_field.name] = value | ||
return {frame.name: temp_dict} | ||
|
||
|
||
def to_dict(packet, layer=0, extend=False): | ||
return _layer2dict(packet.getlayer(layer)) | ||
|
||
|
||
def to_list(packet, extend=False): | ||
return [_layer2dict(packet.getlayer(x)) for x in range(len(packet.layers()))] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from unittest import TestCase | ||
|
||
from scapy.layers.inet import TCP, IP | ||
from scapy.layers.l2 import Ether | ||
|
||
from scapy_helper.helpers.to_dict import to_dict, to_list | ||
|
||
|
||
class TestToDict(TestCase): | ||
def test_simple_dict(self): | ||
packet = Ether(dst="ff:ff:ff:ff:ff:ff", src="00:00:00:00:00:00") / IP() / TCP() | ||
packet_result = {'Ethernet': {'src': '00:00:00:00:00:00', 'dst': 'ff:ff:ff:ff:ff:ff', 'type': 2048}} | ||
|
||
to_dict_result = to_dict(packet) | ||
|
||
self.assertTrue(isinstance(to_dict_result, dict)) | ||
self.assertEqual(to_dict_result, packet_result) | ||
|
||
def test_simple_dict_get_second_element(self): | ||
packet = Ether(dst="ff:ff:ff:ff:ff:ff", src="00:00:00:00:00:00") / IP() / TCP() | ||
packet_result = {'IP': {'frag': 0, 'src': '0.0.0.0', 'proto': 6, 'tos': 0, 'dst': '127.0.0.1', | ||
'chksum': None, 'len': None, 'options': [], 'version': 4, 'flags': None, | ||
'ihl': None, 'ttl': 64, 'id': 1}} | ||
|
||
to_dict_result = to_dict(packet, layer=1) # layer 1 is IP | ||
|
||
self.assertTrue(isinstance(to_dict_result, dict)) | ||
self.assertEqual(to_dict_result, packet_result) | ||
|
||
|
||
class TestToList(TestCase): | ||
def test_simple_list(self): | ||
packet = Ether(dst="ff:ff:ff:ff:ff:ff", src="00:00:00:00:00:00") / IP() / TCP() | ||
packet_result = [{'Ethernet': {'src': '00:00:00:00:00:00', 'dst': 'ff:ff:ff:ff:ff:ff', 'type': 2048}}, { | ||
'IP': {'frag': 0, 'src': '0.0.0.0', 'proto': 6, 'tos': 0, 'dst': '127.0.0.1', 'chksum': None, 'len': None, | ||
'options': [], 'version': 4, 'flags': None, 'ihl': None, 'ttl': 64, 'id': 1}}, { | ||
'TCP': {'reserved': 0, 'seq': 0, 'ack': 0, 'dataofs': None, 'urgptr': 0, 'window': 8192, | ||
'flags': None, 'chksum': None, 'dport': 80, 'sport': 20, 'options': []}}] | ||
|
||
to_list_result = to_list(packet) | ||
|
||
self.assertTrue(isinstance(to_list_result, list)) | ||
self.assertEqual(to_list_result, packet_result) |