Skip to content

Commit

Permalink
✅ ℹ️
Browse files Browse the repository at this point in the history
ℹ️ minor updates
✅ reset method added
✅ pcap_layer_stats method added
  • Loading branch information
securisecctf committed Dec 4, 2019
1 parent d4a69f9 commit a4c089c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/tests_multi_os.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
python -m pip install --upgrade pip
pip install .
- name: Test with pytest
if
run: |
pytest --disable-pytest-warnings --cov-report=xml --cov=chepy --cov-config=.coveragerc tests/
coverage report -m
Expand All @@ -42,6 +43,7 @@ jobs:
run: |
make -C docs/ clean html
- name: Upload to codecov
if: contains(matrix.matrix.python-version-on, '3.8') && contains(matrix.runs-on, 'ubuntu')
uses: codecov/codecov-action@v1.0.3
with:
token: ${{secrets.CODECOV_TOKEN}}
Expand Down
13 changes: 13 additions & 0 deletions chepy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class ChepyCore(object):

def __init__(self, *data):
self.states = dict(list(enumerate(data)))
#: Holder for the initial state
self.__initial_states = dict(list(enumerate(data)))
#: Value of the initial state
self._current_index = 0
self.buffers = dict()
#: Alias for `write_to_file`
Expand Down Expand Up @@ -973,3 +976,13 @@ def debug(self, verbose: bool = False):
print(MAGENTA("Buffers:"), self.buffers)
return self

@ChepyDecorators.call_stack
def reset(self):
"""Reset states back to their initial values
Returns:
Chepy: The Chepy object.
"""
self.states = self.__initial_states
return self

2 changes: 1 addition & 1 deletion chepy/modules/internal/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def __init__(self, pkt):
def to_dict(self):
"""
Turn every layer to dict, store in ChainMap type.
:return: ChainMaq
`Reference <https://github.com/littlezz/scapy2dict>`__
"""
d = list()
count = 0
Expand Down
27 changes: 27 additions & 0 deletions chepy/modules/pcap.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import collections

import scapy.all as scapy
import scapy.layers.dns as scapy_dns
import scapy.layers.http as scapy_http
Expand Down Expand Up @@ -112,3 +114,28 @@ def pcap_to_dict(self):
hold.append(Pkt2Dict(packet).to_dict())
self.state = hold
return self

@ChepyDecorators.call_stack
def pcap_layer_stats(self):
"""Get a count of all layers in the pcap
Returns:
Chepy: The Chepy object.
"""

def get_layers(pkt):
yield pkt.name
while pkt.payload:
pkt = pkt.payload
yield pkt.name

layer_dict = collections.OrderedDict()
for packet in self._pcap_read:
for key in list(get_layers(packet)):
if layer_dict.get(key):
layer_dict[key] += 1
else:
layer_dict[key] = 1

self.state = dict(layer_dict)
return self
3 changes: 3 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,6 @@ def test_loop_dict():
e = Chepy({"some": "hahahaha", "lol": "aahahah"})
e.loop_dict(["some"], "hmac_hash", {"key": "secret"})
assert e.o == {"some": "99f77ec06a3c69a4a95371a7888245ba57f47f55", "lol": "aahahah"}

def test_reset():
assert Chepy('41', '42').from_hex().reset().states == {0: '41', 1: '42'}
3 changes: 3 additions & 0 deletions tests/test_pcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ def test_packet_to_dict():
== "10.10.10.11"
)


def test_pcap_layer_stats():
assert Chepy("tests/files/test.pcapng").read_pcap().pcap_layer_stats().get_by_key("DNS").o == 6

0 comments on commit a4c089c

Please sign in to comment.