-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetcat-clone.py
155 lines (135 loc) · 5.3 KB
/
netcat-clone.py
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
import socket
import sys
import threading
import subprocess
import shlex
import argparse
import textwrap
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Net Tools",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=textwrap.dedent(
"""
netcat.py -t 00.00.00.00 -p 0000 -l -c #command shell
netcat.py -t 00.00.00.00 -p 0000 -l -u=test.txt #upload to file
netcat.py -t 00.00.00.00 -p -e="cat /etc/passwd" # execute command
echo 'ABC' | ./netcat.py -t 00.00.00.00 -p 123 # echo text to server port 123
netcat.py -t 00.00.00.00 -p 0000 # connect to server
"""
),
)
"""
- '-c' setup interactive shell
- '-e' executes one specific command
- '-h' indicates that a listner should be setup
- '-p' specifies the port on which to communicate
- '-t' specifies the target IP
- '-u' specifies the name of file to upload
- the '-c' , '-e' and '-u' args imply the '-l' argument because those args apply to only listener side of communication
- the sender side makes the connection to listener so it needs only the '-t' and '-p' args to define the target listener
"""
parser.add_argument("-c", "--command", action="store_true", help="command shell")
parser.add_argument("-e", "--execute", help="execute specfied command")
parser.add_argument("-l", "--listen", action="store_true", help="listen")
parser.add_argument("-p", "--port", type=int, default=5555, help="specified port")
parser.add_argument("-t", "--target", default="192.168.1.203", help="specified IP")
parser.add_argument("-u", "--upload", help="upload file")
args = parser.parse_args()
if args.listen:
buffer = ""
else:
buffer = sys.stdin.read()
nc = Netcat(args, buffer.encode())
nc.run()
class Netcat:
def __init__(self, args, buffer=None):
self.args = args
self.buffer = buffer
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
def run(self):
if self.args.listen:
self.listen()
else:
self.send()
def send(self):
def listen(self):
def handle(self, client_socket):
# handle method
def send(self):
self.socket.connect((self.args.target, self.args.port))
if self.buffer:
self.socket.send(self.buffer)
else:
try:
while True:
recive_len = 1
response = ""
data = self.socket.recv(4096)
recive_len = len(data)
response += data
if recive_len < 4096:
break
if response:
print(f"{response}")
buffer = input("> ")
buffer += "\n"
self.socket.send(buffer.encode())
except KeyboardInterrupt:
print("[-] User terminated")
self.socket.close()
sys.exit()
def listen(self):
self.socket.bind((self.args.target, self.args.port))
self.socket.listen(5)
while True:
try:
client_socket, _ = self.socket.accept()
client_thread = threading.Thread(target=self.handle, args=(client_socket,))
client_thread.start()
except Exception as e:
print(f"[-] server killed {e}")
self.socket.close()
sys.exit()
def handle(self, client_socket):
if self.args.execute:
try:
output = execute(self.args.execute)
client_socket.send(output.encode())
except:
print("[-] Something went worong")
elif self.args.upload:
file_buffer = b""
while True:
data = client_socket.recv(4096)
if data:
file_buffer += data
else:
break
with open(self.args.upload, "wb") as f:
f.write(file_buffer)
message = f"[+] File Saved {self.args.upload}"
client_socket.send(message.encode())
elif self.args.commad:
cnd_buffer = b""
while True:
try:
client_socket.send(b"CMD: #>")
while "\n" not in cmd_buffer.decode():
# get the commands from client
cmd_buffer += client_socket.recv(64)
response = execute(cmd_buffer.decode())
if response:
# send the response to the client
client_socket.send(response.encode())
cmd_buffer = b""
else:
client_socket.send("[-] Something went wrong")
self.socket.send()
sys.exit()
except Exception as e:
# if something went wtong
print(f"[-] server killed {e}")
self.scoket.close()
sys.exit()