Skip to content

Instantly share code, notes, and snippets.

@ouoam
Created April 15, 2021 12:24
Show Gist options
  • Select an option

  • Save ouoam/c5b38a953e59cc481bd8841cf613e20e to your computer and use it in GitHub Desktop.

Select an option

Save ouoam/c5b38a953e59cc481bd8841cf613e20e to your computer and use it in GitHub Desktop.
live capture arp python pyshark
"""
Phumphathai Chansriwong 61010827
"""
import pyshark
import sys
import os
class colors:
"""
Colors class:reset all colors with colors.reset; two
sub classes fg for foreground
and bg for background; use as colors.subclass.colorname.
i.e. colors.fg.red or colors.bg.greenalso, the generic bold, disable,
underline, reverse, strike through,
and invisible work with the main class i.e. colors.bold
"""
reset='\033[0m'
bold='\033[01m'
disable='\033[02m'
underline='\033[04m'
reverse='\033[07m'
strikethrough='\033[09m'
invisible='\033[08m'
class fg:
black='\033[30m'
red='\033[31m'
green='\033[32m'
orange='\033[33m'
blue='\033[34m'
purple='\033[35m'
cyan='\033[36m'
lightgrey='\033[37m'
darkgrey='\033[90m'
lightred='\033[91m'
lightgreen='\033[92m'
yellow='\033[93m'
lightblue='\033[94m'
pink='\033[95m'
lightcyan='\033[96m'
class bg:
black='\033[40m'
red='\033[41m'
green='\033[42m'
orange='\033[43m'
blue='\033[44m'
purple='\033[45m'
cyan='\033[46m'
lightgrey='\033[47m'
capture = pyshark.LiveCapture(interface='Ethernet', bpf_filter='arp')
print(colors.fg.lightred + 'light red' + colors.reset, '=>', 'Send by')
print(colors.fg.lightblue + 'light blue' + colors.reset, '=>', 'Ask for')
print(colors.fg.green + 'green' + colors.reset, '=>', 'Ans MAC')
print()
print('Source'.center(19, ' ') + '|' + 'Destination'.center(19, ' ') + '|', 'Info')
print('-'*19 + '+' + '-'*19 + '+' + '-'*50)
try:
for pkt in capture.sniff_continuously():
src = pkt.eth.src
dst = pkt.eth.dst
info = ''
if (dst == 'ff:ff:ff:ff:ff:ff'):
dst = 'Broadcast'
src_ip = colors.fg.lightred + pkt.arp.src_proto_ipv4 + colors.reset
src_mac = colors.fg.green + pkt.arp.src_hw_mac + colors.reset
dst_ip = colors.fg.lightblue + pkt.arp.dst_proto_ipv4 + colors.reset
if pkt.arp.opcode == '1': #request
if pkt.arp.src_proto_ipv4 == '0.0.0.0':
info = 'Who has %s? (ARP Probe)' % (dst_ip)
elif pkt.arp.src_proto_ipv4 == pkt.arp.dst_proto_ipv4:
info = 'ARP Announcement for %s' % (src_ip)
else:
info = 'Who has %s? Tell %s' % (dst_ip, src_ip)
elif pkt.arp.opcode == '2': #reply
info = '%s is at %s' % (src_ip, src_mac)
print (src.center(19, ' ') + '|' + dst.center(19, ' ') + '|', info)
except KeyboardInterrupt:
print()
print('Hello user you have pressed ctrl-c button.')
try:
sys.exit(0)
except SystemExit:
os._exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment