Snippets
Check: sudo
import os
def in_sudo_mode():
"""If the user doesn't run the program with super user privileges, don't allow them to continue."""
if not 'SUDO_UID' in os.environ.keys():
print("Try running this program with sudo.")
exit()Arp Scan
def arp_scan(ip_range):
"""We use the arping method in scapy. It is a better implementation than writing your own arp scan. You'll often see that your own arp scan doesn't pick up
mobile devices. You can see the way scapy implemented the function here: https://github.com/secdev/scapy/blob/master/scapy/layers/l2.py#L726-L749
Arguments: ip_range -> an example would be "10.0.0.0/24"
"""
# We create an empty list where we will store the pairs of ARP responses.
arp_responses = list()
# We send arp packets through the network, verbose is set to 0 so it won't show any output.
# scapy's arping function returns two lists. We're interested in the answered results which is at the 0 index.
answered_lst = scapy.arping(ip_range, verbose=0)[0]
# We loop through all the responses and add them to a dictionary and append them to the list arp_responses.
for res in answered_lst:
# Every response will look something lke like -> {"ip" : "10.0.0.4", "mac" : "00:00:00:00:00:00"}
arp_responses.append({"ip" : res[1].psrc, "mac" : res[1].hwsrc})
# We return the list of arp responses which contains dictionaries for every arp response.
return arp_responsesGateway
def is_gateway(gateway_ip):
"""We can see the gateway by running the route -n command
Argument: The gateway_ip address which the program finds automatically should be supplied as an argument.
"""
# We run the command route -n which returns information about the gateways.
result = subprocess.run(["route", "-n"], capture_output=True).stdout.decode().split("\n")
# Loop through every row in the route -n command.
for row in result:
# We look to see if the gateway_ip is in the row, if it is we return True. If False program continues flow and returns False.
if gateway_ip in row:
return True
return False
def gateway_info(network_info):
"""We can see the gateway by running the route -n command. This get us the gateway information. We also need the name of the interface for the sniffer function.
Arguments: network_info -> We supply the arp_scan() data.
"""
# We run route -n and capture the output.
result = subprocess.run(["route", "-n"], capture_output=True).stdout.decode().split("\n")
# We declare an empty list for the gateways.
gateways = []
# We supplied the arp_scan() results (which is a list) as an argument to the network_info parameter.
for iface in network_info:
for row in result:
# We want the gateway information to be saved to list called gateways. We know the ip of the gateway so we can compare and see in which row it appears.
if iface["ip"] in row:
iface_name = match_iface_name(row)
# Once we found the gateway, we create a dictionary with all of its names.
gateways.append({"iface" : iface_name, "ip" : iface["ip"], "mac" : iface["mac"]})
return gatewaysInterface Name
def get_interface_names():
"""The interface names of a networks are listed in the /sys/class/net folder in Kali. This function returns a list of interfaces in Kali."""
# The interface names are directory names in the /sys/class/net folder. So we change the directory to go there.
os.chdir("/sys/class/net")
# We use the listdir() function from the os module. Since we know there won't be files and only directories with the interface names we can save the output as the interface names.
interface_names = os.listdir()
# We return the interface names which we will use to find out which one is the name of the gateway.
return interface_names
def match_iface_name(row):
# We get all the interface names by running the function defined above with the
interface_names = get_interface_names()
# Check if the interface name is in the row. If it is then we return the iface name.
for iface in interface_names:
if iface in row:
return ifaceLast updated