我从哪里获取 Golang 中 Radius MSCHAPv2 中的密码?

Where do I get the password in Radius MSCHAPv2 in Golang?

我不知道如何编辑this Radius MSCHAPv2 example and get the password from the radius client. The go library I'm using is https://github.com/layeh/radius

MSCHAPv2 是否发送带密码的用户名?如何从客户端获取到服务器的密码?

package microsoft

import (
    "log"
    "reflect"

    "layeh.com/radius"
    "layeh.com/radius/rfc2759"
    "layeh.com/radius/rfc2865"
    "layeh.com/radius/rfc2868"
    "layeh.com/radius/rfc2869"
    "layeh.com/radius/rfc3079"
)

const (
    radiusSecret = "secret"
)

func RunRadiusServer() {
    handler := func(w radius.ResponseWriter, r *radius.Request) {
        username := rfc2865.UserName_GetString(r.Packet)
        challenge := MSCHAPChallenge_Get(r.Packet)
        response := MSCHAP2Response_Get(r.Packet)

        // TODO: look up user in local database.
        // The password must be stored in the clear for CHAP mechanisms to work.
        // In theory, it would be possible to use a password hashed with MD4 as
        // all the functions in MSCHAPv2 use the MD4 hash of the password anyway,
        // but given that MD4 is so vulerable that breaking a hash is almost as
        // fast as computing it, it's just not worth it.
        password := "password-from-database"

        if len(challenge) == 16 && len(response) == 50 {
            // See rfc2548 - 2.3.2. MS-CHAP2-Response
            ident := response[0]
            peerChallenge := response[2:18]
            peerResponse := response[26:50]
            ntResponse, err := rfc2759.GenerateNTResponse(challenge, peerChallenge, username, password)
            if err != nil {
                log.Printf("Cannot generate ntResponse for %s: %v", username, err)
                w.Write(r.Response(radius.CodeAccessReject))
                return
            }

            if reflect.DeepEqual(ntResponse, peerResponse) {
                responsePacket := r.Response(radius.CodeAccessAccept)

                recvKey, err := rfc3079.MakeKey(ntResponse, password, false)
                if err != nil {
                    log.Printf("Cannot make recvKey for %s: %v", username, err)
                    w.Write(r.Response(radius.CodeAccessReject))
                    return
                }

                sendKey, err := rfc3079.MakeKey(ntResponse, password, true)
                if err != nil {
                    log.Printf("Cannot make sendKey for %s: %v", username, err)
                    w.Write(r.Response(radius.CodeAccessReject))
                    return
                }

                authenticatorResponse, err := rfc2759.GenerateAuthenticatorResponse(challenge, peerChallenge, ntResponse, username, password)
                if err != nil {
                    log.Printf("Cannot generate authenticator response for %s: %v", username, err)
                    w.Write(r.Response(radius.CodeAccessReject))
                    return
                }

                success := make([]byte, 43)
                success[0] = ident
                copy(success[1:], authenticatorResponse)

                rfc2869.AcctInterimInterval_Add(responsePacket, rfc2869.AcctInterimInterval(3600))
                rfc2868.TunnelType_Add(responsePacket, 0, rfc2868.TunnelType_Value_L2TP)
                rfc2868.TunnelMediumType_Add(responsePacket, 0, rfc2868.TunnelMediumType_Value_IPv4)
                MSCHAP2Success_Add(responsePacket, []byte(success))
                MSMPPERecvKey_Add(responsePacket, recvKey)
                MSMPPESendKey_Add(responsePacket, sendKey)
                MSMPPEEncryptionPolicy_Add(responsePacket, MSMPPEEncryptionPolicy_Value_EncryptionAllowed)
                MSMPPEEncryptionTypes_Add(responsePacket, MSMPPEEncryptionTypes_Value_RC440or128BitAllowed)

                log.Printf("Access granted to %s", username)
                w.Write(responsePacket)
                return
            }
        }

        log.Printf("Access denied for %s", username)
        w.Write(r.Response(radius.CodeAccessReject))
    }

    server := radius.PacketServer{
        Handler:      radius.HandlerFunc(handler),
        SecretSource: radius.StaticSecretSource([]byte(radiusSecret)),
    }

    log.Printf("Starting Radius server on :1812")
    if err := server.ListenAndServe(); err != nil {
        log.Fatal(err)
    }
}

我编辑了函数的顶部:

username := rfc2865.UserName_GetString(r.Packet)
password := rfc2865.UserPassword_GetString(r.Packet)
log.Printf("bytes %+v", r.Get(1), string(r.Get(1)))
log.Printf("bytes %+v", r.Get(2), string(r.Get(2)))
log.Printf("u/p %v, %v\n", username, password)

用户名正确。密码为空。 1 是用户名。我无法对 2 进行排序,因为它不会将字节转换为字符串。

有一个类似但不是 Go 特定的问题 here 也没有正确回答。谢谢!

这不是我特别熟悉的机制,但我预计 CHAP 协议不会通过网络发送密码,因此您无法从客户端获取密码。相反,您生成一个预期的响应,知道客户端应该使用的密码,并将其与您从客户端获得的响应进行比较 - 如果它们匹配,则使用相同的秘密密码生成两个响应,一切都很好,否则不匹配某个地方,你不授予访问权限。

在 MSCHAPv2 中,客户端发送用户密码哈希。无法访问明文密码。如果您需要进行身份验证 - 您需要从用户身份存储中获取密码,以相同的方式对其进行哈希处理并比较两个哈希值 - 你的和从客户端获得的。

具体客户端发送:

  • MSCHAP-Challenge 包含 16 字节挑战
  • MSCHAP-Response 包含:
    • 16 字节客户端挑战
    • 8 字节保留
    • 24 字节 NT 响应 = DesEncrypt(SHA1(客户端挑战,服务器挑战,用户名),MD4Hash(密码))
    • 1 字节标志“使用 NT 质询响应或 LAN 质询响应”

服务器从 ID 存储中获取 MSCHAP-Challenge、客户端质询和用户密码并计算 NT-response。如果等于 received – 服务器发送 Access Accept,否则 Access Reject