Skip to content

Commit

Permalink
增加SOCKET_TIMEOUT参数
Browse files Browse the repository at this point in the history
适配HTTPS网站
server端增加重试机制,增强socket健壮性
  • Loading branch information
zhaochengyu committed Oct 25, 2019
1 parent 1b18240 commit 00af7c3
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 50 deletions.
12 changes: 7 additions & 5 deletions config.ini
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
[NET-CONFIG]
WEBSHELL = http://192.168.3.10:82/proxy.php
SERVER_LISTEN = 127.0.0.1:8000
TARGET_ADDR = 127.0.0.1:3389
LOCAL_ADDR = 127.0.0.1:33899
WEBSHELL = http://192.168.3.10:8080/proxy.jsp
SERVER_LISTEN = 0.0.0.0:8000
TARGET_ADDR = 127.0.0.1:4445
LOCAL_ADDR = 0.0.0.0:4445

[TOOL-CONFIG]
LOG_LEVEL = INFO
READ_BUFF_SIZE = 10240
SLEEP_TIME = 0.1
SOCKET_TIMEOUT = 0.1
CLEAN_DIE = False

[ADVANCED-CONFIG]
SOCKS5 = False
REMOTE_SERVER = http://192.168.3.1:8000
REMOTE_SERVER = http://192.168.146.214:8000
NO_LOG = True
2 changes: 1 addition & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
DATA = "DATA"
WRONG_DATA = b"WRONG DATA" # 错误格式的数据
INVALID_CONN = b"REMOVE CONN" # 无效的连接
SOCKET_TIMEOUT = 0.01



# data = strings.Replace(strings.Replace(data, "\r\n", "", -1), "\n", "", -1)
Expand Down
64 changes: 40 additions & 24 deletions stinger_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
from threading import Thread

import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
from config import *

global LOG_LEVEL, SLEEP_TIME, READ_BUFF_SIZE, WEBSHELL, TARGET_ADDR, LOCAL_ADDR
global LOG_LEVEL, SLEEP_TIME, READ_BUFF_SIZE, WEBSHELL, TARGET_ADDR, LOCAL_ADDR, CLEAN_DIE, SOCKET_TIMEOUT
global cache_conns, die_client_address, not_in_server_cache_conns


Expand Down Expand Up @@ -53,7 +55,7 @@ def check_server(self):
}
for i in range(5):
try:
r = requests.post(WEBSHELL, data=payload, timeout=3)
r = requests.post(WEBSHELL, data=payload, timeout=3, verify=False)
web_return_data = json.loads(b64decodeX(r.content).decode("utf-8"))
except Exception as E:
logger.error(r.content)
Expand Down Expand Up @@ -84,42 +86,36 @@ def sync_data(self):
}
data = {}
# 清除无效的client

for client_address in self.die_client_address:
try:
one = cache_conns.pop(client_address)
one.get("conn").close()
logger.warning("CLIENT_ADDRESS:{} close client not in server cache_conns ".format(client_address))
logger.warning("CLIENT_ADDRESS:{} close client in die_client_address".format(client_address))
except Exception as E:
logger.warning(
"CLIENT_ADDRESS:{} close client not in server cache_conns error".format(client_address))
"CLIENT_ADDRESS:{} close client close client in die_client_address error".format(client_address))

has_data_to_send = False
for client_address in list(cache_conns.keys()):
client = cache_conns.get(client_address).get("conn")
try:
tcp_recv_data = client.recv(READ_BUFF_SIZE)
logger.debug("CLIENT_ADDRESS:{} TCP_RECV_DATA:{}".format(client_address, tcp_recv_data))
if len(tcp_recv_data) > 0:
has_data_to_send = True
logger.info("CLIENT_ADDRESS:{} TCP_RECV_LEN:{}".format(client_address, len(tcp_recv_data)))
except Exception as err:
tcp_recv_data = b""
logger.debug("TCP_RECV_NONE")
data[client_address] = {"data": base64.b64encode(tcp_recv_data)}

# 如果没有数据需要发送
# if has_data_to_send is not True and len(self.die_client_address) <= 0:
# time.sleep(3)
# logger.info("No action to do")
# return

payload["DATA"] = b64encodeX(json.dumps(data))
payload["Die_client_address"] = b64encodeX(json.dumps(self.die_client_address))

try:
r = requests.post(WEBSHELL, data=payload)
r = requests.post(WEBSHELL, data=payload, verify=False)
except Exception as E:
logger.warning("Post data to webshell failed")
logger.exception(E)
return

try:
Expand All @@ -142,22 +138,29 @@ def sync_data(self):
client = cache_conns.get(client_address).get("conn")
tcp_send_data = base64.b64decode(web_return_data.get(client_address).get("data"))
except Exception as E:
logger.warning("CLIENT_ADDRESS:{} Client socket closed".format(client_address))
logger.warning("CLIENT_ADDRESS:{} server socket not in client socket list".format(client_address))
self.die_client_address.append(client_address)
continue

try:
client.send(tcp_send_data)
logger.debug("CLIENT_ADDRESS:{} TCP_SEND_DATA:{}".format(client_address, tcp_send_data))
except Exception as E:
logger.warning("CLIENT_ADDRESS:{} Client socket closed".format(client_address))
logger.warning("CLIENT_ADDRESS:{} Client socket send failed".format(client_address))
self.die_client_address.append(client_address)
client.close()
cache_conns.pop(client_address)
try:
client.close()
cache_conns.pop(client_address)
except Exception as E:
logger.exception(E)

# 检查没有在server返回列表中的client
for client_address in list(cache_conns.keys()):
if web_return_data.get(client_address) is None:
logger.warning("CLIENT_ADDRESS:{} remove client not in server cache_conns".format(client_address))
self.die_client_address.append(client_address)

if CLEAN_DIE:
for client_address in list(cache_conns.keys()):
if web_return_data.get(client_address) is None:
logger.warning("CLIENT_ADDRESS:{} remove client not in server cache_conns".format(client_address))
self.die_client_address.append(client_address)


class TCPClient(BaseRequestHandler):
Expand All @@ -182,7 +185,15 @@ def handle(self):
except Exception as E:
LOG_LEVEL = "INFO"
logger = get_logger(level=LOG_LEVEL, name="StreamLogger")

try:
CLEAN_DIE_str = configini.get("TOOL-CONFIG", "CLEAN_DIE")
if CLEAN_DIE_str.lower() == "true":
CLEAN_DIE = True
else:
CLEAN_DIE = False
except Exception as E:
CLEAN_DIE = True
# read_buff_size
try:
READ_BUFF_SIZE = int(configini.get("TOOL-CONFIG", "READ_BUFF_SIZE"))
except Exception as E:
Expand All @@ -199,6 +210,11 @@ def handle(self):

READ_BUFF_SIZE = 10240

# socket_timeout
try:
SOCKET_TIMEOUT = float(configini.get("TOOL-CONFIG", "SOCKET_TIMEOUT"))
except Exception as E:
SOCKET_TIMEOUT = 0.1
# 获取核心参数
try:
WEBSHELL = configini.get("NET-CONFIG", "WEBSHELL")
Expand All @@ -218,8 +234,8 @@ def handle(self):

logger.info(" ------------Client Config------------")
logger.info(
"\nLOG_LEVEL: {}\nSLEEP_TIME:{}\nREAD_BUFF_SIZE: {}\nWEBSHELL: {}\nREMOTE_SERVER: {}\nLOCAL_ADDR: {}\n".format(
LOG_LEVEL, SLEEP_TIME, READ_BUFF_SIZE, WEBSHELL, REMOTE_SERVER, LOCAL_ADDR
"\nLOG_LEVEL: {}\nSLEEP_TIME:{}\nREAD_BUFF_SIZE: {}\nWEBSHELL: {}\nREMOTE_SERVER: {}\nLOCAL_ADDR: {}\nSOCKET_TIMEOUT: {}\n".format(
LOG_LEVEL, SLEEP_TIME, READ_BUFF_SIZE, WEBSHELL, REMOTE_SERVER, LOCAL_ADDR,SOCKET_TIMEOUT
))

cache_conns = {}
Expand Down
69 changes: 49 additions & 20 deletions stinger_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from config import *

global cache_conns, server_die_client_address
global READ_BUFF_SIZE, LOG_LEVEL, SERVER_LISTEN, TARGET_ADDR, SOCKS5
global READ_BUFF_SIZE, LOG_LEVEL, SERVER_LISTEN, TARGET_ADDR, SOCKS5, SOCKET_TIMEOUT


# import SocketServer
Expand Down Expand Up @@ -183,35 +183,48 @@ def sync():
logger.warning("CLIENT_ADDRESS:{} Create new tcp socket".format(client_address))
cache_conns[client_address] = {"conn": client}
except Exception as E:

logger.warning(
"CLIENT_ADDRESS:{} TARGET_ADDR:{} create new socket failed".format(client_address, TARGET_ADDR))
"CLIENT_ADDRESS:{} TARGET_ADDR:{} Create new socket failed".format(client_address, TARGET_ADDR))
continue
else:
client = cache_conns.get(client_address).get("conn")

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

# 发送数据
try:
client.sendall(tcp_send_data)
except Exception as E: # socket 已失效
logger.warning("CLIENT_ADDRESS:{} client send failed".format(client_address))
send_flag = False
for i in range(20):
# 发送数据
try:
client.sendall(tcp_send_data)
logger.info("CLIENT_ADDRESS:{} TCP_SEND_LEN:{}".format(client_address, len(tcp_send_data)))
send_flag = True
break
except Exception as E: # socket 已失效
logger.warning("CLIENT_ADDRESS:{} Client send failed".format(client_address))
logger.exception(E)

if send_flag is not True:
try:
client.close()
cache_conns.pop(client_address)
except Exception as E:
logger.exception(E)
continue
# 读取数据
continue

try:
tcp_recv_data = client.recv(READ_BUFF_SIZE)
web_return_data[client_address] = {"data": base64.b64encode(tcp_recv_data)}
logger.debug("CLIENT_ADDRESS:{} TCP_RECV_DATA:{}".format(client_address, tcp_recv_data))
if len(tcp_recv_data) > 0:
logger.info("CLIENT_ADDRESS:{} TCP_RECV_LEN:{}".format(client_address, len(tcp_recv_data)))
except Exception as err:
revc_flag = False
for i in range(3):
# 读取数据
try:
tcp_recv_data = client.recv(READ_BUFF_SIZE)
web_return_data[client_address] = {"data": base64.b64encode(tcp_recv_data)}
logger.debug("CLIENT_ADDRESS:{} TCP_RECV_DATA:{}".format(client_address, tcp_recv_data))
if len(tcp_recv_data) > 0:
logger.info("CLIENT_ADDRESS:{} TCP_RECV_LEN:{}".format(client_address, len(tcp_recv_data)))
revc_flag = True
break
except Exception as err:
pass
if revc_flag is not True:
tcp_recv_data = b""
web_return_data[client_address] = {"data": base64.b64encode(tcp_recv_data)}
logger.debug("TCP_RECV_NONE")
Expand Down Expand Up @@ -240,7 +253,7 @@ def sync():
logger = get_logger(level=LOG_LEVEL, name="FileLogger")
except Exception as E:
logger = get_logger(level=LOG_LEVEL, name="FileLogger")

# read_buff_size
try:
READ_BUFF_SIZE = int(configini.get("TOOL-CONFIG", "READ_BUFF_SIZE"))
except Exception as E:
Expand All @@ -256,6 +269,22 @@ def sync():
except Exception as E:
SOCKS5 = False

# socket_timeout
try:
SOCKET_TIMEOUT = float(configini.get("TOOL-CONFIG", "SOCKET_TIMEOUT"))
except Exception as E:
SOCKET_TIMEOUT = 0.1

# socks5
try:
socks5_on = configini.get("ADVANCED-CONFIG", "SOCKS5")
if socks5_on.lower() == "true":
SOCKS5 = True
else:
SOCKS5 = False
except Exception as E:
SOCKS5 = False

# 获取核心参数
try:
SERVER_LISTEN = configini.get("NET-CONFIG", "SERVER_LISTEN")
Expand All @@ -265,8 +294,8 @@ def sync():
sys.exit(1)

logger.info(
"\nLOG_LEVEL: {}\nREAD_BUFF_SIZE: {}\nSERVER_LISTEN: {}\nTARGET_ADDR: {}\nSOCKS5: {}\n".format(
LOG_LEVEL, READ_BUFF_SIZE, SERVER_LISTEN, TARGET_ADDR, SOCKS5
"\nLOG_LEVEL: {}\nREAD_BUFF_SIZE: {}\nSERVER_LISTEN: {}\nTARGET_ADDR: {}\nSOCKS5: {}\nSOCKET_TIMEOUT: {}\n".format(
LOG_LEVEL, READ_BUFF_SIZE, SERVER_LISTEN, TARGET_ADDR, SOCKS5, SOCKET_TIMEOUT
))

cache_conns = {}
Expand Down

0 comments on commit 00af7c3

Please sign in to comment.