-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.py
162 lines (141 loc) · 4.31 KB
/
server.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
156
157
158
159
160
161
#!/usr/bin/python
#encoding=utf-8
import socket
import struct
import time
import threading
import sys
import os
import re
import hashlib
import random
from Queue import *
def sendDataUnit(sock,content):
length = struct.pack("!i",len(content))
try:
sock.send(length+content)
except:
print "[sendDataUnit] send data fail"
return 1
return 0
def recvDataUnit(sock):
try:
tmp = sock.recv(4)
except:
print "[recvDataUnit] recv data fail"
return None
length = struct.unpack("!i",tmp)[0]
content = ""
while length>0 :
content += sock.recv(1)
length -= 1
return content
def recvEncryptDataUnit(sock,key):
content = recvDataUnit(sock)
tmpstr=""
for i in range(0,len(content)):
tmpstr += chr(ord(content[i])^ord(key[i%len(key)]))
return tmpstr
def sendEncryptDataUnit(sock,content,key):
tmpstr = ""
for i in range(0,len(content)):
tmpstr += chr(ord(content[i])^ord(key[i%len(key)]))
return sendDataUnit(sock,tmpstr)
def getFile(sock,key):
du = recvEncryptDataUnit(sock,key)
print "[getFile] getFile file length",len(du)
print "[getFile] done"
return du
def writeFile(sock,fileContent,key):
print "[writeFile] ",fileContent
sendEncryptDataUnit(sock,fileContent,key)
def commandWriteFile(connectSocket,fileContent,dstfile,key):
command = " ".join(["/download",dstfile])
sendEncryptDataUnit(connectSocket,command,key)
print "[commandWriteFile] bytes",len(command)
writeFile(connectSocket,fileContent,key)
def commandExec(connectSocket,execCmd,key):
print "[commandExec] command ",execCmd,"executes"
command = " ".join(["/exec",execCmd])
sendEncryptDataUnit(connectSocket,command,key)
def commandCheckAlive(connectSocket,key):
ctime = str(time.time())
command = " ".join(["/checkAlive",ctime])
sendEncryptDataUnit(connectSocket,command,key)
content = recvEncryptDataUnit(connectSocket,key)
print "[checkAlive] done"
return 0
def commandReadFile(connectSocket,filename,key):
sendEncryptDataUnit(connectSocket,"/readFile "+filename,key)
msg = getFile(connectSocket,key)
print "[commandReadFile]",msg
return msg
class clientSocket(threading.Thread):
def __init__(self,socket,clientList):
threading.Thread.__init__(self)
self.clientList=clientList
self.clientList.append(self)
self.connectSock = socket
self.stat = "auth"
self.MsgQ = Queue()
self.time = time.time()
self.sessionKey = self.genRandID()
def genRandID(self):
sessionKey = hashlib.md5( str(random.random())).hexdigest()
return sessionKey
def authentication(self):
print "[authentication] start authentication"
res = recvDataUnit( self.connectSock )
if not res.strip() == "/hello":
print "[authentication] Fail when hello"
return 0
sendDataUnit( self.connectSock , "/hello OK" )
res = recvDataUnit( self.connectSock )
if not res.startswith("/sendIP"):
print "[authentication] Fail when getIP"
return 0
sendDataUnit( self.connectSock , "/fin " + self.sessionKey )
self.stat = "c&c"
return 1
def enqueue(self,msg):
self.MsgQ.put(msg)
def disconnect(self):
self.connectSock.close()
self.clientList.remove(self)
def run(self):
if not self.authentication():
print "[Client Thread] Authentication Fail, disconnect"
return
time.sleep(3)
challenge = str(random.random())
targetFile = "challenge.txt"
commandWriteFile(self.connectSock,challenge,targetFile,self.sessionKey)
response = commandReadFile(self.connectSock,targetFile,self.sessionKey)
print "[checkResponse] ",challenge,response
targetFile = "result.txt"
if challenge == response:
commandWriteFile(self.connectSock,"correct",targetFile,self.sessionKey)
else :
msg = "Incorrect\n"
msg += "Expect "+challenge+"\n"
msg += "Receive "+response+"\n"
commandWriteFile(self.connectSock,msg,targetFile,self.sessionKey)
commandExec(self.connectSock,"cmd.exe /c calc", self.sessionKey)
commandCheckAlive(self.connectSock, self.sessionKey)
self.disconnect()
if __name__ == "__main__":
if len(sys.argv) != 2:
print "Usage: %s <port>" % (sys.argv[0])
sys.exit(1)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
host = ''
key = "netsecIsSoCool"
port = int(sys.argv[1])
s.bind((host, port))
s.listen(10)
clientList = []
while True:
CsockIns = clientSocket(s.accept()[0],clientList)
CsockIns.start()