-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
70 lines (58 loc) · 1.89 KB
/
Copy pathclient.py
File metadata and controls
70 lines (58 loc) · 1.89 KB
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
65
66
67
68
69
70
#!/usr/bin/env python3
"""
client.py – Secure VPN Client over AES-encrypted TCP Tunnel
This script connects to a VPN server, sets up a TUN interface,
and securely transmits encrypted packets between client and server.
Author: Moniish Mohansrinivasan
Usage:
sudo python3 client.py
"""
import socket
from Crypto.Cipher import AES
import fcntl
import struct
import os
import select
import time
# --- Encryption Key ---
KEY = b"ThisIsASecretKey"
# --- AES Encryption & Decryption Functions ---
def encrypt(data):
cipher = AES.new(KEY, AES.MODE_EAX)
ciphertext, tag = cipher.encrypt_and_digest(data)
return cipher.nonce + tag + ciphertext
def decrypt(data):
nonce, tag, ciphertext = data[:16], data[16:32], data[32:]
cipher = AES.new(KEY, AES.MODE_EAX, nonce=nonce)
return cipher.decrypt_and_verify(ciphertext, tag)
# --- TUN Device Setup ---
IFF_TUN = 0x0001
IFF_NO_PI = 0x1000
TUNSETIFF = 0x400454ca
tun = os.open("/dev/net/tun", os.O_RDWR)
ifr = struct.pack('16sH', b'tun1', IFF_TUN | IFF_NO_PI)
fcntl.ioctl(tun, TUNSETIFF, ifr)
os.system("ip link set tun1 up")
print("[+] Client TUN device ready")
# --- Server Connection ---
server_ip = "127.0.0.1" # Replace with your server’s IP or hotspot IP
server_port = 9999
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((server_ip, server_port))
client_ip = s.recv(12).decode().strip()
os.system(f"ip addr add {client_ip}/24 dev tun1")
print(f"[+] Connected to server {server_ip}:{server_port} → Assigned IP: {client_ip}")
# --- Main Data Loop ---
tun_sockets = [tun, s]
while True:
readables, _, _ = select.select(tun_sockets, [], [], 1.0)
for fd in readables:
if fd == tun:
packet = os.read(tun, 4096)
s.send(encrypt(packet))
else:
data = s.recv(4096)
if data:
packet = decrypt(data)
os.write(tun, packet)
time.sleep(0.01)