caterpillar/server.py

649 lines
23 KiB
Python
Raw Normal View History

2024-02-19 05:13:35 +00:00
# Caterpillar - The simple and parasitic web proxy with spam filter
2024-02-17 08:44:19 +00:00
# Namyheon Go (Catswords Research) <gnh1201@gmail.com>
2024-02-19 04:45:40 +00:00
# https://github.com/gnh1201/caterpillar
# Created at: 2022-10-06
2024-02-24 10:13:08 +00:00
# Updated at: 2024-12-24
2022-10-05 17:19:38 +00:00
import argparse
import socket
import sys
2022-11-24 09:02:32 +00:00
import os
2022-10-05 17:19:38 +00:00
from _thread import *
import base64
2022-10-08 04:03:15 +00:00
import json
2022-11-24 09:02:32 +00:00
import ssl
2022-11-25 08:12:58 +00:00
import time
2024-02-17 12:21:24 +00:00
import re
2024-02-18 22:58:44 +00:00
import hashlib
2024-02-18 04:20:14 +00:00
import resource
2024-02-22 08:16:07 +00:00
import traceback
2024-02-19 07:07:55 +00:00
import io
2024-02-23 06:02:14 +00:00
import textwrap
2022-11-25 08:12:58 +00:00
from subprocess import Popen, PIPE
2022-10-05 17:19:38 +00:00
from datetime import datetime
2022-10-06 12:09:34 +00:00
from platform import python_version
2024-02-19 07:07:55 +00:00
from PIL import Image
2022-10-05 17:19:38 +00:00
2022-10-06 12:09:34 +00:00
import requests
2022-10-05 17:19:38 +00:00
from decouple import config
try:
listening_port = config('PORT', cast=int)
2022-11-24 09:05:58 +00:00
server_url = config('SERVER_URL')
2022-11-25 12:35:02 +00:00
cakey = config('CA_KEY')
cacert = config('CA_CERT')
certkey = config('CERT_KEY')
certdir = config('CERT_DIR')
2022-11-25 12:54:25 +00:00
openssl_binpath = config('OPENSSL_BINPATH')
2022-11-25 12:35:02 +00:00
client_encoding = config('CLIENT_ENCODING')
2024-02-17 19:04:02 +00:00
local_domain = config('LOCAL_DOMAIN')
proxy_pass = config('PROXY_PASS')
2024-02-19 05:24:47 +00:00
mastodon_server = config('MASTODON_SERVER') # catswords.social
mastodon_user_token = config('MASTODON_USER_TOKEN') # catswords.social
truecaptcha_userid = config('TRUECAPTCHA_USERID') # truecaptcha.org
truecaptcha_apikey = config('TRUECAPTCHA_APIKEY') # truecaptcha.org
2024-02-23 09:33:42 +00:00
librey_apiurl = config("LIBREY_APIURL") # https://github.com/Ahwxorg/librey
2022-10-05 17:19:38 +00:00
except KeyboardInterrupt:
print("\n[*] User has requested an interrupt")
print("[*] Application Exiting.....")
sys.exit()
parser = argparse.ArgumentParser()
2024-02-17 09:35:35 +00:00
parser.add_argument('--max_conn', help="Maximum allowed connections", default=255, type=int)
2024-02-17 09:39:11 +00:00
parser.add_argument('--buffer_size', help="Number of samples to be used", default=8192, type=int)
2022-10-05 17:19:38 +00:00
args = parser.parse_args()
max_connection = args.max_conn
buffer_size = args.buffer_size
2024-02-18 04:20:14 +00:00
# https://stackoverflow.com/questions/25475906/set-ulimit-c-from-outside-shell
resource.setrlimit(
resource.RLIMIT_CORE,
(resource.RLIM_INFINITY, resource.RLIM_INFINITY))
2024-02-24 16:31:42 +00:00
# load data to use KnownWords4 strategy
2024-02-22 05:21:55 +00:00
# Download data: https://github.com/dwyl/english-words
known_words = []
if os.path.exists("words_alpha.txt"):
with open("words_alpha.txt", "r") as file:
words = file.readlines()
2024-02-24 16:31:42 +00:00
known_words = [word.strip() for word in words if len(word.strip()) > 3]
print ("[*] data loaded to use KnownWords4 strategy")
2024-02-22 05:21:55 +00:00
2022-10-05 17:19:38 +00:00
def start(): #Main Program
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('', listening_port))
sock.listen(max_connection)
print("[*] Server started successfully [ %d ]" %(listening_port))
except Exception:
print("[*] Unable to Initialize Socket")
print(Exception)
sys.exit(2)
while True:
try:
conn, addr = sock.accept() #Accept connection from client browser
data = conn.recv(buffer_size) #Recieve client data
2022-11-25 08:12:58 +00:00
start_new_thread(conn_string, (conn, data, addr)) #Starting a thread
2022-10-05 17:19:38 +00:00
except KeyboardInterrupt:
sock.close()
print("\n[*] Graceful Shutdown")
sys.exit(1)
2024-02-26 06:04:05 +00:00
def jsonrpc2_encode(method, params):
2024-02-25 17:37:11 +00:00
data = {
"jsonrpc": "2.0",
"method": method,
"params": params
}
id = "0x%s" % (hashlib.sha1(json.dumps(data).encoding(client_encoding)).hexdigest())
data['id'] = id
return json.dumps(data)
2024-02-23 03:53:44 +00:00
def parse_first_data(data):
2024-02-23 05:40:15 +00:00
parsed_data = (b'', b'', b'', b'', b'')
2024-02-23 03:53:44 +00:00
2022-11-25 10:32:17 +00:00
try:
first_line = data.split(b'\n')[0]
method, url = first_line.split()[0:2]
http_pos = url.find(b'://') #Finding the position of ://
scheme = b'http' # check http/https or other protocol
if http_pos == -1:
temp = url
else:
temp = url[(http_pos+3):]
scheme = url[0:http_pos]
port_pos = temp.find(b':')
webserver_pos = temp.find(b'/')
if webserver_pos == -1:
webserver_pos = len(temp)
2024-02-17 19:04:02 +00:00
webserver = b''
2022-11-25 10:32:17 +00:00
port = -1
if port_pos == -1 or webserver_pos < port_pos:
port = 80
webserver = temp[:webserver_pos]
else:
port = int((temp[(port_pos+1):])[:webserver_pos-port_pos-1])
webserver = temp[:port_pos]
if port == 443:
scheme = b'https'
2024-02-23 03:53:44 +00:00
parsed_data = (webserver, port, scheme, method, url)
2022-11-25 10:32:17 +00:00
except Exception as e:
conn.close()
2024-02-18 18:26:46 +00:00
print("[*] Exception on parsing the header of %s. Cause: %s" % (str(addr[0]), str(e)))
2024-02-23 03:53:44 +00:00
return parsed_data
def conn_string(conn, data, addr):
2024-02-26 07:40:45 +00:00
# check is it JSON-RPC 2.0 request
if data.find(b'{') == 0:
context = json.loads(data.decode(client_encoding))
if "jsonrpc" in context:
pass # todo
2024-02-23 05:40:15 +00:00
# parse first data (header)
2024-02-23 03:53:44 +00:00
webserver, port, scheme, method, url = parse_first_data(data)
2022-11-25 08:12:58 +00:00
2024-02-17 19:04:02 +00:00
# if it is reverse proxy
2024-02-18 01:13:58 +00:00
if local_domain != '':
localserver = local_domain.encode(client_encoding)
if webserver == localserver or data.find(b'\nHost: ' + localserver) > -1:
2024-02-22 08:16:07 +00:00
print ("[*] Detected the reverse proxy request: %s" % (local_domain))
2024-02-18 01:13:58 +00:00
scheme, _webserver, _port = proxy_pass.encode(client_encoding).split(b':')
webserver = _webserver[2:]
port = int(_port.decode(client_encoding))
2024-02-17 19:04:02 +00:00
2022-11-25 08:12:58 +00:00
proxy_server(webserver, port, scheme, method, url, conn, addr, data)
def proxy_connect(webserver, conn):
2022-11-25 12:35:02 +00:00
hostname = webserver.decode(client_encoding)
2022-11-25 08:12:58 +00:00
certpath = "%s/%s.crt" % (certdir.rstrip('/'), hostname)
2022-11-25 10:32:17 +00:00
# https://stackoverflow.com/questions/24055036/handle-https-request-in-proxy-server-by-c-sharp-connect-tunnel
conn.send(b'HTTP/1.1 200 Connection Established\r\n\r\n')
2022-11-25 08:12:58 +00:00
2022-11-25 10:32:17 +00:00
# https://github.com/inaz2/proxy2/blob/master/proxy2.py
2022-10-05 17:19:38 +00:00
try:
2022-11-25 08:12:58 +00:00
if not os.path.isfile(certpath):
epoch = "%d" % (time.time() * 1000)
2022-11-25 12:54:25 +00:00
p1 = Popen([openssl_binpath, "req", "-new", "-key", certkey, "-subj", "/CN=%s" % hostname], stdout=PIPE)
p2 = Popen([openssl_binpath, "x509", "-req", "-days", "3650", "-CA", cacert, "-CAkey", cakey, "-set_serial", epoch, "-out", certpath], stdin=p1.stdout, stderr=PIPE)
2022-11-25 08:12:58 +00:00
p2.communicate()
2022-10-06 02:24:19 +00:00
except Exception as e:
2024-02-18 18:26:46 +00:00
print("[*] Skipped generating the certificate. Cause: %s" % (str(e)))
2022-11-25 08:12:58 +00:00
2022-11-25 10:32:17 +00:00
# https://stackoverflow.com/questions/11255530/python-simple-ssl-socket-server
# https://docs.python.org/3/library/ssl.html
2022-11-25 08:12:58 +00:00
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain(certpath, certkey)
2022-11-25 10:32:17 +00:00
# https://stackoverflow.com/questions/11255530/python-simple-ssl-socket-server
2022-11-25 08:12:58 +00:00
conn = context.wrap_socket(conn, server_side=True)
2022-11-25 10:32:17 +00:00
data = conn.recv(buffer_size)
2022-10-05 17:19:38 +00:00
2022-11-25 10:32:17 +00:00
return (conn, data)
2022-11-24 09:02:32 +00:00
2024-02-17 19:19:54 +00:00
def proxy_check_filtered(data, webserver, port, scheme, method, url):
2024-02-17 12:50:36 +00:00
filtered = False
2024-02-17 12:21:24 +00:00
2024-02-17 23:15:17 +00:00
# prevent cache confusing
2024-02-17 23:27:23 +00:00
if data.find(b'<title>Welcome to nginx!</title>') > -1:
2024-02-17 23:15:17 +00:00
return True
2024-02-17 19:04:02 +00:00
# allowed conditions
2024-02-17 19:24:33 +00:00
if method == b'GET' or url.find(b'/api') > -1:
2024-02-17 23:15:17 +00:00
return False
2024-02-17 19:04:02 +00:00
2024-02-17 17:10:01 +00:00
# convert to text
2024-02-20 16:52:26 +00:00
data_length = len(data)
2024-02-18 17:26:04 +00:00
text = data.decode(client_encoding, errors='ignore')
2024-02-20 16:52:26 +00:00
error_rate = (data_length - len(text)) / data_length
2024-02-22 02:40:25 +00:00
if error_rate > 0.2: # it is a binary data
2024-02-20 16:52:26 +00:00
return False
2024-02-17 16:49:29 +00:00
2024-02-22 02:40:25 +00:00
# check ID with K-Anonymity strategy
2024-02-18 23:26:53 +00:00
pattern = r'\b(?:(?<=\/@)|(?<=acct:))([a-zA-Z0-9]{10})\b'
2024-02-18 23:08:41 +00:00
matches = list(set(re.findall(pattern, text)))
2024-02-18 22:58:44 +00:00
if len(matches) > 0:
2024-02-18 23:10:20 +00:00
print ("[*] Found ID: %s" % (', '.join(matches)))
2024-02-20 02:03:25 +00:00
try:
filtered = not all(map(pwnedpasswords_test, matches))
except Exception as e:
print ("[*] K-Anonymity strategy not working! %s" % (str(e)))
filtered = True
2024-02-17 16:49:29 +00:00
2024-02-22 05:21:55 +00:00
# feedback
2024-02-20 05:28:21 +00:00
if filtered and len(matches) > 0:
2024-02-22 05:21:55 +00:00
score = 0
2024-02-24 10:13:08 +00:00
strategies = []
2024-02-22 05:21:55 +00:00
# check ID with VowelRatio10 strategy
2024-02-20 05:28:21 +00:00
def vowel_ratio_test(s):
ratio = calculate_vowel_ratio(s)
2024-02-24 11:30:39 +00:00
return ratio > 0.2 and ratio < 0.8
2024-02-22 05:21:55 +00:00
if all(map(vowel_ratio_test, matches)):
score += 1
2024-02-24 10:13:08 +00:00
strategies.append('VowelRatio10')
2024-02-20 05:28:21 +00:00
2024-02-24 15:13:54 +00:00
# check ID with Palindrome4 strategy
2024-02-22 05:21:55 +00:00
if all(map(has_palindrome, matches)):
score += 1
2024-02-24 15:13:54 +00:00
strategies.append('Palindrome4')
2024-02-22 05:21:55 +00:00
2024-02-24 15:13:54 +00:00
# check ID with KnownWords4 strategy
2024-02-22 05:21:55 +00:00
if all(map(has_known_word, matches)):
2024-02-22 05:28:17 +00:00
score += 2
2024-02-24 15:13:54 +00:00
strategies.append('KnownWords4')
2024-02-22 05:21:55 +00:00
2024-02-23 10:24:35 +00:00
# check ID with SearchEngine3 strategy
2024-02-23 09:41:25 +00:00
if librey_apiurl != '' and all(map(search_engine_test, matches)):
2024-02-23 09:33:42 +00:00
score += 1
2024-02-24 10:13:08 +00:00
strategies.append('SearchEngine3')
2024-02-23 09:33:42 +00:00
2024-02-24 16:18:21 +00:00
# check ID with RepeatedNumbers3 strategy
if all(map(repeated_numbers_test, matches)):
score += 1
strategies.append('RepeatedNumbers3')
2024-02-23 10:24:35 +00:00
# logging score
with open('score.log', 'a') as file:
2024-02-24 10:13:08 +00:00
file.write("%s\t%s\t%s\r\n" % ('+'.join(matches), str(score), '+'.join(strategies)))
2024-02-23 10:24:35 +00:00
2024-02-22 05:21:55 +00:00
# make decision
if score > 1:
filtered = False
2024-02-20 09:08:50 +00:00
2024-02-20 05:49:47 +00:00
# check an attached images (check images with Not-CAPTCHA strategy)
2024-02-23 09:41:25 +00:00
if truecaptcha_userid != '' and not filtered and len(matches) > 0:
2024-02-19 07:07:55 +00:00
def webp_to_png_base64(url):
2024-02-19 06:06:16 +00:00
try:
response = requests.get(url)
2024-02-19 07:07:55 +00:00
img = Image.open(io.BytesIO(response.content))
img_png = img.convert("RGBA")
buffered = io.BytesIO()
img_png.save(buffered, format="PNG")
encoded_image = base64.b64encode(buffered.getvalue()).decode(client_encoding)
return encoded_image
2024-02-19 06:06:16 +00:00
except:
2024-02-19 07:07:55 +00:00
return None
2024-02-19 06:00:32 +00:00
2024-02-19 07:25:07 +00:00
urls = re.findall(r'https://[^\s"]+\.webp', text)
2024-02-19 06:06:16 +00:00
if len(urls) > 0:
for url in urls:
2024-02-22 02:40:25 +00:00
if filtered:
break
2024-02-20 02:03:25 +00:00
print ("[*] downloading... %s" % (url))
encoded_image = webp_to_png_base64(url)
print ("[*] downloaded.")
if encoded_image:
print ("[*] solving...")
try:
2024-02-19 07:18:47 +00:00
solved = truecaptcha_solve(encoded_image)
2024-02-19 07:16:41 +00:00
if solved:
print ("[*] solved: %s" % (solved))
2024-02-23 10:24:35 +00:00
filtered = filtered or (solved.lower() in ['ctkpaarr', 'spam'])
2024-02-19 07:16:41 +00:00
else:
print ("[*] not solved")
2024-02-20 02:03:25 +00:00
except Exception as e:
print ("[*] Not CAPTCHA strategy not working! %s" % (str(e)))
2024-02-19 06:00:32 +00:00
2024-02-18 22:58:44 +00:00
# take action
2024-02-17 12:53:55 +00:00
if filtered:
2024-02-18 08:54:28 +00:00
print ("[*] Filtered from %s:%s" % (webserver.decode(client_encoding), str(port)))
try:
2024-02-18 09:04:53 +00:00
savedir = './savedfiles'
if not os.path.exists(savedir):
os.makedirs(savedir)
2024-02-18 08:54:28 +00:00
current_time = datetime.now().strftime("%Y%m%d%H%M%S")
2024-02-18 09:04:53 +00:00
file_path = os.path.join(savedir, ("%s_%s.bin" % (current_time, webserver.decode(client_encoding))))
2024-02-18 08:54:28 +00:00
with open(file_path, 'wb') as file:
file.write(data)
print ("[*] Saved the file: %s" % (file_path))
except Exception as e:
print ("[*] Failed to save the file: %s" % (str(e)))
2024-02-17 08:44:19 +00:00
return filtered
2024-02-17 05:40:33 +00:00
2022-11-24 09:02:32 +00:00
def proxy_server(webserver, port, scheme, method, url, conn, addr, data):
2022-10-05 17:19:38 +00:00
try:
2024-02-17 05:26:27 +00:00
print("[*] Started the request. %s" % (str(addr[0])))
2022-10-08 04:23:40 +00:00
2024-02-18 03:59:24 +00:00
# SSL negotiation
2024-02-22 08:16:07 +00:00
is_ssl = scheme in [b'https', b'tls', b'ssl']
if is_ssl and method == b'CONNECT':
2024-02-18 03:59:24 +00:00
while True:
try:
conn, data = proxy_connect(webserver, conn)
break # success
2024-02-18 04:43:59 +00:00
#except OSError as e:
# print ("[*] Retrying SSL negotiation... (%s:%s) %s" % (webserver.decode(client_encoding), str(port), str(e)))
2024-02-18 03:59:24 +00:00
except Exception as e:
raise Exception("SSL negotiation failed. (%s:%s) %s" % (webserver.decode(client_encoding), str(port), str(e)))
2022-11-24 09:02:32 +00:00
2024-02-23 04:15:49 +00:00
# override data
2024-02-23 03:59:22 +00:00
if is_ssl:
2024-02-23 06:02:14 +00:00
_, _, _, method, url = parse_first_data(data)
2024-02-23 03:59:22 +00:00
2024-02-22 08:16:07 +00:00
# https://stackoverflow.com/questions/44343739/python-sockets-ssl-eof-occurred-in-violation-of-protocol
def sock_close(sock, is_ssl = False):
#if is_ssl:
# sock = sock.unwrap()
#sock.shutdown(socket.SHUT_RDWR)
sock.close()
2024-02-17 21:23:58 +00:00
# Wait to see if there is more data to transmit
2024-02-18 17:16:25 +00:00
def sendall(sock, conn, data):
2024-02-18 17:10:13 +00:00
# send first chuck
2024-02-18 17:37:37 +00:00
if proxy_check_filtered(data, webserver, port, scheme, method, url):
2024-02-18 18:24:22 +00:00
sock.close()
2024-02-18 17:37:37 +00:00
raise Exception("Filtered request")
2024-02-18 17:10:13 +00:00
sock.send(data)
2024-02-18 17:05:28 +00:00
if len(data) < buffer_size:
return
2024-02-18 17:10:13 +00:00
# send following chunks
2024-02-18 17:05:28 +00:00
buffered = b''
2024-02-17 22:41:17 +00:00
conn.settimeout(1)
2024-02-17 21:23:58 +00:00
while True:
try:
chunk = conn.recv(buffer_size)
if not chunk:
break
2024-02-18 17:05:28 +00:00
buffered += chunk
if proxy_check_filtered(buffered, webserver, port, scheme, method, url):
2024-02-22 08:16:07 +00:00
sock_close(sock, is_ssl)
2024-02-18 17:05:28 +00:00
raise Exception("Filtered request")
2024-02-18 17:10:13 +00:00
sock.send(chunk)
2024-02-18 17:26:04 +00:00
if len(buffered) > buffer_size*2:
2024-02-20 02:03:25 +00:00
buffered = buffered[-buffer_size*2:]
2024-02-17 21:23:58 +00:00
except:
break
2024-02-18 16:50:43 +00:00
# do response
2024-02-17 05:23:51 +00:00
if server_url == "localhost":
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
2024-02-17 05:40:33 +00:00
2024-02-22 08:16:07 +00:00
if is_ssl:
2024-02-17 08:44:19 +00:00
context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
2024-02-17 05:40:33 +00:00
2024-02-17 08:44:19 +00:00
sock = context.wrap_socket(sock, server_hostname=webserver.decode(client_encoding))
sock.connect((webserver, port))
2024-02-18 17:05:28 +00:00
#sock.sendall(data)
2024-02-18 17:16:25 +00:00
sendall(sock, conn, data)
2024-02-17 08:44:19 +00:00
else:
sock.connect((webserver, port))
2024-02-18 17:05:28 +00:00
#sock.sendall(data)
2024-02-18 17:16:25 +00:00
sendall(sock, conn, data)
2024-02-17 05:23:51 +00:00
2024-02-17 05:26:27 +00:00
i = 0
2024-02-22 08:16:07 +00:00
is_http_403 = False
2024-02-18 16:48:15 +00:00
buffered = b''
2024-02-17 05:23:51 +00:00
while True:
2024-02-17 08:44:19 +00:00
chunk = sock.recv(buffer_size)
2024-02-17 05:26:27 +00:00
if not chunk:
2024-02-17 05:23:51 +00:00
break
2024-02-23 03:53:44 +00:00
if i == 0 and chunk.find(b'HTTP/1.1 403') == 0:
is_http_403 = True
break
2024-02-18 16:48:15 +00:00
buffered += chunk
if proxy_check_filtered(buffered, webserver, port, scheme, method, url):
2024-02-22 08:16:07 +00:00
sock_close(sock, is_ssl)
2024-02-18 16:50:43 +00:00
add_filtered_host(webserver.decode(client_encoding), '127.0.0.1')
2024-02-18 17:05:28 +00:00
raise Exception("Filtered response")
2024-02-18 16:48:15 +00:00
conn.send(chunk)
2024-02-18 17:26:04 +00:00
if len(buffered) > buffer_size*2:
2024-02-20 02:03:25 +00:00
buffered = buffered[-buffer_size*2:]
2024-02-17 05:40:33 +00:00
i += 1
2024-02-17 05:26:27 +00:00
2024-02-22 08:16:07 +00:00
# when blocked
if is_http_403:
print ("[*] Blocked the request by remote server: %s" % (webserver.decode(client_encoding)))
2024-02-23 05:06:01 +00:00
def bypass_callback(response, *args, **kwargs):
if response.status_code != 200:
conn.sendall(b"HTTP/1.1 403 Forbidden\r\n\r\n{\"status\":403}")
return
# https://stackoverflow.com/questions/20658572/python-requests-print-entire-http-request-raw
format_headers = lambda d: '\r\n'.join(f'{k}: {v}' for k, v in d.items())
2024-02-23 06:02:14 +00:00
first_data = textwrap.dedent('HTTP/1.1 {res.status_code} {res.reason}\r\n{reshdrs}\r\n\r\n').format(
2024-02-23 05:06:01 +00:00
res=response,
reshdrs=format_headers(response.headers),
).encode(client_encoding)
conn.send(first_data)
for chunk in response.iter_content(chunk_size=buffer_size):
conn.send(chunk)
if is_ssl and method == b'GET':
print ("[*] Trying to bypass blocked request...")
2024-02-23 06:02:14 +00:00
remote_url = "%s://%s%s" % (scheme.decode(client_encoding), webserver.decode(client_encoding), url.decode(client_encoding))
2024-02-23 09:33:42 +00:00
requests.get(remote_url, stream=True, verify=False, hooks={'response': bypass_callback})
2024-02-23 05:06:01 +00:00
else:
conn.sendall(b"HTTP/1.1 403 Forbidden\r\n\r\n{\"status\":403}")
2024-02-22 08:16:07 +00:00
sock_close(sock, is_ssl)
2024-02-17 12:08:02 +00:00
print("[*] Received %s chunks. (%s bytes per chunk)" % (str(i), str(buffer_size)))
2024-02-17 08:44:19 +00:00
2024-02-17 05:23:51 +00:00
else:
2024-02-17 08:44:19 +00:00
2024-02-17 05:23:51 +00:00
proxy_data = {
'headers': {
2024-02-25 17:37:11 +00:00
"User-Agent": "php-httpproxy/0.2.0-dev (Client; Python " + python_version() + "; abuse@catswords.net)",
2024-02-17 05:23:51 +00:00
},
'data': {
2024-02-26 02:41:19 +00:00
"buffer_size": str(buffer_size),
2024-02-26 05:11:09 +00:00
"request_data": base64.b64encode(data).decode(client_encoding),
"request_length": str(len(data)),
2024-02-26 04:07:02 +00:00
"client_address": str(addr[0]),
"client_port": str(listening_port),
2024-02-26 02:41:19 +00:00
"client_encoding": client_encoding,
2024-02-26 04:02:48 +00:00
"remote_address": webserver.decode(client_encoding),
2024-02-26 02:41:19 +00:00
"remote_port": str(port),
2024-02-17 05:23:51 +00:00
"scheme": scheme.decode(client_encoding),
"url": url.decode(client_encoding),
"datetime": datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
}
2022-11-24 09:02:32 +00:00
}
2024-02-26 06:04:05 +00:00
raw_data = jsonrpc2_encode(proxy_data['request_data'])
2024-02-17 05:40:33 +00:00
2024-02-17 05:23:51 +00:00
print("[*] Sending %s bytes..." % (str(len(raw_data))))
2024-02-17 05:40:33 +00:00
2024-02-17 05:23:51 +00:00
i = 0
relay = requests.post(server_url, headers=proxy_data['headers'], data=raw_data, stream=True)
2024-02-20 02:03:25 +00:00
buffered = b''
2024-02-17 05:23:51 +00:00
for chunk in relay.iter_content(chunk_size=buffer_size):
2024-02-18 16:48:15 +00:00
buffered += chunk
if proxy_check_filtered(buffered, webserver, port, scheme, method, url):
2024-02-18 16:50:43 +00:00
add_filtered_host(webserver.decode(client_encoding), '127.0.0.1')
2024-02-18 17:05:28 +00:00
raise Exception("Filtered response")
2024-02-18 16:48:15 +00:00
conn.send(chunk)
2024-02-18 17:26:04 +00:00
if len(buffered) > buffer_size*2:
2024-02-20 02:03:25 +00:00
buffered = buffered[-buffer_size*2:]
2024-02-17 05:40:33 +00:00
i += 1
2024-02-17 12:08:02 +00:00
print("[*] Received %s chunks. (%s bytes per chunk)" % (str(i), str(buffer_size)))
2022-10-05 17:19:38 +00:00
2024-02-17 05:26:27 +00:00
print("[*] Request and received. Done. %s" % (str(addr[0])))
2022-10-05 17:19:38 +00:00
conn.close()
2022-11-25 08:12:58 +00:00
except Exception as e:
2024-02-22 08:16:07 +00:00
print(traceback.format_exc())
2024-02-18 17:05:28 +00:00
print("[*] Exception on requesting the data. Cause: %s" % (str(e)))
2024-02-23 03:48:00 +00:00
conn.sendall(b"HTTP/1.1 403 Forbidden\r\n\r\n{\"status\":403}")
2022-10-05 17:19:38 +00:00
conn.close()
2024-02-18 08:16:14 +00:00
# journaling a filtered hosts
def add_filtered_host(domain, ip_address):
hosts_path = './filtered.hosts'
2024-02-17 16:49:29 +00:00
with open(hosts_path, 'r') as file:
lines = file.readlines()
domain_exists = any(domain in line for line in lines)
if not domain_exists:
lines.append(f"{ip_address}\t{domain}\n")
with open(hosts_path, 'w') as file:
file.writelines(lines)
2024-02-19 05:24:47 +00:00
if mastodon_user_token != '': # notify to catswords.social
post_status_to_mastodon(f"[{mastodon_server} user]\r\n\r\n{domain} is a domain with suspicious spam activity.\r\n\r\n#catswords")
2024-02-18 08:16:14 +00:00
2024-02-19 05:24:47 +00:00
# notify to mastodon server
def post_status_to_mastodon(text, media_ids=None, poll_options=None, poll_expires_in=None, scheduled_at=None, idempotency_key=None):
url = f"https://{mastodon_server}/api/v1/statuses"
2024-02-18 08:16:14 +00:00
headers = {
"Authorization": f"Bearer {user_token}",
"Content-Type": "application/x-www-form-urlencoded",
}
form_data = {
"status": text,
"media_ids[]": media_ids,
"poll[options][]": poll_options,
"poll[expires_in]": poll_expires_in,
"scheduled_at": scheduled_at,
}
if idempotency_key:
headers["Idempotency-Key"] = idempotency_key
response = requests.post(url, headers=headers, data=form_data)
return response.json()
2024-02-17 16:49:29 +00:00
2024-02-20 05:28:21 +00:00
# Strategy: K-Anonymity test - use api.pwnedpasswords.com
2024-02-19 05:27:02 +00:00
def pwnedpasswords_test(s):
2024-02-22 05:49:36 +00:00
# convert to lowercase
2024-02-22 08:16:07 +00:00
s = s.lower()
2024-02-22 05:49:36 +00:00
2024-02-18 22:58:44 +00:00
# SHA1 of the password
p_sha1 = hashlib.sha1(s.encode()).hexdigest()
# First 5 char of SHA1 for k-anonymity API use
f5_sha1 = p_sha1[:5]
# Last 5 char of SHA1 to match API output
l5_sha1 = p_sha1[-5:]
# Making GET request using Requests library
response = requests.get(f'https://api.pwnedpasswords.com/range/{f5_sha1}')
# Checking if request was successful
if response.status_code == 200:
# Parsing response text
hashes = response.text.split('\r\n')
# Using list comprehension to find matching hashes
2024-02-19 09:56:41 +00:00
matching_hashes = [line.split(':')[0] for line in hashes if line.endswith(l5_sha1)]
2024-02-18 22:58:44 +00:00
# If there are matching hashes, return True, else return False
return bool(matching_hashes)
else:
2024-02-20 02:03:25 +00:00
raise Exception("api.pwnedpasswords.com response status: %s" % (str(response.status_code)))
return False
2024-02-18 22:58:44 +00:00
2024-02-20 05:48:54 +00:00
# Strategy: Not-CAPTCHA - use truecaptcha.org
2024-02-19 07:08:32 +00:00
def truecaptcha_solve(encoded_image):
2024-02-19 05:29:41 +00:00
url = 'https://api.apitruecaptcha.org/one/gettext'
2024-02-20 02:03:25 +00:00
data = {
'userid': truecaptcha_userid,
'apikey': truecaptcha_apikey,
2024-02-19 07:08:32 +00:00
'data': encoded_image,
2024-02-19 05:30:40 +00:00
'mode': 'human'
2024-02-19 05:29:41 +00:00
}
response = requests.post(url = url, json = data)
2024-02-19 07:16:41 +00:00
2024-02-20 02:03:25 +00:00
if response.status_code == 200:
data = response.json()
if 'error_message' in data:
print ("[*] Error: %s" % (data['error_message']))
return None
if 'result' in data:
return data['result']
else:
raise Exception("api.apitruecaptcha.org response status: %s" % (str(response.status_code)))
2024-02-19 07:16:41 +00:00
return None
2024-02-19 05:29:41 +00:00
2024-02-20 05:48:54 +00:00
# Strategy: VowelRatio10
2024-02-20 05:28:21 +00:00
def calculate_vowel_ratio(s):
# Calculate the length of the string.
length = len(s)
if length == 0:
return 0.0
2024-02-21 07:01:04 +00:00
# Count the number of vowels ('a', 'e', 'i', 'o', 'u', 'w', 'y') in the string.
vowel_count = sum(1 for char in s if char.lower() in 'aeiouwy')
2024-02-20 05:28:21 +00:00
2024-02-24 10:55:49 +00:00
# Define vowel-ending patterns
2024-02-24 11:07:49 +00:00
vowel_ending_patterns = ['ang', 'eng', 'ing', 'ong', 'ung', 'ank', 'ink', 'dge']
2024-02-24 10:55:49 +00:00
# Count the occurrences of vowel-ending patterns in the string.
vowel_count += sum(s.count(pattern) for pattern in vowel_ending_patterns)
2024-02-20 05:28:21 +00:00
# Calculate the ratio of vowels to the total length of the string.
vowel_ratio = vowel_count / length
return vowel_ratio
2024-02-24 15:13:54 +00:00
# Strategy: Palindrome4
2024-02-20 09:08:50 +00:00
def has_palindrome(input_string):
def is_palindrome(s):
return s == s[::-1]
2024-02-22 05:42:50 +00:00
input_string = input_string.lower()
2024-02-20 09:08:50 +00:00
n = len(input_string)
for i in range(n):
2024-02-24 15:13:54 +00:00
for j in range(i + 4, n + 1): # Find substrings of at least 5 characters
2024-02-20 09:08:50 +00:00
substring = input_string[i:j]
if is_palindrome(substring):
return True
return False
2024-02-24 15:13:54 +00:00
# Strategy: KnownWords4
2024-02-22 05:21:55 +00:00
def has_known_word(input_string):
def is_known_word(s):
return s in known_words
2024-02-22 05:42:50 +00:00
input_string = input_string.lower()
2024-02-22 05:21:55 +00:00
n = len(input_string)
for i in range(n):
2024-02-24 15:13:54 +00:00
for j in range(i + 4, n + 1): # Find substrings of at least 5 characters
2024-02-22 05:21:55 +00:00
substring = input_string[i:j]
if is_known_word(substring):
return True
return False
2024-02-23 10:24:35 +00:00
# Strategy: SearchEngine3
2024-02-23 09:33:42 +00:00
def search_engine_test(s):
url = "%s/api.php?q=%s" % (librey_apiurl, s)
response = requests.get(url, verify=False)
if response.status_code != 200:
return False
data = response.json()
if 'results_source' in data:
del data['results_source']
num_results = len(data)
2024-02-23 10:24:35 +00:00
return num_results > 2
2024-02-23 09:33:42 +00:00
2024-02-24 16:18:21 +00:00
# Strategy: RepeatedNumbers3
def repeated_numbers_test(s):
2024-02-24 16:36:14 +00:00
return bool(re.search(r'\d{3,}', s))
2024-02-24 16:18:21 +00:00
2022-10-05 17:19:38 +00:00
if __name__== "__main__":
start()