112 lines
3.3 KiB
Python
112 lines
3.3 KiB
Python
import socket, hashlib, time
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
s.settimeout(10)
|
|
s.connect(('192.168.100.2', 8728))
|
|
|
|
def el(l):
|
|
if l < 0x80: return bytes([l])
|
|
elif l < 0x4000: l |= 0x8000; return bytes([(l>>8)&0xFF, l&0xFF])
|
|
elif l < 0x200000: l |= 0xC00000; return bytes([(l>>16)&0xFF, (l>>8)&0xFF, l&0xFF])
|
|
elif l < 0x10000000: l |= 0xE0000000; return bytes([(l>>24)&0xFF, (l>>16)&0xFF, (l>>8)&0xFF, l&0xFF])
|
|
else: return bytes([0xF0, (l>>24)&0xFF, (l>>16)&0xFF, (l>>8)&0xFF, l&0xFF])
|
|
|
|
def ww(w): s.sendall(el(len(w)) + (w.encode() if isinstance(w, str) else w))
|
|
|
|
def ws(c, *a):
|
|
ww(c)
|
|
for x in a: ww(x)
|
|
s.sendall(b'\x00')
|
|
|
|
def rb(): return s.recv(1)
|
|
|
|
def rw():
|
|
f = rb()
|
|
if f == b'\x00': return None
|
|
lb = f[0]
|
|
if lb & 0x80:
|
|
if (lb & 0xC0) == 0x80: l = ((lb & 0x3F) << 8) + rb()[0]
|
|
elif (lb & 0xE0) == 0xC0: l = ((lb & 0x1F) << 16) + (rb()[0] << 8) + rb()[0]
|
|
elif (lb & 0xF0) == 0xE0: l = ((lb & 0x0F) << 24) + (rb()[0] << 16) + (rb()[0] << 8) + rb()[0]
|
|
else: l = (rb()[0] << 24) + (rb()[0] << 16) + (rb()[0] << 8) + rb()[0]
|
|
else: l = lb
|
|
d = b''
|
|
while len(d) < l:
|
|
c = s.recv(l - len(d))
|
|
if not c: break
|
|
d += c
|
|
return d.decode('utf-8', errors='replace')
|
|
|
|
def rss():
|
|
ws = []
|
|
while True:
|
|
w = rw()
|
|
if w is None: break
|
|
ws.append(w)
|
|
return ws
|
|
|
|
def rr():
|
|
rs = []
|
|
while True:
|
|
sent = rss()
|
|
if not sent: break
|
|
r = sent[0]; t = sent[1:]
|
|
if r == '!done': break
|
|
elif r == '!re':
|
|
d = {}
|
|
for w in t:
|
|
if '=' in w:
|
|
p = w.split('=', 2)
|
|
d[p[1]] = p[2] if len(p) >= 3 else ''
|
|
else: d[w] = ''
|
|
rs.append(d)
|
|
elif r == '!trap':
|
|
rs.append(('!trap', t))
|
|
return rs
|
|
|
|
def cmd(c, **a):
|
|
w = [c]
|
|
for k, v in a.items():
|
|
if k.startswith('?'): w.append(f'{k}={v}')
|
|
else: w.append(f'={k}={v}')
|
|
ws(*w)
|
|
return rr()
|
|
|
|
# Login
|
|
ws('/login', '=name=mi4', '=password=m14')
|
|
r = rr()
|
|
if any(isinstance(x, tuple) and x[0] == '!trap' for x in r):
|
|
for x in r:
|
|
if isinstance(x, dict) and 'ret' in x: ch = x['ret']
|
|
if ch and len(ch) == 32:
|
|
resp = '00' + hashlib.md5(b'\x00' + b'm14' + bytes.fromhex(ch)).hexdigest()
|
|
ws('/login', '=name=mi4', f'=response={resp}')
|
|
rr()
|
|
|
|
# Dump on-event Bulanan2
|
|
print("=== Bulanan2 on-event ===")
|
|
for s_item in cmd('/system/scheduler/print'):
|
|
if s_item['name'] == 'Bulanan2':
|
|
oe = s_item.get('on-event', '')
|
|
lines = oe.split('\n')
|
|
print(f"Total lines: {len(lines)}")
|
|
for i, l in enumerate(lines):
|
|
print(f"{i+1:3d}: {l}")
|
|
# Count :pic vs :pick
|
|
print(f"\n:pik occurrences: {oe.count(':pic,') + oe.count(':pic')} (should be 0 if fully :pick)")
|
|
print(f":pick occurrences: {oe.count(':pick')}")
|
|
break
|
|
|
|
# Dump on-login Bulanan2
|
|
print("\n=== Bulanan2 on-login ===")
|
|
for p_item in cmd('/ip/hotspot/profile/print'):
|
|
if p_item['name'] == 'Bulanan2':
|
|
ol = p_item.get('on-login', '')
|
|
lines = ol.split('\n')
|
|
print(f"Total lines: {len(lines)}")
|
|
for i, l in enumerate(lines):
|
|
print(f"{i+1:3d}: {l}")
|
|
break
|
|
|
|
s.close()
|