Skip to content

Commit 00af7c3

Browse files
author
zhaochengyu
committed
增加SOCKET_TIMEOUT参数
适配HTTPS网站 server端增加重试机制,增强socket健壮性
1 parent 1b18240 commit 00af7c3

File tree

4 files changed

+97
-50
lines changed

4 files changed

+97
-50
lines changed

config.ini

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
[NET-CONFIG]
2-
WEBSHELL = http://192.168.3.10:82/proxy.php
3-
SERVER_LISTEN = 127.0.0.1:8000
4-
TARGET_ADDR = 127.0.0.1:3389
5-
LOCAL_ADDR = 127.0.0.1:33899
2+
WEBSHELL = http://192.168.3.10:8080/proxy.jsp
3+
SERVER_LISTEN = 0.0.0.0:8000
4+
TARGET_ADDR = 127.0.0.1:4445
5+
LOCAL_ADDR = 0.0.0.0:4445
66

77
[TOOL-CONFIG]
88
LOG_LEVEL = INFO
99
READ_BUFF_SIZE = 10240
1010
SLEEP_TIME = 0.1
11+
SOCKET_TIMEOUT = 0.1
12+
CLEAN_DIE = False
1113

1214
[ADVANCED-CONFIG]
1315
SOCKS5 = False
14-
REMOTE_SERVER = http://192.168.3.1:8000
16+
REMOTE_SERVER = http://192.168.146.214:8000
1517
NO_LOG = True

config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
DATA = "DATA"
1414
WRONG_DATA = b"WRONG DATA" # 错误格式的数据
1515
INVALID_CONN = b"REMOVE CONN" # 无效的连接
16-
SOCKET_TIMEOUT = 0.01
16+
1717

1818

1919
# data = strings.Replace(strings.Replace(data, "\r\n", "", -1), "\n", "", -1)

stinger_client.py

+40-24
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
from threading import Thread
2323

2424
import requests
25+
from requests.packages.urllib3.exceptions import InsecureRequestWarning
2526

27+
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
2628
from config import *
2729

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
2931
global cache_conns, die_client_address, not_in_server_cache_conns
3032

3133

@@ -53,7 +55,7 @@ def check_server(self):
5355
}
5456
for i in range(5):
5557
try:
56-
r = requests.post(WEBSHELL, data=payload, timeout=3)
58+
r = requests.post(WEBSHELL, data=payload, timeout=3, verify=False)
5759
web_return_data = json.loads(b64decodeX(r.content).decode("utf-8"))
5860
except Exception as E:
5961
logger.error(r.content)
@@ -84,42 +86,36 @@ def sync_data(self):
8486
}
8587
data = {}
8688
# 清除无效的client
89+
8790
for client_address in self.die_client_address:
8891
try:
8992
one = cache_conns.pop(client_address)
9093
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))
9295
except Exception as E:
9396
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))
9598

96-
has_data_to_send = False
9799
for client_address in list(cache_conns.keys()):
98100
client = cache_conns.get(client_address).get("conn")
99101
try:
100102
tcp_recv_data = client.recv(READ_BUFF_SIZE)
101103
logger.debug("CLIENT_ADDRESS:{} TCP_RECV_DATA:{}".format(client_address, tcp_recv_data))
102104
if len(tcp_recv_data) > 0:
103-
has_data_to_send = True
104105
logger.info("CLIENT_ADDRESS:{} TCP_RECV_LEN:{}".format(client_address, len(tcp_recv_data)))
105106
except Exception as err:
106107
tcp_recv_data = b""
107108
logger.debug("TCP_RECV_NONE")
108109
data[client_address] = {"data": base64.b64encode(tcp_recv_data)}
109110

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-
116111
payload["DATA"] = b64encodeX(json.dumps(data))
117112
payload["Die_client_address"] = b64encodeX(json.dumps(self.die_client_address))
118113

119114
try:
120-
r = requests.post(WEBSHELL, data=payload)
115+
r = requests.post(WEBSHELL, data=payload, verify=False)
121116
except Exception as E:
122117
logger.warning("Post data to webshell failed")
118+
logger.exception(E)
123119
return
124120

125121
try:
@@ -142,22 +138,29 @@ def sync_data(self):
142138
client = cache_conns.get(client_address).get("conn")
143139
tcp_send_data = base64.b64decode(web_return_data.get(client_address).get("data"))
144140
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))
146142
self.die_client_address.append(client_address)
147143
continue
148144

149145
try:
150146
client.send(tcp_send_data)
147+
logger.debug("CLIENT_ADDRESS:{} TCP_SEND_DATA:{}".format(client_address, tcp_send_data))
151148
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))
153150
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+
156157
# 检查没有在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)
161164

162165

163166
class TCPClient(BaseRequestHandler):
@@ -182,7 +185,15 @@ def handle(self):
182185
except Exception as E:
183186
LOG_LEVEL = "INFO"
184187
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
186197
try:
187198
READ_BUFF_SIZE = int(configini.get("TOOL-CONFIG", "READ_BUFF_SIZE"))
188199
except Exception as E:
@@ -199,6 +210,11 @@ def handle(self):
199210

200211
READ_BUFF_SIZE = 10240
201212

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
202218
# 获取核心参数
203219
try:
204220
WEBSHELL = configini.get("NET-CONFIG", "WEBSHELL")
@@ -218,8 +234,8 @@ def handle(self):
218234

219235
logger.info(" ------------Client Config------------")
220236
logger.info(
221-
"\nLOG_LEVEL: {}\nSLEEP_TIME:{}\nREAD_BUFF_SIZE: {}\nWEBSHELL: {}\nREMOTE_SERVER: {}\nLOCAL_ADDR: {}\n".format(
222-
LOG_LEVEL, SLEEP_TIME, READ_BUFF_SIZE, WEBSHELL, REMOTE_SERVER, LOCAL_ADDR
237+
"\nLOG_LEVEL: {}\nSLEEP_TIME:{}\nREAD_BUFF_SIZE: {}\nWEBSHELL: {}\nREMOTE_SERVER: {}\nLOCAL_ADDR: {}\nSOCKET_TIMEOUT: {}\n".format(
238+
LOG_LEVEL, SLEEP_TIME, READ_BUFF_SIZE, WEBSHELL, REMOTE_SERVER, LOCAL_ADDR,SOCKET_TIMEOUT
223239
))
224240

225241
cache_conns = {}

stinger_server.py

+49-20
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from config import *
3434

3535
global cache_conns, server_die_client_address
36-
global READ_BUFF_SIZE, LOG_LEVEL, SERVER_LISTEN, TARGET_ADDR, SOCKS5
36+
global READ_BUFF_SIZE, LOG_LEVEL, SERVER_LISTEN, TARGET_ADDR, SOCKS5, SOCKET_TIMEOUT
3737

3838

3939
# import SocketServer
@@ -183,35 +183,48 @@ def sync():
183183
logger.warning("CLIENT_ADDRESS:{} Create new tcp socket".format(client_address))
184184
cache_conns[client_address] = {"conn": client}
185185
except Exception as E:
186-
187186
logger.warning(
188-
"CLIENT_ADDRESS:{} TARGET_ADDR:{} create new socket failed".format(client_address, TARGET_ADDR))
187+
"CLIENT_ADDRESS:{} TARGET_ADDR:{} Create new socket failed".format(client_address, TARGET_ADDR))
189188
continue
190189
else:
191190
client = cache_conns.get(client_address).get("conn")
192191

193192
tcp_send_data = base64.b64decode(data.get(client_address).get("data"))
194193

195-
# 发送数据
196-
try:
197-
client.sendall(tcp_send_data)
198-
except Exception as E: # socket 已失效
199-
logger.warning("CLIENT_ADDRESS:{} client send failed".format(client_address))
194+
send_flag = False
195+
for i in range(20):
196+
# 发送数据
197+
try:
198+
client.sendall(tcp_send_data)
199+
logger.info("CLIENT_ADDRESS:{} TCP_SEND_LEN:{}".format(client_address, len(tcp_send_data)))
200+
send_flag = True
201+
break
202+
except Exception as E: # socket 已失效
203+
logger.warning("CLIENT_ADDRESS:{} Client send failed".format(client_address))
204+
logger.exception(E)
205+
206+
if send_flag is not True:
200207
try:
201208
client.close()
202209
cache_conns.pop(client_address)
203210
except Exception as E:
204211
logger.exception(E)
205-
continue
206-
# 读取数据
212+
continue
207213

208-
try:
209-
tcp_recv_data = client.recv(READ_BUFF_SIZE)
210-
web_return_data[client_address] = {"data": base64.b64encode(tcp_recv_data)}
211-
logger.debug("CLIENT_ADDRESS:{} TCP_RECV_DATA:{}".format(client_address, tcp_recv_data))
212-
if len(tcp_recv_data) > 0:
213-
logger.info("CLIENT_ADDRESS:{} TCP_RECV_LEN:{}".format(client_address, len(tcp_recv_data)))
214-
except Exception as err:
214+
revc_flag = False
215+
for i in range(3):
216+
# 读取数据
217+
try:
218+
tcp_recv_data = client.recv(READ_BUFF_SIZE)
219+
web_return_data[client_address] = {"data": base64.b64encode(tcp_recv_data)}
220+
logger.debug("CLIENT_ADDRESS:{} TCP_RECV_DATA:{}".format(client_address, tcp_recv_data))
221+
if len(tcp_recv_data) > 0:
222+
logger.info("CLIENT_ADDRESS:{} TCP_RECV_LEN:{}".format(client_address, len(tcp_recv_data)))
223+
revc_flag = True
224+
break
225+
except Exception as err:
226+
pass
227+
if revc_flag is not True:
215228
tcp_recv_data = b""
216229
web_return_data[client_address] = {"data": base64.b64encode(tcp_recv_data)}
217230
logger.debug("TCP_RECV_NONE")
@@ -240,7 +253,7 @@ def sync():
240253
logger = get_logger(level=LOG_LEVEL, name="FileLogger")
241254
except Exception as E:
242255
logger = get_logger(level=LOG_LEVEL, name="FileLogger")
243-
256+
# read_buff_size
244257
try:
245258
READ_BUFF_SIZE = int(configini.get("TOOL-CONFIG", "READ_BUFF_SIZE"))
246259
except Exception as E:
@@ -256,6 +269,22 @@ def sync():
256269
except Exception as E:
257270
SOCKS5 = False
258271

272+
# socket_timeout
273+
try:
274+
SOCKET_TIMEOUT = float(configini.get("TOOL-CONFIG", "SOCKET_TIMEOUT"))
275+
except Exception as E:
276+
SOCKET_TIMEOUT = 0.1
277+
278+
# socks5
279+
try:
280+
socks5_on = configini.get("ADVANCED-CONFIG", "SOCKS5")
281+
if socks5_on.lower() == "true":
282+
SOCKS5 = True
283+
else:
284+
SOCKS5 = False
285+
except Exception as E:
286+
SOCKS5 = False
287+
259288
# 获取核心参数
260289
try:
261290
SERVER_LISTEN = configini.get("NET-CONFIG", "SERVER_LISTEN")
@@ -265,8 +294,8 @@ def sync():
265294
sys.exit(1)
266295

267296
logger.info(
268-
"\nLOG_LEVEL: {}\nREAD_BUFF_SIZE: {}\nSERVER_LISTEN: {}\nTARGET_ADDR: {}\nSOCKS5: {}\n".format(
269-
LOG_LEVEL, READ_BUFF_SIZE, SERVER_LISTEN, TARGET_ADDR, SOCKS5
297+
"\nLOG_LEVEL: {}\nREAD_BUFF_SIZE: {}\nSERVER_LISTEN: {}\nTARGET_ADDR: {}\nSOCKS5: {}\nSOCKET_TIMEOUT: {}\n".format(
298+
LOG_LEVEL, READ_BUFF_SIZE, SERVER_LISTEN, TARGET_ADDR, SOCKS5, SOCKET_TIMEOUT
270299
))
271300

272301
cache_conns = {}

0 commit comments

Comments
 (0)