Skip to content

Commit a7bcaf4

Browse files
committed
implement logic for file uploads cmd exec & shell
Resolves: Related: Signed-off-by: Daniel Andrei Minca <mandrei17@gmail.com>
1 parent a488e9d commit a7bcaf4

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

netcat.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,60 @@ def run_command(command):
164164
# send output back to client
165165
return output
166166

167+
def client_handler(client_socket):
168+
global upload
169+
global execute
170+
global comand
171+
172+
# check for upload
173+
if len(upload_destination):
174+
# read in all of the bytes and write to destination
175+
file_buffer = ""
176+
177+
# keep reading data until none is available
178+
while True:
179+
data = client_socket.recv(1024)
180+
181+
if not data:
182+
break
183+
else:
184+
file_buffer += data
185+
186+
# take these bytes and write them out
187+
try:
188+
file_descriptor = open(upload_destination, "wb")
189+
file_descriptor.write(file_buffer)
190+
file_descriptor.close()
191+
192+
# ack that we wrote file out
193+
client_socket.send("Successfully saved file to %s\r\n" % upload_destination)
194+
except:
195+
client_socket.send("Failed to save file to %s\r\n" % upload_destination)
196+
197+
# check for cmd execution
198+
if len(execute):
199+
# run cmd
200+
output = run_command(execute)
201+
202+
client_socket.send(output)
203+
204+
# go in another loop if cmd shell requested
205+
if command:
206+
207+
while True:
208+
# show simple prompt
209+
client_socket.send("<NETCAT:#> ")
210+
211+
# receive until linefeed (enter key)
212+
cmd_buffer = ""
213+
while "\n" not in cmd_buffer:
214+
cmd_buffer += client_socket.recv(1024)
215+
216+
# send back cmd output
217+
response = run_command(cmd_buffer)
218+
219+
# send back response
220+
client_socket.send(response)
221+
167222
main()
168223

0 commit comments

Comments
 (0)