-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetcat.py
More file actions
86 lines (62 loc) · 2.5 KB
/
netcat.py
File metadata and controls
86 lines (62 loc) · 2.5 KB
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
import sys
import socket
import getopt
import threading
import subprocess
listen = False
command = False
upload =False
execute =""
target = ""
upload_destination =""
port = 0
def run_command(cmd):
cmd = cmd.rstrip()
try:
output = subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
except subprocess.CalledProcessError as e:
output = e.output
return output
def client_handler(client_socket):
global upload
global execute
global command
if len(upload_destination):
file_buffer=""
while True:
data = client_socket.recv(1024)
if not data:
break
else:
file_buffer += data
try:
file_descriptor = open(upload_destination,"wb")
file_descriptor.write(file_buffer.encode('utf-8'))
file_descriptor.close()
client.send(
"Successfully saved file to %s\r\n" %upload_destination)
except OSError:
client_socket.send(
"Failed to save file to %s\r\n" % upload_destination)
if len(execute):
output =run_command(execute)
client_socket.send(output)
if command:
while True:
client_socket.send("<BHP:#>".encode('utf-8'))
cmd_buffer += client_socket.recv(1024)
response = run_command(cmd_buffer.decode())
client_socket.send(response)
def sever_loop():
global traget
global port
if not len(target):
target = "0.0.0.0"
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind((target,port))
server.listen(5)
while True:
client_socket,addr =server.accept()
client_thread = threading.Thread(target=client_handler,
args=(client_socket,))
client_thread.start()