diff --git a/iroha-drip-general-master/bluetooth/beacon.py b/iroha-drip-general-master/bluetooth/beacon.py index 87b6a9eb20676bd557e29b85c6180b5a692aa20c..d124436cfa5edf280bf0740aa00fbcd7f8ac9beb 100644 --- a/iroha-drip-general-master/bluetooth/beacon.py +++ b/iroha-drip-general-master/bluetooth/beacon.py @@ -21,6 +21,9 @@ from drippy.gps_poll import GpsPoller from drippy.util import astm_float_to_int_hex, get_timestamp, astm_time from drippy.host_identity import HostIdentity +from wifi_beacon import beacon_wifi +from uuid import getnode as get_mac + # File with flight_id that will be incremented and used if no other # flight_id is specified. FLIGHT_ID_FILE = os.path.join(FILE_DIR, "default_flight_id.txt") @@ -105,88 +108,100 @@ def location_update(gpsd: GpsPoller, iroha: DripIrohaAccount, flight_id): iroha.set_account_details, "location", iroha_msg ) - # Set (Extended) Advertising command - if is_bt5: - """ - OGF: 0x08 (LE controller commands) - OCF: 0x0037 (LE Set Extended Advertising Data command) - Advertising handle: 0x00 (Advertising set number) - Operation: 0x03 (Complete extended advertising data) - Fragment_Preference: 0x01 (The Controller should not fragment or should minimize fragmentation of Host advertising data) - Advertising_Data_Length: 0x1f - Advertising_Data: 1e 16 fa ff 0d 00 10 + payload - """ - subprocess.run( - "hcitool -i hci0 cmd 0x08 0x0037 0x00 0x03 0x01 0x1f 0x1e 0x16 0xfa 0xff 0x0d 0x00 0x10 {payload}".format(payload=payload), shell=True) + if is_wifi: + beacon_wifi(mac_arr, "0x08 0x0037 0x00 0x03 0x01 0x1f 0x1e 0x16 0xfa 0xff 0x0d 0x00 0x10 {payload}".format(payload=payload)) else: - """ - OGF: 0x08 (LE controller commands) - OCF: 0x0008 (LE Set Advertising Data command) - Advertising_Data_Length: 0x1f - Advertising_Data: 1e 16 fa ff 0d 00 10 + payload - """ - subprocess.run( - "hcitool -i hci0 cmd 0x08 0x0008 1f 1e 16 fa ff 0d 00 10 {payload}".format(payload=payload), shell=True) + # Set (Extended) Advertising command + if is_bt5: + """ + OGF: 0x08 (LE controller commands) + OCF: 0x0037 (LE Set Extended Advertising Data command) + Advertising handle: 0x00 (Advertising set number) + Operation: 0x03 (Complete extended advertising data) + Fragment_Preference: 0x01 (The Controller should not fragment or should minimize fragmentation of Host advertising data) + Advertising_Data_Length: 0x1f + Advertising_Data: 1e 16 fa ff 0d 00 10 + payload + """ + subprocess.run( + "hcitool -i hci0 cmd 0x08 0x0037 0x00 0x03 0x01 0x1f 0x1e 0x16 0xfa 0xff 0x0d 0x00 0x10 {payload}".format(payload=payload), shell=True) + else: + """ + OGF: 0x08 (LE controller commands) + OCF: 0x0008 (LE Set Advertising Data command) + Advertising_Data_Length: 0x1f + Advertising_Data: 1e 16 fa ff 0d 00 10 + payload + """ + subprocess.run( + "hcitool -i hci0 cmd 0x08 0x0008 1f 1e 16 fa ff 0d 00 10 {payload}".format(payload=payload), shell=True) def basic_id_update(hhit): # ASTM F3-411 Basic ID message 0x0 with HHIT. print("basic_id_update") # Set (Extended) Advertising Command - if is_bt5: - """ - OGF: 0x08 (LE controller commands) - OCF: 0x0037 (LE Set Extended Advertising Data command) - Advertising handle: 0x00 (Advertising set number) - Operation: 0x03 (Complete extended advertising data) - Fragment_Preference: 0x01 (The Controller should not fragment or should minimize fragmentation of Host advertising data) - Advertising_Data_Length: 0x1f - Advertising_Data: 1e 16 fa ff 0d 00 10 + hhit - """ - subprocess.run( - "hcitool -i hci0 cmd 0x08 0x0037 0x00 0x03 0x01 0x1f 0x1e 0x16 0xfa 0xff 0x0d 0x00 0x00 0x42 {} 0x00 0x00 0x00 0x00 0x00 0x00 0x00".format(hhit), shell=True) + if is_wifi: + beacon_wifi(mac_arr, "0x08 0x0037 0x00 0x03 0x01 0x1f 0x1e 0x16 0xfa 0xff 0x0d 0x00 0x00 0x42 {} 0x00 0x00 0x00 0x00 0x00 0x00 0x00".format(hhit)) else: - """ - OGF: 0x08 (LE controller commands) - OCF: 0x0008 (LE Set Advertising Data command) - Advertising_Data_Length: 0x1f - Advertising_Data: 1e 16 fa ff 0d 00 00 42 + hhit - """ - subprocess.run( - "hcitool -i hci0 cmd 0x08 0x0008 1f 1e 16 fa ff 0d 00 00 42 {} 00 00 00 00 00 00 00".format(hhit), shell=True) - time.sleep(0.5) - - -def auth_update(auth_pages, msg_number): - print("auth_update") - for page in auth_pages: - msg = separate_bytes(hexlify(page).decode("utf-8")) - # Set (Extended) Advertising Command if is_bt5: """ OGF: 0x08 (LE controller commands) OCF: 0x0037 (LE Set Extended Advertising Data command) Advertising handle: 0x00 (Advertising set number) - Operation: 0x01 / 0x02 (First and last part of advertising data) + Operation: 0x03 (Complete extended advertising data) Fragment_Preference: 0x01 (The Controller should not fragment or should minimize fragmentation of Host advertising data) Advertising_Data_Length: 0x1f - Advertising_Data: 1e 16 fa ff 0d 00 20 + msg + Advertising_Data: 1e 16 fa ff 0d 00 10 + hhit """ - if msg_number == 1: - subprocess.run( - "hcitool -i hci0 cmd 0x08 0x0037 0x00 0x01 0x01 0x1f 0x1e 0x16 0xfa 0xff 0x0d {} 0x20 {}".format("00", msg), shell=True) - elif msg_number == 2: - subprocess.run( - "hcitool -i hci0 cmd 0x08 0x0037 0x00 0x02 0x01 0x1f 0x1e 0x16 0xfa 0xff 0x0d {} 0x20 {}".format("00", msg), shell=True) + subprocess.run( + "hcitool -i hci0 cmd 0x08 0x0037 0x00 0x03 0x01 0x1f 0x1e 0x16 0xfa 0xff 0x0d 0x00 0x00 0x42 {} 0x00 0x00 0x00 0x00 0x00 0x00 0x00".format(hhit), shell=True) else: """ OGF: 0x08 (LE controller commands) OCF: 0x0008 (LE Set Advertising Data command) Advertising_Data_Length: 0x1f - Advertising_Data: 1e 16 fa ff 0d 00 20 + msg + Advertising_Data: 1e 16 fa ff 0d 00 00 42 + hhit """ subprocess.run( - "hcitool -i hci0 cmd 0x08 0x0008 1f 1e 16 fa ff 0d {} 20 {}".format("00", msg), shell=True) + "hcitool -i hci0 cmd 0x08 0x0008 1f 1e 16 fa ff 0d 00 00 42 {} 00 00 00 00 00 00 00".format(hhit), shell=True) + time.sleep(0.5) + + +def auth_update(auth_pages, msg_number): + print("auth_update") + for page in auth_pages: + msg = separate_bytes(hexlify(page).decode("utf-8")) + if is_wifi: + if msg_number == 1: + beacon_wifi(mac_arr, "0x08 0x0037 0x00 0x01 0x01 0x1f 0x1e 0x16 0xfa 0xff 0x0d {} 0x20 {}".format("00", msg)) + elif msg_number == 2: + beacon_wifi(mac_arr, "0x08 0x0037 0x00 0x02 0x01 0x1f 0x1e 0x16 0xfa 0xff 0x0d {} 0x20 {}".format("00", msg)) + else: + # Set (Extended) Advertising Command + if is_bt5: + """ + OGF: 0x08 (LE controller commands) + OCF: 0x0037 (LE Set Extended Advertising Data command) + Advertising handle: 0x00 (Advertising set number) + Operation: 0x01 / 0x02 (First and last part of advertising data) + Fragment_Preference: 0x01 (The Controller should not fragment or should minimize fragmentation of Host advertising data) + Advertising_Data_Length: 0x1f + Advertising_Data: 1e 16 fa ff 0d 00 20 + msg + """ + if msg_number == 1: + subprocess.run( + "hcitool -i hci0 cmd 0x08 0x0037 0x00 0x01 0x01 0x1f 0x1e 0x16 0xfa 0xff 0x0d {} 0x20 {}".format("00", msg), shell=True) + elif msg_number == 2: + subprocess.run( + "hcitool -i hci0 cmd 0x08 0x0037 0x00 0x02 0x01 0x1f 0x1e 0x16 0xfa 0xff 0x0d {} 0x20 {}".format("00", msg), shell=True) + else: + """ + OGF: 0x08 (LE controller commands) + OCF: 0x0008 (LE Set Advertising Data command) + Advertising_Data_Length: 0x1f + Advertising_Data: 1e 16 fa ff 0d 00 20 + msg + """ + subprocess.run( + "hcitool -i hci0 cmd 0x08 0x0008 1f 1e 16 fa ff 0d {} 20 {}".format("00", msg), shell=True) time.sleep(0.2) @@ -207,6 +222,10 @@ def main(): global g_lat global g_lon global is_bt5 + + global is_wifi + global mac_arr + parser = argparse.ArgumentParser(description=DESCRIPTION) parser.add_argument( "-f", "--file", @@ -229,6 +248,13 @@ def main(): type=int, help="Specify the bluetooth version, 4 or 5 (Default: 4)." ) + parser.add_argument( + "--wifi", + dest="activate_wifi" + default= False, + action= "store_true" + type= bool + ) parser.add_argument( "--gps", help="Turn on gps updates", @@ -258,6 +284,7 @@ def main(): hi_file = args.host_identity_file iroha_priv_key_file = args.iroha_priv_key_file is_bt5 = True if args.bluetooth_version == 5 else False + is_wifi= args.activate_wifi exp_date = datetime.strptime(args.exp_date, "%Y-%m-%dT%H:%M") if (exp_date - datetime.utcnow()).total_seconds() <= 0: @@ -296,67 +323,75 @@ def main(): auth_pages = get_auth_pages(attest) - # Reset bt - subprocess.run("hcitool -i hci0 cmd 0x03 0x0003", shell=True) - - # Set (Extended) Advertising Parameters - if is_bt5: - """ - OGF: 0x08 (LE controller commands) - OCF: 0x0036 (LE Set Extended Advertising Parameters command) - Advertising handle: 0x00 (Advertising set number) - Advertising_Event_Properties: 0x0000 (Non-connectable, non-scannable undirected and not legacy PDU) - Primary_Advertising_Interval_Min: 0xa00000 (100 ms) - Primary_Advertising_Interval_Max: 0xa00000 (100 ms) - Primary_Advertising_Channel_Map: 0x07 (All three channels enabled) - Own_Address_Type: 0x00 (Public device address) - Peer_Address_Type: 0x00 (Public Device Address or Public Identity Address) - Peer_Address: 0x000000000000 - Advertising_Filter_Policy: 0x02 (Process scan requests: all devices, Connection requests: only Filter Accept List) - Advertising_TX_Power: 0x7f (Host has no preference) - Primary_Advertising_PHY: 0x03 (Primary advertisement PHY is LE Coded) - Secondary_Advertising_Max_Skip: 0x00 (AUX_ADV_IND shall be sent prior to the next advertising event) - Secondary_Advertising_PHY: 0x03 (Secondary advertisement PHY is LE Coded) - Advertising_SID: 0x00 (Value of the Advertising SID subfield in the ADI field of the PDU) - Scan_Request_Notification_Enable: 0x00 (Scan request notifications disabled) - """ - subprocess.run( - "hcitool -i hci0 cmd 0x08 0x0036 0x00 0x00 0x00 0xa0 0x00 0x00 0xa0 0x00 0x00 0x07 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x7f 0x03 0x00 0x03 0x00 0x00", shell=True) - else: - """ - OGF: 0x08 (LE controller commands) - OCF: 0x0006 (LE Set Advertising Parameters command) - Advertising_Interval_Min: 0xa000 (100 ms) - Advertising_Interval_Max: 0xa000 (100 ms) - Advertising_Type: 0x03 (Non connectable undirected advertising (ADV_NONCONN_IND)) - Own_Address_Type: 0x00 (Public device address) - Peer_Address_Type: 0x00 (Public Device Address or Public Identity Address) - Peer_Address: 0x000000000000 - Advertising_Channel_Map: 0x07 (All three channels enabled) - Advertising_Filter_Policy: 0x02 (Process scan requests: all devices, Connection requests: only Filter Accept List) - """ - subprocess.run( - "hcitool -i hci0 cmd 0x08 0x0006 a0 00 a0 00 03 00 00 00 00 00 00 00 00 07 02", shell=True) - - # Enable (Extended) Bluetooth Advertisement - if is_bt5: - """ - OGF: 0x08 (LE controller commands) - OCF: 0x0039 (LE Set Extended Advertising Enable command) - Enable: 0x01 (True) - Num_Sets: 0x01 (Number of advertising sets to enable) - Advertising_Handle[i]: 0x00 (Advertising set's handles) - Duration[i]: 0x0000 (Advertise until the Host disables it) - Max_Extended_Advertising_Events[i]: 0x00 (No maximum) - """ - subprocess.run("hcitool -i hci0 cmd 0x08 0x0039 0x01 0x01 0x00 0x00 0x00 0x00", shell=True) + mac = hex(get_mac()) # fetches device mac address + mac_arr = list(bytearray.fromhex(mac[2:])) # converts it into int + + if is_wifi: + beacon_wifi(mac_arr, "0x03 0x0003") + beacon_wifi(mac_arr, "0x08 0x0036 0x00 0x00 0x00 0xa0 0x00 0x00 0xa0 0x00 0x00 0x07 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x7f 0x03 0x00 0x03 0x00 0x00") + beacon_wifi(mac_arr, "0x08 0x0039 0x01 0x01 0x00 0x00 0x00 0x00") else: - """ - OGF: 0x08 (LE controller commands) - OCF: 0x000a (LE Set Advertising Enable command) - Enable: 0x01 (True) - """ - subprocess.run("hcitool -i hci0 cmd 0x08 0x000a 01", shell=True) + # Reset bt + subprocess.run("hcitool -i hci0 cmd 0x03 0x0003", shell=True) + + # Set (Extended) Advertising Parameters + if is_bt5: + """ + OGF: 0x08 (LE controller commands) + OCF: 0x0036 (LE Set Extended Advertising Parameters command) + Advertising handle: 0x00 (Advertising set number) + Advertising_Event_Properties: 0x0000 (Non-connectable, non-scannable undirected and not legacy PDU) + Primary_Advertising_Interval_Min: 0xa00000 (100 ms) + Primary_Advertising_Interval_Max: 0xa00000 (100 ms) + Primary_Advertising_Channel_Map: 0x07 (All three channels enabled) + Own_Address_Type: 0x00 (Public device address) + Peer_Address_Type: 0x00 (Public Device Address or Public Identity Address) + Peer_Address: 0x000000000000 + Advertising_Filter_Policy: 0x02 (Process scan requests: all devices, Connection requests: only Filter Accept List) + Advertising_TX_Power: 0x7f (Host has no preference) + Primary_Advertising_PHY: 0x03 (Primary advertisement PHY is LE Coded) + Secondary_Advertising_Max_Skip: 0x00 (AUX_ADV_IND shall be sent prior to the next advertising event) + Secondary_Advertising_PHY: 0x03 (Secondary advertisement PHY is LE Coded) + Advertising_SID: 0x00 (Value of the Advertising SID subfield in the ADI field of the PDU) + Scan_Request_Notification_Enable: 0x00 (Scan request notifications disabled) + """ + subprocess.run( + "hcitool -i hci0 cmd 0x08 0x0036 0x00 0x00 0x00 0xa0 0x00 0x00 0xa0 0x00 0x00 0x07 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x7f 0x03 0x00 0x03 0x00 0x00", shell=True) + else: + """ + OGF: 0x08 (LE controller commands) + OCF: 0x0006 (LE Set Advertising Parameters command) + Advertising_Interval_Min: 0xa000 (100 ms) + Advertising_Interval_Max: 0xa000 (100 ms) + Advertising_Type: 0x03 (Non connectable undirected advertising (ADV_NONCONN_IND)) + Own_Address_Type: 0x00 (Public device address) + Peer_Address_Type: 0x00 (Public Device Address or Public Identity Address) + Peer_Address: 0x000000000000 + Advertising_Channel_Map: 0x07 (All three channels enabled) + Advertising_Filter_Policy: 0x02 (Process scan requests: all devices, Connection requests: only Filter Accept List) + """ + subprocess.run( + "hcitool -i hci0 cmd 0x08 0x0006 a0 00 a0 00 03 00 00 00 00 00 00 00 00 07 02", shell=True) + + # Enable (Extended) Bluetooth Advertisement + if is_bt5: + """ + OGF: 0x08 (LE controller commands) + OCF: 0x0039 (LE Set Extended Advertising Enable command) + Enable: 0x01 (True) + Num_Sets: 0x01 (Number of advertising sets to enable) + Advertising_Handle[i]: 0x00 (Advertising set's handles) + Duration[i]: 0x0000 (Advertise until the Host disables it) + Max_Extended_Advertising_Events[i]: 0x00 (No maximum) + """ + subprocess.run("hcitool -i hci0 cmd 0x08 0x0039 0x01 0x01 0x00 0x00 0x00 0x00", shell=True) + else: + """ + OGF: 0x08 (LE controller commands) + OCF: 0x000a (LE Set Advertising Enable command) + Enable: 0x01 (True) + """ + subprocess.run("hcitool -i hci0 cmd 0x08 0x000a 01", shell=True) gpsd = None if not args.run_scenario and args.gps: @@ -392,8 +427,9 @@ def main(): i += 1 time.sleep(1 - time.monotonic() % 1) except KeyboardInterrupt: - # Reset bt - subprocess.run("hcitool -i hci0 cmd 0x03 0x0003", shell=True) + if not is_wifi: + # Reset bt + subprocess.run("hcitool -i hci0 cmd 0x03 0x0003", shell=True) if __name__ == '__main__': diff --git a/iroha-drip-general-master/bluetooth/wifi_beacon.py b/iroha-drip-general-master/bluetooth/wifi_beacon.py new file mode 100644 index 0000000000000000000000000000000000000000..4ebaec1acd1b5672114bfec061f1d0576809ffae --- /dev/null +++ b/iroha-drip-general-master/bluetooth/wifi_beacon.py @@ -0,0 +1,68 @@ +from scapy.layers.dot11 import Dot11, Dot11Beacon, RadioTap, sendp, Dot11Elt +# from scapy.all import hexdump # Used in commented code so uncomment when you uncomment the code +from uuid import getnode as get_mac +import os +import sys +import psutil + +# The following is used to ensure that there is no issues with finding any local packages. +# Mainly used for accessing python-nan once upon a time but could still be useful now. +file_dir = os.path.dirname(__file__) +sys.path.append(file_dir) +sys.path.append(os.path.join(os.path.dirname(__file__), '..')) +sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) + + +iface = 'Wi-Fi' # This is the wifi interface on the drone, Wi-Fi seems to be a common windows name +# while linux seems to use wlan0 so the following checks if wlan0 is an interface then switches to that in that case. + +addrs = psutil.net_if_addrs() +if "wlan0" in addrs.keys(): + iface = "wlan0" + + +def beacon_wifi(mac_address, element_info): + """ + Broadcasts a wifi beacon from mac_address with the SSID as the given element_info. + :param mac_address: A array of integer values representing the devices mac_address + :param element_info: The info needed sent + :return: None + """ + dot11 = Dot11(type=0, subtype=8, addr1='ff:ff:ff:ff:ff:ff', + addr2=mac_address, addr3=mac_address) + + dot_11_beacon = Dot11Beacon() + # Beacon that claims it needs a password for access + # dot_11_beacon = Dot11Beacon(cap='ESS+privacy') + + # Actual information that should be sent + dot_11_element = Dot11Elt(ID='SSID', info=element_info, len=len(element_info)) + # Test to ensure easy debug + # dot_11_element = Dot11Elt(ID='SSID', info='fakeTestNetwork', len=len('fakeTestNetwork')) + + frame = RadioTap()/dot11 / dot_11_beacon / dot_11_element + # Following block adds fake security to the access point when detected so it will say on other devices + # that it needs a password. + # rsn = Dot11Elt(ID='RSNinfo', info=( + # '\x01\x00' + # '\x00\x0f\xac\x02' + # '\x02\x00' + # '\x00\x0f\xac\x04' + # '\x00\x0f\xac\x02' + # '\x01\x00' + # '\x00\x0f\xac\x02' + # '\x00\x00')) + # frame = RadioTap()/dot11/dot_11_beacon/ dot_11_element /rsn + + # frame.show() # Prints a readable version of the frame that is being sent + + # hexdump(frame) # Prints a hexdump of the frame that is being sent. + + sendp(frame, iface=iface, inter=0.100, loop=1) # send the packet over interface iface every 100 milliseconds + + +mac = hex(get_mac()) # fetches device mac address +mac_arr = list(bytearray.fromhex(mac[2:])) # converts it into int + +#vendor_info = '130042200100250B3FA14C1CEE522E25ADD5B000000000000000' # Static message to send +#beacon_wifi(mac_arr, vendor_info) # Actual call to the method