-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.py
57 lines (50 loc) · 2.05 KB
/
checker.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
import hashlib
import poll
poll.install()
from pyrad.client import Client
from pyrad.dictionary import Dictionary
from pyrad.packet import AccessRequest, AccessAccept
import sys
def read_config(file_path):
config = {}
with open(file_path, 'r') as file:
for line in file:
key, value = line.strip().split('=')
config[key] = value
return config
def authenticate_with_radius(config):
client = Client(server=config['IP'], secret=config['RadiusSecret'].encode(), dict=Dictionary("dictionary"))
if config['Method'] == 'CHAP':
# CHAP Implementation
chap_id = 1 # CHAP ID, typically a single octet
challenge = b'some_random_challenge' # The CHAP challenge
hashed = hashlib.md5(bytes([chap_id]) + config['Password'].encode() + challenge).digest()
# Create an Access-Request packet
req = client.CreateAuthPacket(code=AccessRequest, User_Name=config['Username'])
req["CHAP-Password"] = bytes([chap_id]) + hashed
req["CHAP-Challenge"] = challenge
req["Called-Station-Id"] = "hotspotname" #comment if unneeded
req["NAS-Port-Type"] = 19 #Wireless CCITT 802.11
elif config['Method'] == 'PAP':
# PAP Implementation
req = client.CreateAuthPacket(code=AccessRequest, User_Name=config['Username'])
req["User-Password"] = req.PwCrypt(config['Password'])
elif config['Method'] == 'MSCHAPv2':
# MSCHAPv2 Implementation (placeholder, requires actual implementation)
pass
else:
print("Unsupported authentication method")
return
# Send request
try:
reply = client.SendPacket(req)
if reply.code == AccessAccept:
print("Authentication successful")
else:
print("Authentication failed")
except Exception as e:
print(f"Failed to connect to the RADIUS server: {e}")
if __name__ == "__main__":
config_file = 'config.txt'
config = read_config(config_file)
authenticate_with_radius(config)