Skip to content

Commit b3975f7

Browse files
committed
feat: update subprocess
Change-Id: Ib095986ad8686baa2c9134797baa7ed07900cdfb
1 parent 72eefa8 commit b3975f7

File tree

2 files changed

+135
-9
lines changed

2 files changed

+135
-9
lines changed

code/subprocess_demo.py

+26-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
11
# coding=utf-8
2-
2+
import os
3+
import six
34
import subprocess
45

6+
if six.PY2:
7+
input = raw_input
8+
cwd = os.getcwd()
9+
10+
511
def run_command(command):
12+
global cwd
613
command = command.rstrip()
14+
commands = command.split()
15+
if commands and commands[0] == "cd":
16+
cwd = (len(commands) > 1 and os.path.join(cwd, commands[1])) or os.path.expanduser('~')
17+
return b""
18+
719
try:
8-
output = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True)
20+
process = subprocess.Popen(command,stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True, cwd=cwd)
21+
stdout, stderr = process.communicate()
22+
retcode = process.poll()
23+
if retcode:
24+
return stderr
25+
return stdout
926
except:
1027
output = 'Failed to execute command.\r\n'
1128

1229
return output
1330

1431

1532
if __name__ == '__main__':
16-
while 1:
17-
command = raw_input("$ ")
18-
if command == "exit" or command == "quit":
19-
break
20-
result = run_command(command)
21-
22-
print result,
33+
while 1:
34+
command = input("$ ")
35+
if command == "exit" or command == "quit":
36+
break
37+
result = run_command(command)
38+
39+
print(result.decode("utf-8"))

code/subprocess_shell.py

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import sys
4+
import socket
5+
import argparse
6+
import threading
7+
import subprocess
8+
9+
10+
class TargetServer(object):
11+
12+
def __init__(self, port):
13+
self.port = port
14+
self.host = socket.gethostname()
15+
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
16+
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
17+
self.server.bind(("0.0.0.0", int(self.port)))
18+
self.server.listen(10)
19+
20+
def run(self):
21+
while 1:
22+
client_socket, client_addr = self.server.accept()
23+
client_thread = threading.Thread(target=self.client_handler,
24+
args=(client_socket,))
25+
client_thread.start()
26+
27+
def client_handler(self, client_socket):
28+
client_socket.sendall("<@ %s $ >" % self.host)
29+
while 1:
30+
try:
31+
cmd_buffer = client_socket.recv(1024)
32+
response = self.run_command(cmd_buffer)
33+
if len(response) == 0:
34+
response = "[Successful!]\n"
35+
client_socket.sendall(response)
36+
except Exception as e:
37+
# print e
38+
break
39+
40+
def run_command(self, command):
41+
command = command.strip()
42+
try:
43+
output = subprocess.check_output(command, stderr=subprocess.STDOUT,
44+
shell=True)
45+
except:
46+
output = '[*]Failed to execute command ! \n'
47+
48+
return output
49+
50+
51+
class Client(object):
52+
53+
def __init__(self, host, port):
54+
self.host = host
55+
self.port = port
56+
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
57+
self.client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
58+
59+
def run(self):
60+
try:
61+
self.client.connect((self.host, int(self.port)))
62+
header = self.client.recv(4096)
63+
command = raw_input(header)
64+
if command == "exit" or command == "quit":
65+
self.clien.close()
66+
sys.exit(0)
67+
self.client.sendall(command)
68+
while 1:
69+
recv_len = 1
70+
response = ""
71+
72+
while recv_len:
73+
data = self.client.recv(4096)
74+
recv_len = len(data)
75+
response += data
76+
if recv_len < 4096:
77+
break
78+
79+
print(response)
80+
81+
command = raw_input(header)
82+
if command == "exit" or command == "quit":
83+
self.client.close()
84+
break
85+
self.client.sendall(command)
86+
87+
except:
88+
print("[*] Exception Failed ! \n")
89+
90+
91+
if __name__ == '__main__':
92+
parser = argparse.ArgumentParser(description="NetCat Shell")
93+
parser.add_argument("-s", "--server", help="Target Server",
94+
action="store_true")
95+
parser.add_argument("-c", "--client", help="Client", action="store_true")
96+
parser.add_argument("--host", help="target host IP", action="store",
97+
default="127.0.0.1")
98+
parser.add_argument("-p", "--port", help="target host port", action="store",
99+
type=int)
100+
args = parser.parse_args()
101+
port = args.port
102+
if args.server:
103+
s = TargetServer(port)
104+
s.run()
105+
if args.client:
106+
host = args.host
107+
c = Client(host, port)
108+
c.run()
109+

0 commit comments

Comments
 (0)