forked from gallinapassus/huawei-e5186
-
Notifications
You must be signed in to change notification settings - Fork 0
/
huawei-e5186-http-client.py
314 lines (264 loc) · 13.4 KB
/
huawei-e5186-http-client.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import http.client
import requests
import urllib
import xml.etree.ElementTree as ET
import hashlib
import base64
import re
import time
import datetime
import argparse
# Python3
#
# Router password SHA256 hexdigest
# Use the 'admin' password.
#
# How to get password hexdigest:
#
# Mac/Linux: echo -n 'your password here' | shasum -a 256
#
# Example value (password = password):
#
# passwordSHA256 = '5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8'
passwordSHA256 = None
#
# Version
#
version = '1.1.0'
class Hwcli:
def __init__(self, baseUrl, username = 'admin', password = ''):
self.baseUrl = baseUrl
self.port = 80
self.username = username
self.password = password
self.token = None
self.sessionInfo = None
self.session = requests.Session()
self.cookie = None
self.rvt0 = None
self.rvt1 = None
self.rvt2 = None
self.debug = False
def apiUrl(self, api = ''):
return 'http://{}{}{}'.format(self.baseUrl, '' if self.port == 80 else ':{}'.format(self.port), api)
def apiPost(self, api = '', data = None):
self._getSession()
r = self.session.post(self.apiUrl(api), data = data)
if self.debug == True: print(r.url, '->', r.status_code)
arr = [
'__RequestVerificationToken',
'__RequestVerificationTokenone',
'__RequestVerificationTokentwo'
]
for k in arr:
try:
self.session.headers.update({ k: r.headers[k] })
except KeyError as e:
pass
return r
def apiGet(self, api = ''):
self._getSession()
r = self.session.get(self.apiUrl(api))
if self.debug == True: print(r.url, '->', r.status_code)
return r
def _getSession(self):
if self.token != None and self.sessionInfo != None: return
request = self.session.get('http://' + self.baseUrl + '/api/webserver/SesTokInfo')
data = request.text
tree = ET.fromstring(data)
if len(tree.findall('TokInfo')) > 0:
self.token = tree.findall('TokInfo')[0].text
else:
self.token = ''
if self.debug == True: print('Failed to get session token')
if len(tree.findall('SesInfo')) > 0:
self.sessionInfo = tree.findall('SesInfo')[0].text
else:
self.sessionInfo = ''
if self.debug == True: print('Failed to get session info')
def login(self):
self._getSession()
self.session.headers.update({ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie': self.sessionInfo,
'__RequestVerificationToken': self.token
})
xml = '<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<request>\n <Username>{}</Username>\n <Password>{}</Password>\n <password_type>4</password_type>\n</request>\n'.format(self.username, self._authToken())
p = self.apiPost('/api/user/login', xml)
xml = p.text
tree = ET.fromstring(xml)
if tree.text != 'OK':
raise Exception('login: Login failed.')
# Cookie
try:
cookie = p.headers['Set-Cookie']
self.cookie = cookie
self.session.headers.update({ 'Cookie': cookie })
self.rvt0 = p.headers['__RequestVerificationToken']
self.rvt1 = p.headers['__RequestVerificationTokenone']
self.rvt2 = p.headers['__RequestVerificationTokentwo']
self.session.headers.update({ '__RequestVerificationToken': self.rvt0 })
self.session.headers.update({ '__RequestVerificationTokenone': self.rvt1 })
self.session.headers.update({ '__RequestVerificationTokentwo': self.rvt2 })
except KeyError as e:
raise Exception('login: Missing key {}.'.format(str(e)))
def _authToken(self):
if self.token == None or self.token == '': raise ValueError('error: No valid authentication token available.')
def encrypt(text):
m = hashlib.sha256()
m.update(str.encode(text))
return base64.b64encode(str.encode(m.hexdigest())).decode()
return encrypt(self.username + base64.b64encode(str.encode(self.password)).decode() + self.token)
def dumpXMLFields(self, matchingFields, content):
root = ET.fromstring(content)
if matchingFields == None or len(matchingFields) < 1:
ET.dump(root)
else:
for needle in matchingFields:
val = root.findall(needle)
if type(val) is list:
if len(val) == 0: print('No matches for \'{}\''.format(needle))
else:
def _prettyprint(item, lvl):
pad = ' '
if type(item) is not list:
children = list(item)
if len(children) > 0: _prettyprint(children, lvl + 2)
else: print('{}<{}>{}</{}>'.format(pad * lvl, item.tag, item.text if item.text is not None else '', item.tag))
else:
for i in item:
children = list(i)
if children != None:
print('{}<{}>'.format(pad * lvl, i.tag), end = '')
if len(children) > 0:
print()
_prettyprint(children, lvl + 2)
print('{}</{}>'.format(pad * lvl, i.tag))
else:
print('{}</{}>'.format(i.text if i.text is not None else '', i.tag))
lvl = 0
for index,i in enumerate(val):
_prettyprint(i, lvl)
else:
print('***', val)
def apiDeviceConfigXml(self, fields = None):
self.dumpXMLFields(fields, self.apiGet('/config/deviceinformation/config.xml').content)
def apiGlobalConfigXml(self, fields = None):
self.dumpXMLFields(fields, self.apiGet('/config/global/config.xml').content)
def apiSmsConfig(self, fields = None):
self.dumpXMLFields(fields, self.apiGet('/config/sms/config.xml').content)
def apiMonitoringTrafficStatistics(self, fields = None):
self.dumpXMLFields(fields, self.apiGet('/api/monitoring/traffic-statistics').content)
def apiWlanFeature(self, fields = None):
self.dumpXMLFields(fields, self.apiGet('/api/wlan/wifi-feature-switch').content)
def apiWlanHostList(self, fields = None):
self.dumpXMLFields(fields, self.apiGet('/api/wlan/host-list').content)
def apiMonitoringStatus(self, fields = None):
self.dumpXMLFields(fields, self.apiGet('/api/monitoring/status').content)
def apiSimStatus(self, fields = None):
self.dumpXMLFields(fields, self.apiGet('/api/pin/status').content)
def apiIsLoggedIn(self, fields = None):
self.dumpXMLFields(fields, self.apiGet('/api/user/state-login').content)
def apiWlanBasicSettings(self, fields = None):
self.dumpXMLFields(fields, self.apiGet('/api/wlan/basic-settings').content)
def apiWlanHandoverSettings(self, fields = None):
self.dumpXMLFields(fields, self.apiGet('/api/wlan/handover-setting').content)
def apiGlobalModuleSwitch(self, fields):
self.dumpXMLFields(fields, self.apiGet('/api/global/module-switch').content)
def apiSmsInbox(self, fields = None):
xml = '<?xml version="1.0" encoding="UTF-8"?><request><PageIndex>1</PageIndex><ReadCount>10</ReadCount><BoxType>1</BoxType><SortType>0</SortType><Ascending>0</Ascending><UnreadPreferred>1</UnreadPreferred></request>'
self.dumpXMLFields(fields, self.apiPost('/api/sms/sms-list', xml).content)
def apiSendSms(self, recipients = None, message = None):
if recipients == None or message == None: return
assert(type(recipients) is list)
assert(type(message) is str)
now = datetime.datetime.now()
future = now + datetime.timedelta(minutes = 1)
plist = ''
for phone in recipients:
if type(phone) is str: plist += '<Phone>{}</Phone>'.format(phone)
else: print('Invalid recipient \'{}\' skipped.'.format(str(phone)))
xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><request><Index>-1</Index><Phones>" + plist + "</Phones><Sca></Sca><Content>" + message + "</Content><Length>" + str(len(message)) + "</Length><Reserved>1</Reserved><Date>" + future.strftime('%Y-%m%d %H:%M:%S') + "</Date></request>"
r = self.apiPost('/api/sms/send-sms', str(xml))
def apiReboot(self, fields = None):
xml = '<?xml version="1.0" encoding="UTF-8"?>\n<request>\n <Control>1</Control>\n</request>'
self.dumpXMLFields(fields, self.apiPost('/api/device/control', data = xml).content)
def apiDataSwitch(self,on=True):
if on:
xml = '<?xml version="1.0" encoding="UTF-8"?>\n<request>\n <dataswitch>1</dataswitch>\n</request>'
else:
xml = '<?xml version="1.0" encoding="UTF-8"?>\n<request>\n <dataswitch>0</dataswitch>\n</request>'
self.dumpXMLFields(None, self.apiPost('/api/dialup/mobile-dataswitch', data = xml).content)
def parseArgumentsAndRun():
global passwordSHA256
global version
parser = argparse.ArgumentParser()
subParsers = parser.add_subparsers(title='commands', dest = 'commands')
# Configure 'main parser'
parser.add_argument('-v', '--version', action='version', version = '%(prog)s {}'.format(version), help = 'Display version number')
parser.add_argument('-d', '--debug', action = 'store_true', default = False, help = 'Show debug information.')
parser.add_argument('-t', metavar = 'address', type=str, default = '192.168.8.1', help = 'Target hostname or IP-address')
parser.add_argument('-s', metavar = 'sha256', type=str, default = None, help = 'Router\'s password as SHA256 hexdigest.')
# Create 'sub parsers'
show_parser = subParsers.add_parser('show', help = 'Show router information.')
data_parser = subParsers.add_parser('data', help = 'Enable or disable mobile data.')
reboot_parser = subParsers.add_parser('reboot', help = 'Reboot router.')
send_parser = subParsers.add_parser('sendsms', help = 'Send SMS message.')
# Config 'show'
showSubParsers = show_parser.add_subparsers(title='item', dest='item', help = 'Available items')
showarr = {
'sim': ('SIM status.', 'apiSimStatus'),
'monitoring': ('Monitoring status.', 'apiMonitoringStatus'),
'traffic': ('Traffic statistics.', 'apiMonitoringTrafficStatistics'),
'wlan': ('WLAN status.', 'apiWlanBasicSettings'),
'wlan-hosts': ('Connected hosts status.', 'apiWlanHostList'),
'wlan-features': ('WLAN features.', 'apiWlanFeature'),
'inbox': ('SMS Inbox.', 'apiSmsInbox'),
'module-switch': ('Global module switch.', 'apiGlobalModuleSwitch'),
'sms-config': ('SMS config.', 'apiSmsConfig'),
'global-config': ('Global config.', 'apiGlobalConfigXml'),
'device-config': ('Device config.', 'apiDeviceConfigXml'),
}
for k in showarr:
prsr = showSubParsers.add_parser(k, help = showarr[k][0])
prsr.add_argument('fields', nargs = '*', type = str, metavar = 'FILTER', help = 'XML XPath spec.')
# Config 'data'
dataSubParsers = data_parser.add_subparsers(title='item', dest='item', help = 'Available items')
dataarr = {
'on': ('Mobile data on.', True),
'off': ('Mobile data off.', False),
}
for k in dataarr:
prsr = dataSubParsers.add_parser(k, help = dataarr[k][0])
prsr.add_argument('fields', nargs = '*', type = str, metavar = 'FILTER', help = 'XML XPath spec.')
# Config 'sendsms'
send_parser.add_argument('message', type = str, help = 'Message.')
send_parser.add_argument('phone_number', nargs = '+', type = str, help = 'Recipient phone number.')
args = parser.parse_args()
if args.commands == None: parser.print_help()
else:
if args.s != None:
pwd = args.s
else:
if passwordSHA256 == None:
raise ValueError('\nPassword hash variable is not set. Please edit \'{}\' and set variable \'passwordSHA256\' to match your router\'s password (sha256) or alternatively set password with command line option -s.\n'.format(__file__))
else:
pwd = passwordSHA256
hw = Hwcli(baseUrl = args.t, password = pwd)
if args.debug == True: hw.debug = True
hw.login()
if args.commands == 'show':
try:
getattr(hw, showarr[args.item][1])(args.fields)
except KeyError as e:
show_parser.print_help()
elif args.commands == 'sendsms':
hw.apiSendSms(message = args.message, recipients = args.phone_number)
elif args.commands == 'reboot':
hw.apiReboot()
elif args.commands == 'data':
hw.apiDataSwitch(dataarr[args.item][1])
if __name__ == '__main__':
try:
parseArgumentsAndRun()
except Exception as e:
print(str(e))