Skip to content

Commit

Permalink
Merge pull request #500 from kbandla/example_cleanup
Browse files Browse the repository at this point in the history
using utils for DRY; cleaning up examples a bit
  • Loading branch information
brifordwylie committed Dec 13, 2020
2 parents 81dce9e + 6cad879 commit 14c0110
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 91 deletions.
41 changes: 41 additions & 0 deletions dpkt/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Various Utility Functions"""
import socket
from .compat import compat_ord


def mac_to_str(address):
"""Convert a MAC address to a readable/printable string
Args:
address (str): a MAC address in hex form (e.g. '\x01\x02\x03\x04\x05\x06')
Returns:
str: Printable/readable MAC address
"""
return ':'.join('%02x' % compat_ord(b) for b in address)


def inet_to_str(inet):
"""Convert inet object to a string
Args:
inet (inet struct): inet network address
Returns:
str: Printable/readable IP address
"""
# First try ipv4 and then ipv6
try:
return socket.inet_ntop(socket.AF_INET, inet)
except ValueError:
return socket.inet_ntop(socket.AF_INET6, inet)


def test_utils():
"""Test the utility methods"""

print(mac_to_str(b'\x01\x02\x03\x04\x05\x06'))
assert mac_to_str(b'\x01\x02\x03\x04\x05\x06') == '01:02:03:04:05:06'
print(inet_to_str(b'\x91\xfe\xa0\xed'))
assert inet_to_str(b'\x91\xfe\xa0\xed') == '145.254.160.237'
ipv6_inet = b' \x01\r\xb8\x85\xa3\x00\x00\x00\x00\x8a.\x03ps4'
assert inet_to_str(ipv6_inet) == '2001:db8:85a3::8a2e:370:7334'
print('Success!')
31 changes: 2 additions & 29 deletions examples/print_http_requests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python
"""
This example expands on the print_packets example. It checks for HTTP request headers and displays their contents.
NOTE: We are not reconstructing 'flows' so the request (and response if you tried to parse it) will only
Expand All @@ -8,35 +7,9 @@
"""
import dpkt
import datetime
import socket
from dpkt.compat import compat_ord
from dpkt.utils import mac_to_str, inet_to_str


def mac_addr(address):
"""Convert a MAC address to a readable/printable string
Args:
address (str): a MAC address in hex form (e.g. '\x01\x02\x03\x04\x05\x06')
Returns:
str: Printable/readable MAC address
"""
return ':'.join('%02x' % compat_ord(b) for b in address)


def inet_to_str(inet):
"""Convert inet object to a string
Args:
inet (inet struct): inet network address
Returns:
str: Printable/readable IP address
"""
# First try ipv4 and then ipv6
try:
return socket.inet_ntop(socket.AF_INET, inet)
except ValueError:
return socket.inet_ntop(socket.AF_INET6, inet)

def print_http_requests(pcap):
"""Print out information about each packet in a pcap
Expand Down Expand Up @@ -76,7 +49,7 @@ def print_http_requests(pcap):

# Print out the info
print('Timestamp: ', str(datetime.datetime.utcfromtimestamp(timestamp)))
print('Ethernet Frame: ', mac_addr(eth.src), mac_addr(eth.dst), eth.type)
print('Ethernet Frame: ', mac_to_str(eth.src), mac_to_str(eth.dst), eth.type)
print('IP: %s -> %s (len=%d ttl=%d DF=%d MF=%d offset=%d)' %
(inet_to_str(ip.src), inet_to_str(ip.dst), ip.len, ip.ttl, do_not_fragment, more_fragments, fragment_offset))
print('HTTP request: %s\n' % repr(request))
Expand Down
38 changes: 7 additions & 31 deletions examples/print_icmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,9 @@
"""
import dpkt
import datetime
import socket
from dpkt.compat import compat_ord
from dpkt.utils import mac_to_str, inet_to_str


def mac_addr(address):
"""Convert a MAC address to a readable/printable string
Args:
address (str): a MAC address in hex form (e.g. '\x01\x02\x03\x04\x05\x06')
Returns:
str: Printable/readable MAC address
"""
return ':'.join('%02x' % compat_ord(b) for b in address)


def inet_to_str(inet):
"""Convert inet object to a string
Args:
inet (inet struct): inet network address
Returns:
str: Printable/readable IP address
"""
# First try ipv4 and then ipv6
try:
return socket.inet_ntop(socket.AF_INET, inet)
except ValueError:
return socket.inet_ntop(socket.AF_INET6, inet)

def print_icmp(pcap):
"""Print out information about each packet in a pcap
Expand Down Expand Up @@ -64,10 +38,12 @@ def print_icmp(pcap):

# Print out the info
print('Timestamp: ', str(datetime.datetime.utcfromtimestamp(timestamp)))
print( 'Ethernet Frame: ', mac_addr(eth.src), mac_addr(eth.dst), eth.type)
print( 'IP: %s -> %s (len=%d ttl=%d DF=%d MF=%d offset=%d)' % \
(inet_to_str(ip.src), inet_to_str(ip.dst), ip.len, ip.ttl, do_not_fragment, more_fragments, fragment_offset))
print('ICMP: type:%d code:%d checksum:%d data: %s\n' % (icmp.type, icmp.code, icmp.sum, repr(icmp.data)))
print('Ethernet Frame: ', mac_to_str(eth.src), mac_to_str(eth.dst), eth.type)
print('IP: %s -> %s (len=%d ttl=%d DF=%d MF=%d offset=%d)' %
(inet_to_str(ip.src), inet_to_str(ip.dst), ip.len, ip.ttl,
do_not_fragment, more_fragments, fragment_offset))
print('ICMP: type:%d code:%d checksum:%d data: %s\n' %
(icmp.type, icmp.code, icmp.sum, repr(icmp.data)))


def test():
Expand Down
36 changes: 5 additions & 31 deletions examples/print_packets.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,12 @@
#!/usr/bin/env python
"""
Use DPKT to read in a pcap file and print out the contents of the packets
This example is focused on the fields in the Ethernet Frame and IP packet
"""
import dpkt
import datetime
import socket
from dpkt.compat import compat_ord
from dpkt.utils import mac_to_str, inet_to_str


def mac_addr(address):
"""Convert a MAC address to a readable/printable string
Args:
address (str): a MAC address in hex form (e.g. '\x01\x02\x03\x04\x05\x06')
Returns:
str: Printable/readable MAC address
"""
return ':'.join('%02x' % compat_ord(b) for b in address)


def inet_to_str(inet):
"""Convert inet object to a string
Args:
inet (inet struct): inet network address
Returns:
str: Printable/readable IP address
"""
# First try ipv4 and then ipv6
try:
return socket.inet_ntop(socket.AF_INET, inet)
except ValueError:
return socket.inet_ntop(socket.AF_INET6, inet)

def print_packets(pcap):
"""Print out information about each packet in a pcap
Expand All @@ -48,7 +21,7 @@ def print_packets(pcap):

# Unpack the Ethernet frame (mac src/dst, ethertype)
eth = dpkt.ethernet.Ethernet(buf)
print('Ethernet Frame: ', mac_addr(eth.src), mac_addr(eth.dst), eth.type)
print('Ethernet Frame: ', mac_to_str(eth.src), mac_to_str(eth.dst), eth.type)

# Make sure the Ethernet data contains an IP packet
if not isinstance(eth.data, dpkt.ip.IP):
Expand All @@ -65,8 +38,9 @@ def print_packets(pcap):
fragment_offset = ip.off & dpkt.ip.IP_OFFMASK

# Print out the info
print('IP: %s -> %s (len=%d ttl=%d DF=%d MF=%d offset=%d)\n' % \
(inet_to_str(ip.src), inet_to_str(ip.dst), ip.len, ip.ttl, do_not_fragment, more_fragments, fragment_offset))
print('IP: %s -> %s (len=%d ttl=%d DF=%d MF=%d offset=%d)\n' %
(inet_to_str(ip.src), inet_to_str(ip.dst), ip.len, ip.ttl,
do_not_fragment, more_fragments, fragment_offset))


def test():
Expand Down

0 comments on commit 14c0110

Please sign in to comment.