-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI-Capture.py
More file actions
63 lines (52 loc) · 2.63 KB
/
Copy pathAPI-Capture.py
File metadata and controls
63 lines (52 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import sys
import argparse
import datetime
from scapy.all import *
def capture_packets(iface, dest_ips, verbose, silent):
api_call_count = 0
start_time = datetime.datetime.now()
filename = ""
def process_packet(packet):
nonlocal api_call_count, filename
# check if packet has TCP layer
if TCP in packet:
# check if packet destination IP is in scope
if packet[IP].dst in dest_ips:
# check if packet contains HTTP layer
if packet.haslayer(Raw) and "HTTP" in str(packet[TCP].payload):
# check if packet contains API call
if "API" in str(packet[Raw].load):
api_call_count += 1
# create new pcap file name
dest_ip = packet[IP].dst
curr_time = datetime.datetime.now().strftime("%H%M%S")
curr_date = datetime.datetime.now().strftime("%Y%m%d")
filename = f"{dest_ip}_{curr_time}_{curr_date}.pcap"
# save packets to pcap file
wrpcap(filename, packet, append=True)
# print message if not in silent mode
if not silent:
print(f"API call detected on {iface}. Saving as {filename} for later inspection")
# start capturing packets on specified interface
try:
sniff(iface=iface, prn=process_packet, store=0)
except KeyboardInterrupt:
print("Exiting program...")
finally:
end_time = datetime.datetime.now()
elapsed_time = end_time - start_time
print(f"Captured {api_call_count} API calls in {elapsed_time}.")
if not silent and filename:
print(f"Saved packets to {filename}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Packet capture program that saves TCP streams featuring API calls.")
parser.add_argument("-I", "--interface", help="Network interface to capture packets from.", required=True)
parser.add_argument("-S", "--silent", help="Run in silent mode (no print to screen).", action="store_true")
parser.add_argument("-V", "--verbose", help="Run in verbose mode (print all packets).", action="store_true")
parser.add_argument("-F", "--file", help="File containing destination IPs in scope.", required=True)
args = parser.parse_args()
# read in destination IPs from file
with open(args.file) as f:
dest_ips = [line.strip() for line in f.readlines()]
# start packet capture
capture_packets(args.interface, dest_ips, args.verbose, args.silent)