EvilZone

Programming and Scripting => Scripting Languages => : frog June 04, 2012, 10:22:43 AM

: mactool.sh - linux mac changer
: frog June 04, 2012, 10:22:43 AM
I think this is an important tool to have so I made one for myself and for anybody else who might want to use it.

: (bash)
#!/bin/bash
##
### mactool.sh - set random/designated/original mac address
##  Note: run as root
#
export MAC
export OLDMAC
export RANDMAC
export NEWMAC=$3
export TMP='/tmp/stored.mactool'
export IFACE=$2
export BASE16=('a' 'b' 'c' 'd' 'e' 'f' 0 1 2 3 4 5 6 7 8 9)

usage() {
  echo """
          mactool.sh - manipulate MAC address
              by frog on June 1st, 2012
    Usage:
             Set MAC: $0 -s <interface> <mac>
      Set Random MAC: $0 -r <interface>
    Set Original MAC: $0 -o <interface>
    Show Current MAC: $0 -w <interface>

      Example: $0 -s wlan1 00:11:22:aa:bb:cc
  """
  exit
}

showMAC() {
  MAC=`/sbin/ifconfig $IFACE | grep HW`
  MAC=`echo ${MAC:38:18}`
  echo "[+] MAC address for" $IFACE":" $MAC
}

saveOldMAC() {
  MAC=`ifconfig $IFACE | grep HW`
  MAC=`echo ${MAC:38:18}`
 
  if [ -a $TMP ]; then
    if [ `cat $TMP` != "$MAC" ]; then
      return
    fi
  fi
 
  OLDMAC=`ifconfig $IFACE | grep HW`
  OLDMAC=`echo ${OLDMAC:38:18}`
  echo "[-] Saving original MAC:" $OLDMAC "to" $TMP
  echo $OLDMAC > $TMP
}

setNewMAC() {
  echo "[-] Setting MAC address to" $NEWMAC
  ifconfig $IFACE down
  ifconfig $IFACE hw ether `echo $NEWMAC | sed 's/://g'`
  ifconfig $IFACE up
}

setRandomMAC() {
  for(( x=0; x<12; x++ )); do
    RANDMAC[$x]=`shuf -e ${BASE16[@]} -n 1`
  done
 
  RANDMAC=`echo ${RANDMAC[@]} | sed 's/ //g'`
  echo "[-] Setting Random MAC:" $RANDMAC
  ifconfig $IFACE down
  ifconfig $IFACE hw ether $RANDMAC 2> /dev/null
 
  if [ $? -gt 0 ]; then
    setRandomMAC
  fi

  ifconfig $IFACE up
}

restoreOldMAC() {
  if [ -z `cat $TMP 2> /dev/null` ]; then
    echo "[+] MAC address is original"
    exit
  fi

  echo "[-] Restoring MAC address to" `cat $TMP`
  ifconfig $IFACE down
  ifconfig $IFACE hw ether `cat $TMP | sed 's/://g'`
  ifconfig $IFACE up
}

checkForRoot() {
  # Make sure we are root(needed to manipulate interfaces)
  if [ `whoami` != 'root' ]; then
    echo "[-] You need to be root; try again.."
    exit
  fi
}

checkArgIface() {
  # Check for interface as argument 2
  if [ -z $IFACE ]; then
    echo "[-] Error: Set network interface"
    usage
    exit
  fi
}

checkArgMAC() {
  # Check for mac to set as argument 3
  if [ -z $NEWMAC ]; then
    echo "[-] Error: Set MAC address"
    usage
    exit
  fi
}
   
cleanup() {
  echo "[-] Deleting" $TMP
  rm -f $TMP
}

#
##
### Start

# Check our arguments meticulously..
if [ -z $1 ]; then
  usage
  exit
fi

# Check for option to set mac
if [ $1 = '-s' ]; then
  checkForRoot
  checkArgIface
  checkArgMAC
  saveOldMAC
  setNewMAC
  showMAC
  exit
fi

# Check for option to set random mac
if [ $1 = '-r' ]; then
  checkForRoot
  checkArgIface
  saveOldMAC
  setRandomMAC
  showMAC
  exit
fi

# Check for option to set mac to original
if [ $1 = '-o' ]; then
  checkForRoot
  checkArgIface 
  restoreOldMAC
  cleanup
  showMAC
  exit
fi

# Check for option to show mac
if [ $1 = '-w' ]; then
  checkArgIface
  showMAC
  exit
fi

### End
##
#
: Re: mactool.sh - linux mac changer
: lucid June 05, 2012, 01:13:04 AM
Nice tool. Have you heard of macchanger?
: Re: mactool.sh - linux mac changer
: frog June 05, 2012, 09:26:52 AM
Nice tool. Have you heard of macchanger?

yea, and thank you for the compliment. I know there's better mac-changing programs out there written in c but I wanted one I could call my own. Mac-changing code is platform specific so I thought ' fuck it, lets make a bash script that does this using native linux commands.'

It doesn't fall very short when comparing features. The only features I would want to add would be the ability to set vendor-specific mac addresses and the ability to clone the mac of another interface.