Skip to content

Instantly share code, notes, and snippets.

@WJDigby
Last active March 28, 2018 01:34
Show Gist options
  • Save WJDigby/3589add1a8ce1f584ef8c265043dd24a to your computer and use it in GitHub Desktop.
Save WJDigby/3589add1a8ce1f584ef8c265043dd24a to your computer and use it in GitHub Desktop.
import argparse
from subprocess import call
# Useful for Bluetooth device discovery when Bluetooth device addresses may be one off from wireless MAC addresses
# See, for example, "Hacking Exposed: Wireless", 3rd edition, by Joshua Wright and Johnny Cache, pages 211-214.
# To generate the list of addresses and test using hcitool (or another command line tool):
# python3 off-by-one.py -l macs.lst | while read -r line; do hcitool name "$line"; done
def off_by_one(mac_list, flag):
with open(mac_list) as f:
lines = f.readlines()
for line in lines:
mac = line.rstrip()
first_octets = mac[:15] # Assumes a single character delimeter between octets (e.g. 00:11:22:33:44:55 or 00-11-22-33-44-55)
last_octet = mac[-2:]
if flag == 0: # Do both operations
if last_octet.upper() == "FF": pass
else:
print("{}{:02x}".format(first_octets, int(last_octet, 16) + 1))
if last_octet == "00": pass
else:
print("{}{:02x}".format(first_octets, int(last_octet, 16) -1))
elif flag == 1: # Only do plus one
if last_octet.upper() == "FF": pass
else:
print("{}{:02x}".format(first_octets, int(last_octet, 16) + 1))
elif flag == 2: # Only do minus one
if last_octet == "00": pass
else:
print("{}{:02x}".format(first_octets, int(last_octet, 16) - 1))
def main():
parser = argparse.ArgumentParser(description='Given a list of MAC addresses, produce a list of addresses one higher and one lower.')
parser.add_argument('-l', '--list', dest='mac_list', required=True, help='File containing list of MAC addresses, separated by line.')
parser.add_argument('-p', '--plus-only', dest='plus_only', action='store_true', help='Only list MAC addresses one higher.')
parser.add_argument('-m', '--minus-only', dest='minus_only', action='store_true', help='Only list MAC addresses one lower.')
args = parser.parse_args()
mac_list = args.mac_list
plus_only = args.plus_only
minus_only = args.minus_only
if plus_only == False and minus_only == False:
flag = 0
elif plus_only == True and minus_only == False:
flag = 1
elif plus_only == False and minus_only == True:
flag = 2
elif plus_only == True and minus_only == True:
print("The options '--plus-only' and '--minus-only' are mutually exclusie. Select only one.")
exit()
off_by_one(mac_list, flag)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment