Search code examples
pythonpython-3.5radius

pyrad get plaintext password


I am trying to build a simple Radius server with pyrad. It works fine, but to check the user's password to my records I need the plaintext password at first.

My code:

    #!/usr/bin/python
from __future__ import print_function
from pyrad import dictionary, packet, server
import six
import logging
from pyrad.tools import *

from pyrad.packet import AuthPacket

try:
    import hashlib
    md5_constructor = hashlib.md5
except ImportError:
    # BBB for python 2.4
    import md5
    md5_constructor = md5.new

logging.basicConfig(filename="pyrad.log", level="DEBUG",
                    format="%(asctime)s [%(levelname)-8s] %(message)s")

def dump(obj):
   for attr in dir(obj):
       if hasattr( obj, attr ):
           print( "obj.%s = %s" % (attr, getattr(obj, attr)))

class FakeServer(server.Server):

    def HandleAuthPacket(self, pkt):

        pwd = pkt.PwDecrypt(pkt['User-Password'][0])
        uname = pkt['User-Name'][0]
        print (uname)
        print ('Plaintext PW: {}' . format(pwd))
        print("Received an authentication request")


        print("Attributes: ")
        for attr in pkt.keys():
            print("%s: %s" % (attr, pkt[attr]))

        reply = self.CreateReplyPacket(pkt, **{
            "Service-Type": "Framed-User",
            "Framed-IP-Address": '192.168.0.1',
            "Framed-IPv6-Prefix": "fc66::1/64"
        })

        reply.code = packet.AccessAccept
        self.SendReplyPacket(pkt.fd, reply)

    def HandleAcctPacket(self, pkt):

        print("Received an accounting request")
        print("Attributes: ")
        for attr in pkt.keys():
            print("%s: %s" % (attr, pkt[attr]))

        reply = self.CreateReplyPacket(pkt)
        self.SendReplyPacket(pkt.fd, reply)

    def HandleCoaPacket(self, pkt):

        print("Received an coa request")
        print("Attributes: ")
        for attr in pkt.keys():
            print("%s: %s" % (attr, pkt[attr]))

        reply = self.CreateReplyPacket(pkt)
        self.SendReplyPacket(pkt.fd, reply)

    def HandleDisconnectPacket(self, pkt):

        print("Received an disconnect request")
        print("Attributes: ")
        for attr in pkt.keys():
            print("%s: %s" % (attr, pkt[attr]))

        reply = self.CreateReplyPacket(pkt)
        # COA NAK
        reply.code = 45
        self.SendReplyPacket(pkt.fd, reply)

if __name__ == '__main__':

    # create server and read dictionary
    srv = FakeServer(dict=dictionary.Dictionary("dictionary"), coa_enabled=True)

    # add clients (address, secret, name)
    srv.hosts["xxx.xxx.xxx.xxx"] = server.RemoteHost("xxx.xxx.xxx.xxx", b"mysecretpw", "xxx.xxx.xxx.xxx")

    srv.BindToAddress("")

    # start server
    srv.Run()

It's output is:

b'thisistheusername\xe1\xe9'
Plaintext PW: b'\xc6NK\x18\xcb\xea\xd2Ne6t\xe1[p{\xb9'
Received an authentication request
Attributes:
User-Name: [b'thisistheusername\xe1\xe9']
User-Password: [b'r?\xbd7\xbd&\x9b\xdc17i\xc1\xc6\x95\xe6\xee']

I am testing with NTRadPing. The input were:

Username: thisistheusernameáé
Password: asdasd

So my problem is that I can't get the original plaintext password. What am I doing wrong?

pyrad server is running on Ubuntu Xenial.

You can find it in GitHub here: https://github.com/wichert/pyrad

Thank you very much!


Solution

  • I have found the solution!

    The code is perfect. The problem is that I've tried with to authenticate with a bad SECRET key.

    As the password is encypted into the packages, the code cant decrypt it with a bad secret key.