-
Notifications
You must be signed in to change notification settings - Fork 72
/
relay.py
267 lines (224 loc) · 9.07 KB
/
relay.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
import requests
from requests import packages
from requests.packages import urllib3
from requests.packages.urllib3 import exceptions
from pathlib import Path
from utils import *
def custom_checksec(host, port, message):
""" Some embedded devices with 'psh' will hang after checksec() """
cache_dir = ''.join(tempfile.gettempdir() + '/pwntools-ssh-cache')
Path(cache_dir).mkdir(parents=True, exist_ok=True)
fpath = ''.join(cache_dir + '/{}-{}'.format(host, port))
with open(fpath, 'w+') as f:
f.write(message)
def init_relay(relay=None, rhost=None, rport=None, discover=False):
""" Relay via SSH """
dh_remote = None
# import paramiko
# paramiko ssh debugging
# logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
# logging.basicConfig(stream=sys.stdout)
try:
proto = relay[0:relay.index('://')]
tmp = relay[len(proto)+3:].split('@')
relay_username = tmp[0].split(':')[0]
relay_password = tmp[0].split(':')[1]
relay_rhost = tmp[1].split(':')[0]
relay_rport = tmp[1].split(':')[1]
""" Check if RPORT is valid """
if not check_port(relay_rport):
log.failure("Invalid relay port - Choose between 1 and 65535")
return False
""" Check if RHOST is valid IP or FQDN, get IP back """
if not check_host(relay_rhost):
log.failure("Invalid relay host")
return False
except (ValueError, IndexError):
log.failure('relay usage: <proto>://<user>:<password>@<host|fqdn>:<port>')
return False
if proto == 'ssh':
message = '(null)'
custom_checksec(host=relay_rhost, port=relay_rport, message=message)
try:
dh_relay = ssh(
user=relay_username,
password=relay_password,
host=relay_rhost,
port=int(relay_rport),
timeout=60,
cache=False
)
# return relay
except Exception as e:
print('[init_relay] ssh: {}'.format(repr(e)))
return False
if not discover:
try:
dh_remote = dh_relay.connect_remote(rhost, rport)
except AttributeError:
dh_relay.close()
return False
except Exception as e:
print('[init_relay] remote: ', repr(e))
dh_relay.close()
return False
"""
print(relay.transport.remote_version)
print(relay.transport.local_version)
print(relay.transport.remote_mac)
print(relay.transport.local_mac)
print(relay.transport.remote_cipher)
print(relay.transport.get_security_options())
"""
return {
"dh_relay": dh_relay,
"dh_remote": dh_remote
}
else:
log.failure('"{}" relay proto not implemented'.format(proto))
return False
class DahuaHttp(object):
def __del__(self):
log.info('DahuaHttp DELETE')
""" Dahua http """
# TODO
# Get HTTP/HTTPS working with SSH relay
def __init__(self, rhost, rport, proto, timeout=60):
super(DahuaHttp, self).__init__()
self.rhost = rhost
self.rport = rport
self.proto = proto
self.timeout = timeout
self.remote = None
self.uri = None
self.stream = None
""" Most devices will use self-signed certificates, suppress any warnings """
requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)
self.remote = requests.Session()
"""Used with _debug"""
self.headers = self.remote.headers
self.cookies = self.remote.cookies
self._init_uri()
import random as random_agent
random_agent.seed(1)
self.remote.headers.update({
'User-Agent': useragents.random(),
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'en-US,en;q=0.9',
'Host': '{}:{}'.format(self.rhost, self.rport),
})
# TODO
"""To use '--relay' option"""
"""
self.remote.proxies.update({
# 'http': 'http://127.0.0.1:8080',
})
"""
def send(self, url=None, query_args=None, login=False, timeout=5):
"""JSON API communication"""
if query_args:
if query_args.get('params') is not None and not len(query_args.get('params')):
query_args.update({'params': None})
"""This weird code will try automatically switch between http/https
and update Host
"""
try:
if url and not query_args:
return self.get(url, timeout)
else:
dh_data = self.post(self._get_url(login, url), query_args, timeout)
except requests.exceptions.ConnectionError:
self.proto = 'https' if self.proto == 'http' else 'https'
self._init_uri()
try:
if url and not query_args:
return self.get(url, timeout)
else:
dh_data = self.post(self._get_url(login, url), query_args, timeout)
except requests.exceptions.ConnectionError as e:
if login:
return self._error(dh_error=e)
return None
except requests.exceptions.RequestException as e:
if login:
return self._error(dh_error=e)
return None
except KeyboardInterrupt:
return None
"""302 when requesting http on https enabled device"""
if dh_data.status_code == 302:
redirect = dh_data.headers.get('Location')
self.uri = redirect[:redirect.rfind('/')]
self._update_host()
if url and not query_args:
return self.get(url, timeout)
else:
dh_data = self.post(self._get_url(login, url), query_args, timeout)
"""Catch non dahua hosts"""
if not dh_data.status_code == 200:
return self._error(dh_error=dh_data.text, code=dh_data.status_code)
"""JSON API communication"""
dh_json = dh_data.json()
"""Set SessionID Cookie during login"""
if login and self.remote.cookies.get('DWebClientSessionID') is None:
self.remote.cookies.set('username', query_args.get('params').get('userName'))
self.remote.cookies.set('DWebClientSessionID', str(dh_json.get('session')))
return dh_data
@staticmethod
def _get_url(login, url):
if login:
return '/RPC2_Login'
elif url:
"""GET or other POST JSON API communication"""
return url
"""Default JSON API communication"""
return '/RPC2'
def _update_host(self):
if not self.remote.headers.get('Host') == self.uri[self.uri.rfind('://') + 3:]:
self.remote.headers.update({
'Host': self.uri[self.uri.rfind('://') + 3:],
})
def _init_uri(self):
self.uri = '{proto}://{rhost}:{rport}'.format(proto=self.proto, rhost=self.rhost, rport=str(self.rport))
@staticmethod
def _error(dh_error=None, code=500):
"""Keep 'login' happy and give some info back in case of failure"""
return json.dumps({'result': False, 'error': {'code': code, 'message': str(dh_error)}})
def options(self):
timeout = 10
req = requests.Request('OPTIONS', 'rtsp://{host}:{port}?proto=Onvif RTSP/1.1\r\nCSeq: 1\r\n\r\n'.format(
host='192.168.5.27', port=80))
print(req.prepare())
print(req.url)
dh_data = self.remote.send(req.url, verify=False, allow_redirects=False, timeout=timeout)
print(dh_data)
def post(self, url, query_args, timeout):
"""JSON API Communication"""
return self.remote.post(self.uri + url, json=query_args, verify=False, allow_redirects=False, timeout=timeout)
def get(self, url, timeout):
"""Non JSON Communication"""
return self.remote.get(self.uri + url, verify=False, allow_redirects=False, timeout=timeout)
def open_stream(self, session_id):
"""Open stream session for events and other 'client.Notify'"""
self.stream = self.remote.get(
'{}/SubscribeNotify.cgi?sessionId={}'.format(self.uri, session_id),
verify=False, allow_redirects=False, stream=True
)
def recv_stream(self):
"""Return events and other 'client.Notify'"""
return fix_json(self.stream.raw.readline().decode('utf-8'))
@staticmethod
def can_recv():
"""We do not expect unexpected data
the 'stream' above will handle that"""
return False
@staticmethod
def connected():
# TODO: Assume connected, should find a way to check
return True
def close(self):
# TODO: Not really sure if this way
self.remote.close()
return True