forked from Telefonica/HomePWN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite-characteristic.py
More file actions
78 lines (68 loc) · 3.08 KB
/
write-characteristic.py
File metadata and controls
78 lines (68 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from utils.ble import BLE
from time import sleep
from modules._module import Module
from utils.custom_print import print_info, print_error, print_ok
from utils.check_root import is_root
from utils.shell_options import ShellOptions
from utildata.dataset_options import Option
class HomeModule(Module):
def __init__(self):
information = {"Name": "BLE write a characteristic",
"Description": "This module allows you to write content encoded in the feature specified by the UUID. The feature must be writable to proceed.",
"privileges": "root",
"OS": "Linux",
"Author": "@josueencinar"}
# -----------name-----default_value--description--required?
options = {"bmac": Option.create(name="bmac", required=True),
"uuid": Option.create(name="uuid", required=True, description='Specific UUID for a characteristic'),
"type": Option.create(name="type", value="random", required=True, description='Device addr type'),
"data": Option.create(name="data", value="Pwned", required=True, description="Data to write"),
"encode": Option.create(name="encode", required=True, description='Choose data encode'),
"wait": Option.create(name="wait", value=0, required=True, description='seconds to wait connected after writing')
}
# Constructor of the parent class
super(HomeModule, self).__init__(information, options)
# Autocomplete set option with values
def update_complete_set(self):
s_options = ShellOptions.get_instance()
s_options.add_set_option_values("encode", ["ascii", "hex"])
s_options.add_set_option_values("type", ["random", "public"])
# This function must be always implemented, it is called by the run option
@is_root
def run(self):
bmac = self.args["bmac"]
data = self._transform_data(self.args["encode"], self.args["data"])
if not data:
return
attempt = 1
success = False
ble_device = BLE(bmac, self.args["type"])
while attempt <= 5 and not success:
print_info(f"Trying to connect {bmac}. (Attempt: {attempt})")
try:
ble_device.connect()
success = True
except KeyboardInterrupt:
print_info("Interrupted... exit run")
return
except:
attempt += 1
if not success:
print_error("Failed to connect")
return
ble_device.write_data(data, self.args["uuid"])
try:
sleep(int(self.args["wait"]))
except:
sleep(2)
ble_device.disconnect()
def _transform_data(self, encode, data):
if encode == "hex":
try:
data = bytes.fromhex(data.replace("0x",""))
except:
print_error("Bad Hexadecimal value check it")
data = None
else:
data = data.encode()
return data