-
Notifications
You must be signed in to change notification settings - Fork 0
/
exploit.py
64 lines (58 loc) · 2.14 KB
/
exploit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import sys
import socket
import ssl
import threading
# X.509 Certificate Generation
def generate_cert(hostname):
# Generate a new RSA key pair
os.system(f"openssl genrsa -out {hostname}.key 2048")
# Generate a new certificate signing request (CSR)
os.system(f"openssl req -new -key {hostname}.key -out {hostname}.csr -subj '/CN={hostname}'")
# Sign the CSR with a self-signed certificate
os.system(f"openssl x509 -req -in {hostname}.csr -signkey {hostname}.key -out {hostname}.crt -days 365")
# SSL/TLS Server
def ssl_server(hostname, port):
# Generate SSL/TLS certificate and key
generate_cert(hostname)
# Create a socket object
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to a specific address and port
server_socket.bind((hostname, port))
# Listen for incoming connections
server_socket.listen(1)
print(f"[*] Listening on {hostname}:{port}")
while True:
# Accept a client connection
client_socket, address = server_socket.accept()
print(f"[*] Accepted connection from {address[0]}:{address[1]}")
# Wrap the client socket with SSL/TLS
ssl_socket = ssl.wrap_socket(client_socket, server_side=True, certfile=f"{hostname}.crt", keyfile=f"{hostname}.key")
# Handle the SSL/TLS connection
threading.Thread(target=handle_client, args=(ssl_socket,)).start()
# Client Handler
def handle_client(ssl_socket):
try:
while True:
# Receive data from the client
data = ssl_socket.recv(1024).decode()
if not data:
break
print(f"[*] Received: {data}")
# Send a response back to the client
ssl_socket.send(b"Hello from the server!")
except Exception as e:
print(f"[*] Error: {e}")
finally:
# Close the SSL/TLS connection
ssl_socket.close()
# Main function
def main():
if len(sys.argv) != 3:
print("Usage: python script.py <hostname> <port>")
sys.exit(1)
hostname = sys.argv[1]
port = int(sys.argv[2])
ssl_server(hostname, port)
if __name__ == "__main__":
main()