Skip to content

Commit 3776d41

Browse files
committed
Cleanup code
Signed-off-by: Daniel Andrei Minca <mandrei17@gmail.com>
1 parent c967939 commit 3776d41

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

netcat.py

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,27 @@
1616
upload_destination = ""
1717
port = 0
1818

19+
1920
def usage():
2021
print "NETCAT Net Tool\n", \
21-
"", \
22-
"\tUsage: netcat.py -t target_host -p port\n", \
23-
"\t-l --listen - listen on [host]:[port] for\n", \
24-
"\ticoming connections\n", \
25-
"\t-e --execute=file_to_run - execute the given file upon\n", \
26-
"\treceiving a connection\n", \
27-
"\t-c --command - initialize a command shell\n", \
28-
"\t-u --upload=destination - upon receiving connection upload\n", \
29-
"\ta file and write to [destination]\n", \
30-
"", \
31-
"\tExamples: \n", \
32-
"\tnetcat.py -t 192.168.0.1 -p 5555 -l -c\n", \
33-
"\tnetcat.py -t 192.168.0.1 -p 5555 -l -u=c:\\target.exe\n", \
34-
"\tnetcat.py -t 192.168.0.1 -p 5555 -l -e=\"cat /etc/passwd\"\n", \
35-
"\techo 'ABCDEFGHI' | ./netcat.py -t 192.168.0.1 -p 135\n"
22+
"", \
23+
"\tUsage: netcat.py -t target_host -p port\n", \
24+
"\t-l --listen - listen on [host]:[port] for\n", \
25+
"\ticoming connections\n", \
26+
"\t-e --execute=file_to_run - execute the given file upon\n", \
27+
"\treceiving a connection\n", \
28+
"\t-c --command - initialize a command shell\n", \
29+
"\t-u --upload=destination - upon receiving connection upload\n", \
30+
"\ta file and write to [destination]\n", \
31+
"", \
32+
"\tExamples: \n", \
33+
"\tnetcat.py -t 192.168.0.1 -p 5555 -l -c\n", \
34+
"\tnetcat.py -t 192.168.0.1 -p 5555 -l -u=c:\\target.exe\n", \
35+
"\tnetcat.py -t 192.168.0.1 -p 5555 -l -e=\"cat /etc/passwd\"\n", \
36+
"\techo 'ABCDEFGHI' | ./netcat.py -t 192.168.0.1 -p 135\n"
3637
sys.exit(0)
3738

39+
3840
def main():
3941
global listen
4042
global port
@@ -49,13 +51,12 @@ def main():
4951
# read cmdline opts
5052
try:
5153
opts, args = getopt.getopt(sys.argv[1:], "hle:t:p:cu:",
52-
["help", "listen", "execute", "target", "port", "command", "upload"])
54+
["help", "listen", "execute", "target", "port", "command", "upload"])
5355
except getopt.GetoptError as err:
5456
print str(err)
5557
usage()
5658

57-
58-
for o,a in opts:
59+
for o, a in opts:
5960
if o in ("-h", "--help"):
6061
usage()
6162
elif o in ("-l", "--listen"):
@@ -76,34 +77,33 @@ def main():
7677

7778
# listen or just send data from stdin?
7879
if not listen and len(target) and port > 0:
79-
80-
# read in buffer from cmdline
81-
# this will block, so send CTRL-D if not sending input
82-
# to stdin
80+
# read in buffer from cmdline
81+
# this will block, so send CTRL-D if not sending input
82+
# to stdin
8383
buffer = sys.stdin.read()
8484

85-
# send data off
85+
# send data off
8686
client_sender(buffer)
8787

8888
# we listen and upload things, execute cmds, and drop a shell back
8989
# depending on our cmdline opts above
9090
if listen:
9191
server_loop()
9292

93-
def client_sender(buffer):
9493

94+
def client_sender(buffer):
9595
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
9696

9797
try:
98-
# connect to target host
98+
# connect to target host
9999
client.connect((target, port))
100100

101101
if len(buffer):
102102
client.send(buffer)
103103

104104
while True:
105105

106-
# wait for data back
106+
# wait for data back
107107
recv_len = 1
108108
response = ""
109109

@@ -118,24 +118,25 @@ def client_sender(buffer):
118118

119119
print response,
120120

121-
# wait for more input
121+
# wait for more input
122122
buffer = raw_input("")
123123
buffer += "\n"
124124

125-
# send it off
125+
# send it off
126126
client.send(buffer)
127127

128128
except:
129129

130130
print "[*] Exception! Exiting."
131131

132-
# tear down connection
132+
# tear down connection
133133
client.close()
134134

135+
135136
def server_loop():
136137
global target
137138

138-
# if no target defined, listen on all interfaces
139+
# if no target defined, listen on all interfaces
139140
if not len(target):
140141
target = "0.0.0.0"
141142

@@ -146,30 +147,31 @@ def server_loop():
146147
while True:
147148
client_socket, addr = server.accept()
148149

149-
# spin off thread to handle new client
150-
client_thread = threading.Thread(target = client_handler, args = (client_socket,))
150+
# spin off thread to handle new client
151+
client_thread = threading.Thread(target=client_handler, args=(client_socket,))
151152
client_thread.start()
152153

153-
def run_command(command):
154154

155-
# trim newline
155+
def run_command(command):
156+
# trim newline
156157
command = command.rstrip()
157158

158-
# run cmd and get output
159+
# run cmd and get output
159160
try:
160-
output = subprocess.check_output(command, stderr = subprocess.STDOUT, shell = True)
161+
output = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True)
161162
except:
162163
output = "Failed to execute command.\r\n"
163164

164-
# send output back to client
165+
# send output back to client
165166
return output
166167

168+
167169
def client_handler(client_socket):
168170
global upload
169171
global execute
170172
global comand
171173

172-
# check for upload
174+
# check for upload
173175
if len(upload_destination):
174176
# read in all of the bytes and write to destination
175177
file_buffer = ""
@@ -208,7 +210,7 @@ def client_handler(client_socket):
208210
# show simple prompt
209211
client_socket.send("<NETCAT:#> ")
210212

211-
# receive until linefeed (enter key)
213+
# receive until linefeed (enter key)
212214
cmd_buffer = ""
213215
while "\n" not in cmd_buffer:
214216
cmd_buffer += client_socket.recv(1024)
@@ -219,5 +221,5 @@ def client_handler(client_socket):
219221
# send back response
220222
client_socket.send(response)
221223

222-
main()
223224

225+
main()

0 commit comments

Comments
 (0)