Skip to content

Commit 0a7aa37

Browse files
committed
Add callback mechanism to hook into found creds processing
1 parent 6d9c1a7 commit 0a7aa37

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

credslayer/core/manager.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def _process_packet(session: Session, packet: Packet, must_inspect_strings: bool
8787
logger.info(session, "Credit card '{}' found: '{}'".format(credit_card.name, credit_card.number))
8888

8989

90-
def _process_packets_from(packets_input: Capture, manager: SessionsManager, must_inspect_strings: bool):
90+
def _process_packets_from(packets_input: Capture, manager: SessionsManager, must_inspect_strings: bool = False):
9191
"""
9292
Loops over available packets, retrieves its session and handles potential exceptions.
9393
@@ -131,7 +131,8 @@ def _process_packets_from(packets_input: Capture, manager: SessionsManager, must
131131
clean_before_exit()
132132

133133

134-
def process_pcap(filename: str, must_inspect_strings=False, tshark_filter=None, debug=False, decode_as=None) -> SessionsManager:
134+
def process_pcap(filename: str, must_inspect_strings=False, tshark_filter=None, debug=False,
135+
decode_as=None, creds_found_callback=None) -> SessionsManager:
135136
"""
136137
Initialize the processing of a pcap file and retrieve results of the analysis.
137138
This is one of the main entry points most people will want to use.
@@ -154,6 +155,9 @@ def process_pcap(filename: str, must_inspect_strings=False, tshark_filter=None,
154155
decode_as : Dict[str, str]
155156
Associate a protocol to a port so that tshark processes packets correctly.
156157
158+
creds_found_callback : Callable[[Credentials], None]
159+
The function to call every time new credentials are found. Credentials are passed as parameter.
160+
157161
Returns
158162
-------
159163
A `SessionsManager` instance which gives to ability to the user of that function to retrieve
@@ -162,6 +166,7 @@ def process_pcap(filename: str, must_inspect_strings=False, tshark_filter=None,
162166

163167
logger.DEBUG_MODE = debug
164168
sessions_manager = SessionsManager()
169+
Session.creds_found_callback = creds_found_callback
165170

166171
with pyshark.FileCapture(filename, display_filter=tshark_filter, decode_as=decode_as, debug=debug) as pcap:
167172
logger.info("Processing packets in '{}'".format(filename))
@@ -184,7 +189,8 @@ def process_pcap(filename: str, must_inspect_strings=False, tshark_filter=None,
184189
return sessions_manager
185190

186191

187-
def active_processing(interface: str, must_inspect_strings=False, tshark_filter=None, debug=False, decode_as=None, pcap_output=None):
192+
def active_processing(interface: str, must_inspect_strings=False, tshark_filter=None, debug=False, decode_as=None,
193+
pcap_output=None, creds_found_callback=None):
188194
"""
189195
Initialize packets capturing on a given interface file.
190196
This is one of the main entry points most people will want to use.
@@ -209,11 +215,15 @@ def active_processing(interface: str, must_inspect_strings=False, tshark_filter=
209215
210216
pcap_output : str
211217
Captured packets will be output to that file path.
218+
219+
creds_found_callback : Callable[[Credentials], None]
220+
The function to call every time new credentials are found. Credentials are passed as parameter.
212221
"""
213222

214223
logger.DEBUG_MODE = debug
215224

216225
sessions = SessionsManager(remove_outdated=True)
226+
Session.creds_found_callback = creds_found_callback
217227

218228
signal.signal(signal.SIGINT, signal_handler)
219229

credslayer/core/session.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class Session(dict):
6767
"""
6868

6969
INACTIVE_SESSION_DELAY = 10 # in seconds
70+
creds_found_callback = None
7071

7172
def __init__(self, packet: Packet):
7273
super().__init__()
@@ -138,6 +139,10 @@ def validate_credentials(self):
138139
instance of `Credentials` in order to build new potential incoming credentials of the same session.
139140
"""
140141
self.credentials_list.append(self.credentials_being_built)
142+
143+
if Session.creds_found_callback:
144+
Session.creds_found_callback(self.credentials_being_built)
145+
141146
self.credentials_being_built = Credentials()
142147

143148
def invalidate_credentials_and_clear_session(self):

0 commit comments

Comments
 (0)