22
22
from threading import Thread
23
23
24
24
import requests
25
+ from requests .packages .urllib3 .exceptions import InsecureRequestWarning
25
26
27
+ requests .packages .urllib3 .disable_warnings (InsecureRequestWarning )
26
28
from config import *
27
29
28
- global LOG_LEVEL , SLEEP_TIME , READ_BUFF_SIZE , WEBSHELL , TARGET_ADDR , LOCAL_ADDR
30
+ global LOG_LEVEL , SLEEP_TIME , READ_BUFF_SIZE , WEBSHELL , TARGET_ADDR , LOCAL_ADDR , CLEAN_DIE , SOCKET_TIMEOUT
29
31
global cache_conns , die_client_address , not_in_server_cache_conns
30
32
31
33
@@ -53,7 +55,7 @@ def check_server(self):
53
55
}
54
56
for i in range (5 ):
55
57
try :
56
- r = requests .post (WEBSHELL , data = payload , timeout = 3 )
58
+ r = requests .post (WEBSHELL , data = payload , timeout = 3 , verify = False )
57
59
web_return_data = json .loads (b64decodeX (r .content ).decode ("utf-8" ))
58
60
except Exception as E :
59
61
logger .error (r .content )
@@ -84,42 +86,36 @@ def sync_data(self):
84
86
}
85
87
data = {}
86
88
# 清除无效的client
89
+
87
90
for client_address in self .die_client_address :
88
91
try :
89
92
one = cache_conns .pop (client_address )
90
93
one .get ("conn" ).close ()
91
- logger .warning ("CLIENT_ADDRESS:{} close client not in server cache_conns " .format (client_address ))
94
+ logger .warning ("CLIENT_ADDRESS:{} close client in die_client_address " .format (client_address ))
92
95
except Exception as E :
93
96
logger .warning (
94
- "CLIENT_ADDRESS:{} close client not in server cache_conns error" .format (client_address ))
97
+ "CLIENT_ADDRESS:{} close client close client in die_client_address error" .format (client_address ))
95
98
96
- has_data_to_send = False
97
99
for client_address in list (cache_conns .keys ()):
98
100
client = cache_conns .get (client_address ).get ("conn" )
99
101
try :
100
102
tcp_recv_data = client .recv (READ_BUFF_SIZE )
101
103
logger .debug ("CLIENT_ADDRESS:{} TCP_RECV_DATA:{}" .format (client_address , tcp_recv_data ))
102
104
if len (tcp_recv_data ) > 0 :
103
- has_data_to_send = True
104
105
logger .info ("CLIENT_ADDRESS:{} TCP_RECV_LEN:{}" .format (client_address , len (tcp_recv_data )))
105
106
except Exception as err :
106
107
tcp_recv_data = b""
107
108
logger .debug ("TCP_RECV_NONE" )
108
109
data [client_address ] = {"data" : base64 .b64encode (tcp_recv_data )}
109
110
110
- # 如果没有数据需要发送
111
- # if has_data_to_send is not True and len(self.die_client_address) <= 0:
112
- # time.sleep(3)
113
- # logger.info("No action to do")
114
- # return
115
-
116
111
payload ["DATA" ] = b64encodeX (json .dumps (data ))
117
112
payload ["Die_client_address" ] = b64encodeX (json .dumps (self .die_client_address ))
118
113
119
114
try :
120
- r = requests .post (WEBSHELL , data = payload )
115
+ r = requests .post (WEBSHELL , data = payload , verify = False )
121
116
except Exception as E :
122
117
logger .warning ("Post data to webshell failed" )
118
+ logger .exception (E )
123
119
return
124
120
125
121
try :
@@ -142,22 +138,29 @@ def sync_data(self):
142
138
client = cache_conns .get (client_address ).get ("conn" )
143
139
tcp_send_data = base64 .b64decode (web_return_data .get (client_address ).get ("data" ))
144
140
except Exception as E :
145
- logger .warning ("CLIENT_ADDRESS:{} Client socket closed " .format (client_address ))
141
+ logger .warning ("CLIENT_ADDRESS:{} server socket not in client socket list " .format (client_address ))
146
142
self .die_client_address .append (client_address )
147
143
continue
148
144
149
145
try :
150
146
client .send (tcp_send_data )
147
+ logger .debug ("CLIENT_ADDRESS:{} TCP_SEND_DATA:{}" .format (client_address , tcp_send_data ))
151
148
except Exception as E :
152
- logger .warning ("CLIENT_ADDRESS:{} Client socket closed " .format (client_address ))
149
+ logger .warning ("CLIENT_ADDRESS:{} Client socket send failed " .format (client_address ))
153
150
self .die_client_address .append (client_address )
154
- client .close ()
155
- cache_conns .pop (client_address )
151
+ try :
152
+ client .close ()
153
+ cache_conns .pop (client_address )
154
+ except Exception as E :
155
+ logger .exception (E )
156
+
156
157
# 检查没有在server返回列表中的client
157
- for client_address in list (cache_conns .keys ()):
158
- if web_return_data .get (client_address ) is None :
159
- logger .warning ("CLIENT_ADDRESS:{} remove client not in server cache_conns" .format (client_address ))
160
- self .die_client_address .append (client_address )
158
+
159
+ if CLEAN_DIE :
160
+ for client_address in list (cache_conns .keys ()):
161
+ if web_return_data .get (client_address ) is None :
162
+ logger .warning ("CLIENT_ADDRESS:{} remove client not in server cache_conns" .format (client_address ))
163
+ self .die_client_address .append (client_address )
161
164
162
165
163
166
class TCPClient (BaseRequestHandler ):
@@ -182,7 +185,15 @@ def handle(self):
182
185
except Exception as E :
183
186
LOG_LEVEL = "INFO"
184
187
logger = get_logger (level = LOG_LEVEL , name = "StreamLogger" )
185
-
188
+ try :
189
+ CLEAN_DIE_str = configini .get ("TOOL-CONFIG" , "CLEAN_DIE" )
190
+ if CLEAN_DIE_str .lower () == "true" :
191
+ CLEAN_DIE = True
192
+ else :
193
+ CLEAN_DIE = False
194
+ except Exception as E :
195
+ CLEAN_DIE = True
196
+ # read_buff_size
186
197
try :
187
198
READ_BUFF_SIZE = int (configini .get ("TOOL-CONFIG" , "READ_BUFF_SIZE" ))
188
199
except Exception as E :
@@ -199,6 +210,11 @@ def handle(self):
199
210
200
211
READ_BUFF_SIZE = 10240
201
212
213
+ # socket_timeout
214
+ try :
215
+ SOCKET_TIMEOUT = float (configini .get ("TOOL-CONFIG" , "SOCKET_TIMEOUT" ))
216
+ except Exception as E :
217
+ SOCKET_TIMEOUT = 0.1
202
218
# 获取核心参数
203
219
try :
204
220
WEBSHELL = configini .get ("NET-CONFIG" , "WEBSHELL" )
@@ -218,8 +234,8 @@ def handle(self):
218
234
219
235
logger .info (" ------------Client Config------------" )
220
236
logger .info (
221
- "\n LOG_LEVEL: {}\n SLEEP_TIME:{}\n READ_BUFF_SIZE: {}\n WEBSHELL: {}\n REMOTE_SERVER: {}\n LOCAL_ADDR: {}\n " .format (
222
- LOG_LEVEL , SLEEP_TIME , READ_BUFF_SIZE , WEBSHELL , REMOTE_SERVER , LOCAL_ADDR
237
+ "\n LOG_LEVEL: {}\n SLEEP_TIME:{}\n READ_BUFF_SIZE: {}\n WEBSHELL: {}\n REMOTE_SERVER: {}\n LOCAL_ADDR: {}\n SOCKET_TIMEOUT: {} \ n " .format (
238
+ LOG_LEVEL , SLEEP_TIME , READ_BUFF_SIZE , WEBSHELL , REMOTE_SERVER , LOCAL_ADDR , SOCKET_TIMEOUT
223
239
))
224
240
225
241
cache_conns = {}
0 commit comments