更改用户的authKey

问题描述 投票:0回答:2

使用 SNMP 版本 3,我正在创建一个用户。 现在,我已经在克隆用户的地方设置了它,并且效果很好。但是,我需要更改新用户的 authKey。我怎样才能做到这一点?我知道 authKeyChange 的 oid,但是,我不知道如何生成新密钥。我如何生成该密钥?可以使用 SNMPSharpNet 来完成吗? 如果在创建用户时有更简单的方法来执行此操作,我也可以这样做。非常感谢任何更改 authKey(和 privKey,但一次一步)的方法。我正在使用 VB.net,如果这有什么意义的话。

snmp snmpsharpnet
2个回答
0
投票

所以我已经弄清楚如何做到这一点。这是一个有点复杂的过程。我遵循了this文档,即rfc2574。对“keyChange ::=”执行 ctrl+F,您会发现该段落将引导您完成生成 keyChange 值的算法。以下代码可以可靠地生成 keyChange 值。从此时起,您所要做的就是将 keyChange 值推送到 usmAuthKeyChange OID。如果您要更改隐私密码,请将 keyChange 值推送到 usmPrivKeyChange OID。我很惭愧地说,由于时间紧迫,我没有时间完全完成这项工作,因此在使用 SHA 时,我不得不编写一个全新的方法来完成几乎完全相同的事情。再说一次,我很羞于发布它,但我知道我有多么用头撞墙,如果有人后来来这里看到这个,我希望他们知道该怎么做而不经历挣扎。

以下是使用 VB.Net 和 SNMPSharpNet 库所需的所有代码:

Private Function GenerateKeyChange(ByVal newPass As String, ByVal oldPass As String, ByRef target As UdpTarget, ByRef param As SecureAgentParameters) As Byte()

    Dim authProto As AuthenticationDigests = param.Authentication
    Dim hash As IAuthenticationDigest = Authentication.GetInstance(authProto)
    Dim L As Integer = hash.DigestLength
    Dim oldKey() As Byte = hash.PasswordToKey(Encoding.UTF8.GetBytes(oldPass), param.EngineId)
    Dim newKey() As Byte = hash.PasswordToKey(Encoding.UTF8.GetBytes(newPass), param.EngineId)
    Dim random() As Byte = Encoding.UTF8.GetBytes(GenerateRandomString(L))
    Dim temp() As Byte = oldKey
    Dim delta(L - 1) As Byte
    Dim iterations As Integer = ((newKey.Length - 1) / L) - 1
    Dim k As Integer = 0
    If newKey.Length > L Then
        For k = 0 To iterations

            'Append random to temp
            Dim merged1(temp.Length + random.Length - 1) As Byte
            temp.CopyTo(merged1, 0)
            random.CopyTo(merged1, random.Length)

            'Store hash of temp in itself
            temp = hash.ComputeHash(merged1, 0, merged1.Length)

            'Generate the first 16 values of delta
            For i = 0 To L - 1
                delta(k * L + i) = temp(i) Xor newKey(k * L + i)
            Next
        Next
    End If

    'Append random to temp
    Dim merged(temp.Length + random.Length - 1) As Byte
    temp.CopyTo(merged, 0)
    random.CopyTo(merged, temp.Length)

    'Store hash of temp in itself
    temp = hash.ComputeHash(merged, 0, merged.Length)

    'Generate the first 16 values of delta
    For i = 0 To (newKey.Length - iterations * L) - 1
        delta(iterations * L + i) = temp(i) Xor newKey(iterations * L + i)
    Next

    Dim keyChange(delta.Length + random.Length - 1) As Byte
    random.CopyTo(keyChange, 0)
    delta.CopyTo(keyChange, random.Length)
    Return keyChange
End Function

Private Function GenerateKeyChangeShaSpecial(ByVal newPass As String, ByVal oldPass As String, ByRef target As UdpTarget, ByRef param As SecureAgentParameters) As Byte()

    Dim authProto As AuthenticationDigests = param.Authentication
    Dim hash As IAuthenticationDigest = Authentication.GetInstance(authProto)
    Dim L As Integer = 16

    Dim oldKey() As Byte = hash.PasswordToKey(Encoding.UTF8.GetBytes(oldPass), param.EngineId)
    Dim newKey() As Byte = hash.PasswordToKey(Encoding.UTF8.GetBytes(newPass), param.EngineId)

    Array.Resize(oldKey, L)
    Array.Resize(newKey, L)

    Dim random() As Byte = Encoding.UTF8.GetBytes(GenerateRandomString(L))
    Dim temp() As Byte = oldKey
    Dim delta(L - 1) As Byte
    Dim iterations As Integer = ((newKey.Length - 1) / L) - 1
    Dim k As Integer = 0
    If newKey.Length > L Then
        For k = 0 To iterations
            'Append random to temp
            Dim merged1(temp.Length + random.Length - 1) As Byte
            temp.CopyTo(merged1, 0)
            random.CopyTo(merged1, random.Length)

            'Store hash of temp in itself
            temp = hash.ComputeHash(merged1, 0, merged1.Length)
            Array.Resize(temp, L)

            'Generate the first 16 values of delta
            For i = 0 To L - 1
                delta(k * L + i) = temp(i) Xor newKey(k * L + i)
            Next
        Next
    End If

    'Append random to temp
    Dim merged(temp.Length + random.Length - 1) As Byte
    temp.CopyTo(merged, 0)
    random.CopyTo(merged, temp.Length)

    'Store hash of temp in itself
    temp = hash.ComputeHash(merged, 0, merged.Length)
    Array.Resize(temp, L)

    'Generate the first 16 values of delta
    For i = 0 To (newKey.Length - iterations * L) - 1
        delta(iterations * L + i) = temp(i) Xor newKey(iterations * L + i)
    Next

    Dim keyChange(delta.Length + random.Length - 1) As Byte
    random.CopyTo(keyChange, 0)
    delta.CopyTo(keyChange, random.Length)
    Return keyChange
End Function

Private Function GenerateRandomString(ByVal length As Integer) As String
    Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    Dim r As New Random
    Dim sb As New StringBuilder
    For i As Integer = 1 To length
        Dim idx As Integer = r.Next(0, 51)
        sb.Append(s.Substring(idx, 1))
    Next
    Return sb.ToString()
End Function

再次,我非常清楚这段代码很丑陋,但它有效,这就是我目前所需要的。我知道这是技术债务,而不是我应该编码的方式,但它就在这里,我希望你能从中得到一些用处。

如果这不起作用,别忘了去frc2574看看算法。


0
投票

我知道这是 7 年前的事了,但是你有没有想过为什么你的代码的 SHA 版本要求 L 为 16 而不是 20?我正在尝试用 python 实现它,并且一直在努力让它按预期工作。

谢谢

© www.soinside.com 2019 - 2024. All rights reserved.