Pages

24 October 2011

FuCkMAC v.0.1

#!/usr/bin/env python
#
######################################################
# FuCkMAC.py is a script to change the MAC addres
# on *nix using 'ifconfig' tool..
#
# D4wFl1N[at]deadc0de[dot]or[dot]id
######################################################
#
#

import sys
import os
import socket
import fcntl
import struct
import array
import getopt
import platform

# print banner
def Banner():
    print """
################################
## FuCkMAC v0.1                ##
##
D4wFl1N[at]deadc0de[dot]or[dot]id##
################################
"""

# check os
def CheckOS():
    OS = platform.system()
    if OS != 'Linux':
        print "[-] Warning you'r not using Linux"

# check the user if root or not
def CheckRoot():
    if os.getuid() & os.getgid() != 0:
        Banner()
        print "[-] Your have to be root"
        sys.exit(0)

# set the mac address
def SetMAC(device,mac):
    os.system("ifconfig %s down" % device)
    os.system("ifconfig %s hw ether %s" % (device,mac))
    os.system("ifconfig %s up" % device)

# get names of all "up" network interfaces
def GetInterfaces():
    max_possible = 128  # arbitrary. raise if needed.
    bytes = max_possible * 32
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    names = array.array('B', '\0' * bytes)
    outbytes = struct.unpack('iL', fcntl.ioctl(
        s.fileno(),
        0x8912,  # SIOCGIFCONF
        struct.pack('iL', bytes, names.buffer_info()[0])
    ))[0]
    namestr = names.tostring()
    return [namestr[i:i+32].split('\0', 1)[0] for i in range(0, outbytes, 32)]

# Useage
def Usage():
    Banner()
    print """%s\n
-h\t\t: print this message
-l\t\t: list up interfaces
-i \t: select interface
-a

\t: change MAC address   
""" % (sys.argv[0])

######################################
################ MAIN ################
######################################

CheckOS()
CheckRoot()

if len(sys.argv) < 2:
    Usage()
    sys.exit(0)

try:
    opts, args = getopt.getopt(sys.argv[1:], "la:i:h")
except getopt.GetoptError, err:
    print str(err)
    sys.exit(2)

interfaces = GetInterfaces()

Address        = ""
Interface    = ""

for o, a in opts:
    if o == "-h":
        Usage()
        sys.exit(0)
    elif o == "-l":
        print "Available interfaces :"
        for device_name in interfaces:
            print device_name
        sys.exit(0)
    elif o == "-i":
        Interface = a
        for device_name in interfaces:
            if Interface not in interfaces:
                print "[-]",Interface,"is invalid interface."
                sys.exit(0)
        print "[*] Interface:", Interface
    elif o == "-a":
        Address = a
        if len(Address) != 17:
            print "[-] \"",Address,"\" is invalid MAC address."
            sys.exit(0)
        print "[*] FuCk MACaddr:", Address
    else:
        assert False, "unhandled option"

if len(Address) < 1:
    print "[-] You have to enter the fuCk MAC address try '%s -h' for help" % (sys.argv[0])
    sys.exit(0)
elif len(Interface) < 1:
    print "[-] You have to enter the interface name try '%s -h' for help" % (sys.argv[0])
    sys.exit(0)

SetMAC(Interface, Address)

print "[*] Done"

0 comments: