-
Notifications
You must be signed in to change notification settings - Fork 0
/
brute_force.py
51 lines (38 loc) · 1.21 KB
/
brute_force.py
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
import serial
import struct
import binascii
import time
def loopback_write(s: serial.Serial, bytes: bytearray):
s.write(bytes)
r = s.read(len(bytes))
assert r == bytes
def checksum(bytes: bytearray) -> bytearray:
return struct.pack("B", (~sum(bytes)) & 0xFF)
def send_data(s: serial.Serial, bytes: bytearray):
loopback_write(s, b'\xFF\xFF' + bytes + checksum(bytes))
def wait_for_bytes(s: serial.Serial, bytes: bytearray):
assert s.read(len(bytes)) == bytes
s = serial.Serial("COM3", 77170)
# s.timeout = 0.025
# for i in range(0xFFFF):
# send_data(s, b"\x01\x04\x02" + struct.pack(">H", i))
# r = s.read(100)
# if r:
# print(f"{i}: {r}")
# prev = b''
# for i in range(0xFF):
# for j in range(1, 0xFF):
# send_data(s, b"\x01\x04\x02" + struct.pack("BB", i, j))
# r = s.read(100)
# if not r:
# break
# prev = r
# print(
# f"{hex(i)[2:].upper().rjust(2,'0')}: {binascii.hexlify(prev, ' ').decode('utf-8').upper()}")
data = b""
send_data(s, b"\x01\x04\x02\x00\x39")
data += s.read(63)[5:-1]
time.sleep(0.4)
send_data(s, b"\x01\x04\x02\x39\x0b")
data += s.read(17)[5:-1]
print(binascii.hexlify(data, ' ').decode('utf-8').upper())