This commit is contained in:
José Henrique 2023-12-06 17:00:05 -03:00
parent 5dc8a4406e
commit f4f5690ff8
4 changed files with 76 additions and 20 deletions

57
trabalho5/forbidden.html Normal file
View File

@ -0,0 +1,57 @@
<!DOCTYPE html>
<html>
<head>
<title>Forbidden</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f1f1f1;
margin: 0;
padding: 0;
}
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
text-align: center;
}
h1 {
color: #333;
font-size: 24px;
margin-bottom: 10px;
}
p {
color: #666;
font-size: 16px;
margin-bottom: 20px;
}
.btn {
display: inline-block;
padding: 10px 20px;
background-color: #333;
color: #fff;
text-decoration: none;
border-radius: 3px;
transition: background-color 0.3s;
}
.btn:hover {
background-color: #555;
}
</style>
</head>
<body>
<div class="container">
<h1>403 Forbidden</h1>
<p>Access to the requested resource is forbidden.</p>
<p>Contact support.</p>
</div>
</body>
</html>

View File

@ -41,7 +41,6 @@ LOGGING = {
}
config.dictConfig(LOGGING)
class Logger:
_instance = None
def __init__(self):
@ -51,7 +50,7 @@ class Logger:
self.logger = logging.getLogger('PythonProxy')
self.logger.info('Initiating Proxy logger!')
def log(self, message:str):
def info(self, message:str):
self.logger.info(message)
def critical(self, message:str):
@ -113,9 +112,9 @@ def connectionHandle(client_socket, client_address):
client_socket.close()
return
if "CONNECT" in raw_request:
client_socket.sendall(b"HTTP/1.1 200 Connection Established\r\n\r\n")
request = client_socket.recv(16 * 1024)
# if "CONNECT" in raw_request:
# client_socket.sendall(b"HTTP/1.1 200 Connection Established\r\n\r\n")
# request = client_socket.recv(16 * 1024)
request_url = raw_request.split(' ')[1]
request_host = ""
@ -134,11 +133,11 @@ def connectionHandle(client_socket, client_address):
request_host = request_host.split(':')[0]
if "monitorando" in request_url:
body = "<HTML><HEAD><TITLE>403</TITLE><meta charset=\"UTF-8\"></HEAD> <BODY>Acesso não autorizado!</BODY> </HTML>"
body = open('forbidden.html', 'r').read()
client_socket.sendall(b"HTTP/1.1 403 Forbidden\r\n\r\n")
client_socket.sendall(body.encode())
client_socket.close()
logger.log(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}] - 403")
logger.info(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}] - 403 Forbidden")
return
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@ -146,7 +145,7 @@ def connectionHandle(client_socket, client_address):
server_socket.send(request)
while True:
triple = select.select([client_socket, server_socket], [], [], 20)[0]
triple = select.select([client_socket, server_socket], [], [], 10)[0]
if not len(triple):
break
try:
@ -159,9 +158,9 @@ def connectionHandle(client_socket, client_address):
status_code = data.decode().split('\r\n')[0].split(' ')[1:]
status_code = ' '.join(status_code)
if is_valid_status_code(status_code):
logger.log(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}] - {status_code}")
logger.info(f"PROXY [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}] - {status_code}")
except UnicodeDecodeError:
logger.log(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}]")
logger.info(f"PROXY [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}]")
pass
client_socket.send(data)
@ -182,28 +181,28 @@ def verify_code_integrity():
import sys
content = None
md5_hash = None
shas256_hash = None
with open(sys.argv[0], 'rb') as f:
content = f.read()
with open('proxy.py.md5', 'r') as f:
md5_hash = f.read()
with open('proxy.sha256sum', 'r') as f:
shas256_hash = f.read()
hash = hashlib.md5(content).hexdigest()
hash = hashlib.sha256(content).hexdigest()
if hash != md5_hash:
print('ERROR: proxy.py has been tampered with!')
if hash != shas256_hash:
print('ERRO: Código do proxy está diferente!')
exit(1)
print('[Code Integrity] proxy.py is verified!')
print('Arquivo do proxy verificado!')
if __name__ == '__main__':
verify_code_integrity()
# verify_code_integrity()
try:
ser = Server(host="0.0.0.0", port=8080)
ser.start()
except KeyboardInterrupt:
print("Shutting down...")
print("Desligando proxy...")
exit(0)

View File

@ -1 +0,0 @@
bd693dcd1e1a0f178fcd0cf0aa50efaf

View File

@ -0,0 +1 @@
24d2be62b48be63f31093585f52f7887