Skip to content

Commit

Permalink
Merge pull request #37 from pteek/master
Browse files Browse the repository at this point in the history
new fixes
  • Loading branch information
pteek committed Dec 4, 2014
2 parents c55dfc7 + b83e0e1 commit 92b2684
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 11 deletions.
136 changes: 136 additions & 0 deletions scripts/spoof-mac
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""SpoofMAC
Usage:
spoof-mac list [--wifi]
spoof-mac randomize [--local] <devices>...
spoof-mac set <mac> <devices>...
spoof-mac reset <devices>...
spoof-mac normalize <mac>
spoof-mac -h | --help
spoof-mac --version
Options:
-h --help Shows this message.
--version Show package version.
--wifi Try to only show wireless interfaces.
--local Set the locally administered flag on randomized MACs.
"""
import sys
import os

if sys.platform == 'win32':
import ctypes

from docopt import docopt

from spoofmac.version import __version__
from spoofmac.util import random_mac_address, MAC_ADDRESS_R, normalize_mac_address

from spoofmac.interface import (
wireless_port_names,
find_interfaces,
find_interface,
set_interface_mac,
get_os_spoofer
)

# Return Codes
SUCCESS = 0
INVALID_ARGS = 1001
UNSUPPORTED_PLATFORM = 1002
INVALID_TARGET = 1003
INVALID_MAC_ADDR = 1004
NON_ROOT_USER = 1005


def list_interfaces(args, spoofer):
targets = []

# Should we only return prospective wireless interfaces?
if args['--wifi']:
targets += wireless_port_names

for port, device, address, current_address in spoofer.find_interfaces(targets=targets):
line = []
line.append('- "{port}"'.format(port=port))
line.append('on device "{device}"'.format(device=device))
if address:
line.append('with MAC address {mac}'.format(mac=address))
if current_address and address != current_address:
line.append('currently set to {mac}'.format(mac=current_address))
print(' '.join(line))


def main(args, root_or_admin):
spoofer = None

try:
spoofer = get_os_spoofer()
except NotImplementedError:
return UNSUPPORTED_PLATFORM

if args['list']:
list_interfaces(args, spoofer)
elif args['randomize'] or args['set'] or args['reset']:
for target in args['<devices>']:
# Fill out the details for `target`, which could be a Hardware
# Port or a literal device.
#print("Debuf:",target)
result = find_interface(target)
if result is None:
print('- couldn\'t find the device for {target}'.format(
target=target
))
return INVALID_TARGET

port, device, address, current_address = result
if args['randomize']:
target_mac = random_mac_address(args['--local'])
elif args['set']:
target_mac = args['<mac>']
elif args['reset']:
if address is None:
print('- {target} missing hardware MAC'.format(
target=target
))
continue
target_mac = address

if not MAC_ADDRESS_R.match(target_mac):
print('- {mac} is not a valid MAC address'.format(
mac=target_mac
))
return INVALID_MAC_ADDR

if not root_or_admin:
if sys.platform == 'win32':
print('Error: Must run this with administrative privileges to set MAC addresses')
return NON_ROOT_USER
else:
print('Error: Must run this as root (or with sudo) to set MAC addresses')
return NON_ROOT_USER

set_interface_mac(device, target_mac, port)
elif args['normalize']:
print(normalize_mac_address(args['<mac>']))

else:
print('Error: Invalid arguments - check help usage')
return INVALID_ARGS

del spoofer

return SUCCESS


if __name__ == '__main__':
arguments = docopt(__doc__, version=__version__)
try:
root_or_admin = os.geteuid() == 0
except AttributeError:
root_or_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0

sys.exit(main(arguments, root_or_admin))
2 changes: 1 addition & 1 deletion scripts/spoof-mac.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# -*- coding: utf-8 -*-
"""SpoofMAC
Usage:
Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# -*- coding: utf-8 -*-
"""
Easily spoof your MAC address in OS X, Windows & Linux.
"""
Expand Down Expand Up @@ -30,7 +30,8 @@ def get_version():
'docopt'
],
scripts=[
'scripts/spoof-mac.py'
'scripts/spoof-mac.py',
'scripts/spoof-mac'
],
license='MIT'
)
2 changes: 1 addition & 1 deletion spoofmac/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# -*- coding: utf8 -*-
# -*- coding: utf-8 -*-
from spoofmac.util import *
from spoofmac.interface import *
10 changes: 5 additions & 5 deletions spoofmac/interface.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf8 -*-
# -*- coding: utf-8 -*-
__all__ = (
'find_interfaces',
'find_interface',
Expand Down Expand Up @@ -45,7 +45,7 @@ class LinuxSpoofer(OsSpoofer):
Linux platform specfic implementation for MAC spoofing.
"""
def get_interface_mac(self, device):
result = subprocess.check_output(["ifconfig", device], stderr=subprocess.STDOUT)
result = subprocess.check_output(["ifconfig", device], stderr=subprocess.STDOUT, universal_newlines=True)
m = re.search("(?<=HWaddr\\s)(.*)", result)
if not hasattr(m, "group") or m.group(0) == None:
return None
Expand All @@ -63,7 +63,7 @@ def find_interfaces(self, targets=None):
# - the adapter name/device associated with this, if any,
# - the MAC address, if any

output = subprocess.check_output(["ifconfig"], stderr=subprocess.STDOUT)
output = subprocess.check_output(["ifconfig"], stderr=subprocess.STDOUT, universal_newlines=True)

# search for specific adapter gobble through mac address
details = re.findall("(.*?)HWaddr(.*)", output, re.MULTILINE)
Expand Down Expand Up @@ -310,7 +310,7 @@ def find_interfaces(self, targets=None):
subprocess.check_output((
'networksetup',
'-listallhardwareports'
)), re.MULTILINE
), universal_newlines=True), re.MULTILINE
)
# Split the results into chunks of 3 (for our three fields) and yield
# those that match `targets`.
Expand Down Expand Up @@ -390,7 +390,7 @@ def get_interface_mac(self, device):
result = subprocess.check_output([
'ifconfig',
device
], stderr=subprocess.STDOUT)
], stderr=subprocess.STDOUT, universal_newlines=True)
except subprocess.CalledProcessError:
return None

Expand Down
4 changes: 2 additions & 2 deletions spoofmac/util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf8 -*-
# -*- coding: utf-8 -*-
__all__ = ('MAC_ADDRESS_R', 'random_mac_address')
import re
import random
Expand Down Expand Up @@ -98,4 +98,4 @@ def normalise_mac_address_windows(mac):
if m:
return '-'.join([g.zfill(2) for g in m.groups()]).upper()

return None
return None

0 comments on commit 92b2684

Please sign in to comment.