NTLMv2 challenge-response for MoonBit, without FFI
moon add Derk2006/moonsso// moon.pkg
import {
"Derk2006/moonsso" @moonsso,
}
// or, to pull in only one layer:
// "Derk2006/moonsso/src/ntlm" @ntlm
// "Derk2006/moonsso/src/spnego" @spnego
// "Derk2006/moonsso/src/http" @httpfn conversation() -> Bool raise {
let challenge : Bytes = b"\x01\x23\x45\x67\x89\xab\xcd\xef"
let client = @moonsso.Client::new("User", "Domain", "Password")
let server = @moonsso.Server::new("User", "Domain", "Password", challenge)
let first = client.start() // Authorization: Negotiate <type 1>
let www = server.challenge_for(first) // WWW-Authenticate: Negotiate <type 2>
let answer = client.answer(www) // Authorization: Negotiate <type 3>
server.accept(answer) // true if the password is right
}GET / HTTP/1.1
401 Unauthorized
WWW-Authenticate: Negotiate
GET / HTTP/1.1
Authorization: Negotiate YEAGBisGAQUFAqA2MDSgDjAMBgorBgEEAYI3AgIKoiIEIE5UTE1TU1AAAQAAAAeCiAAAAAAAIAAAAAAAAAAgAAAA
401 Unauthorized
WWW-Authenticate: Negotiate oXkwd6ADCgEBoQwGCisGAQQBgjcCAgqiYgRgTlRMTVNTUAACAAAADAAMADAAAAABgoggASNFZ4mrze8AAAAAAAAAACQAJAA8AAAARABvAG0AYQBpAG4AAgAMAEQAbwBtAGEAaQBuAAEADABTAEUAUgBWAEUAUgAAAAAA
GET / HTTP/1.1
Authorization: Negotiate oYGxMIGuooGrBIGoTlRMTVNTUAADAAAAAAAAAFQAAABUAFQAVAAAAAwADABAAAAACAAIAEwAAAAAAAAAVAAAAAAAAACoAAAAB4KIAEQAbwBtAGEAaQBuAFUAcwBlAHIAOmnZuiG/HYEGKhytdhkLtgEBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAwARABvAG0AYQBpAG4AAQAMAFMARQBSAFYARQBSAAAAAAAAAAAA
200 OKlet server = @moonsso.Server::new("User", "Domain", "Password", challenge).require_mic()moonsso negotiate # no MIC: the challenge does not ask for one
moonsso negotiate --mic # the challenge asks, the answer carries 16 byteslet server = @moonsso.Server::new("User", "Domain", "Password", challenge)
.require_mech_list_mic()moonsso negotiate --mechlist-mic # four requests: request-mic, answer, accept-completed, 200moonsso type2 "TlRMTVNTUAACAAAADAAMADAAAAABgoggASNFZ4mrze8AAAAAAAAAACQAJAA8AAAARABPAE0AQQBJAE4AAgAMAEQATwBNAEEASQBOAAEADABTAEUAUgBWAEUAUgAAAAAA"message : challenge (2)
flags : 0x20888201 UNICODE NTLM ALWAYS_SIGN EXTENDED_SESSION_SECURITY TARGET_INFO 128
challenge: 0123456789abcdef
target : DOMAIN
av pairs : 32 bytes
target info (hex): 02000c0044004f004d00410049004e0001000c00530045005200560045005200moonsso respond --user User --domain Domain --password Password \
--challenge 0123456789abcdef --client-challenge aaaaaaaaaaaaaaaa \
--target-info 02000c0044004f004d00410049004e0001000c005300450052005600450052 \
--spnegontlmv2 response: ef708ba61dcf311ac7d78604310b4f1f01010000000000000000000000000000aaaaaaaaaaaaaaaa0000000002000c0044004f004d00410049004e0001000c0053004500520056004500520000000000000000
type3 (base64):TlRMTVNTUAADAAAAGAAYAFQAAABTAFMAbAAAAAwADABAAAAACAAIAEwAAAAAAAAAVAAAAAAAAAC/AAAAF4KIAEQAbwBtAGEAaQBuAFUAcwBlAHIAhsNQl6yc7BAlVHZKV8zMGaqqqqqqqqqq73CLph3PMRrH14YEMQtPHwEBAAAAAAAAAAAAAAAAAACqqqqqqqqqqgAAAAACAAwARABPAE0AQQBJAE4AAQAMAFMARQBSAFYARQBSAAAAAAAAAAA=
authorization : Negotiate oYHIMIHFooHCBIG/TlRMTVNTUAADAAAAGAAYAFQAAABTAFMAbAAAAAwADABAAAAACAAIAEwAAAAAAAAAVAAAAAAAAAC/AAAAF4KIAEQAbwBtAGEAaQBuAFUAcwBlAHIAhsNQl6yc7BAlVHZKV8zMGaqqqqqqqqqq73CLph3PMRrH14YEMQtPHwEBAAAAAAAAAAAAAAAAAACqqqqqqqqqqgAAAAACAAwARABPAE0AQQBJAE4AAQAMAFMARQBSAFYARQBSAAAAAAAAAAA=moonsso verify --user User --domain Domain --password Password \
--challenge 0123456789abcdef \
--authorization "Negotiate oYGxMIGuooGrBIGoTlRMTVNTUAADAAAAAAAAAFQAAABUAFQAVAAAAAwADABAAAAACAAIAEwAAAAAAAAAVAAAAAAAAACoAAAAB4KIAEQAbwBtAGEAaQBuAFUAcwBlAHIAOmnZuiG/HYEGKhytdhkLtgEBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAwARABvAG0AYQBpAG4AAQAMAFMARQBSAFYARQBSAAAAAAAAAAAA"moonsso answer --user User --domain Domain --password Password \
--www "Negotiate oXkwd6ADCgEBoQwGCisGAQQBgjcCAgqiYgRgTlRMTVNTUAACAAAADAAMADAAAAABgoggASNFZ4mrze8AAAAAAAAAACQAJAA8AAAARABvAG0AYQBpAG4AAgAMAEQAbwBtAGEAaQBuAAEADABTAEUAUgBWAEUAUgAAAAAA"/// Build the type 3 message for a decoded type 2 token.
fn answer(type2 : Bytes, client_challenge : Bytes) -> Bytes raise {
let msg = @moonsso.parse_type2(type2)
let blob = @moonsso.make_blob(0UL, client_challenge, msg.av_pairs())
let proof = @moonsso.ntlmv2_response(
"User", "Domain", "Password", msg.server_challenge(), blob,
)
let lmv2 = @moonsso.lmv2_response(
"User", "Domain", "Password", msg.server_challenge(), client_challenge,
)
@moonsso.encode_type3("User", "Domain", "WS01", lmv2, proof, b"", msg.flags())
}| Source | Covers |
|---|---|
| RFC 1320 | MD4, 7 vectors |
| RFC 1321 | MD5, 7 vectors |
| RFC 2202 | HMAC-MD5, 4 vectors |
| RC4 reference vectors | keystream, 3 vectors |
| MS-NLMP 4.2.2 | NT hash, NTLMv2 hash, LMv2 response |
moon testfn acceptor_mech_list_mic(session_base_key : Bytes, flags : UInt, mech_list_der : Bytes) -> Bytesfn can_sign(flags : UInt) -> Boolfn challenge_header_with_token(scheme : String, token : Bytes) -> Stringfn compute_mic(session_key : Bytes, type1 : Bytes, type2 : Bytes, type3 : Bytes) -> Bytesfn describe(token : Bytes) -> Stringfn encode_type1(flags : UInt) -> Bytesfn encode_type2(flags : UInt, server_challenge : Bytes, target_name : Bytes, target_info : Bytes) -> Bytesfn encode_type3(user : String, domain : String, workstation : String, lm_response : Bytes, nt_response : Bytes, session_key : Bytes, flags : UInt) -> Bytesfn encode_type3_with_mic(user : String, domain : String, workstation : String, lm_response : Bytes, nt_response : Bytes, session_key : Bytes, flags : UInt, mic : Bytes) -> Bytesfn is_spnego(token : Bytes) -> Boolfn lmv2_response(user : String, domain : String, password : String, server_challenge : Bytes, client_challenge : Bytes) -> Bytesfn mac(signing_key : Bytes, flags : UInt, seq_num : UInt, message : Bytes) -> BytesVersion = 0x00000001
Checksum = HMAC_MD5(SigningKey, SeqNum || Message)[0..8]
SeqNum = SeqNumfn make_blob(timestamp : UInt64, client_challenge : Bytes, target_info : Bytes) -> Bytesfn mech_list_der() -> Bytesfn mech_list_mic(session_base_key : Bytes, flags : UInt, mech_list_der : Bytes) -> Bytesfn nt_hash(password : String) -> Bytesfn ntlmv2_hash(user : String, domain : String, password : String) -> Bytesfn ntlmv2_response(user : String, domain : String, password : String, server_challenge : Bytes, blob : Bytes) -> Bytesfn session_base_key(user : String, domain : String, password : String, nt_proof : Bytes) -> Bytesfn signing_key(session_base_key : Bytes, flags : UInt, initiator : Bool) -> Bytesfn token_to_header(token : Bytes) -> Stringfn verify_acceptor_mech_list_mic(session_base_key : Bytes, flags : UInt, mech_list_der : Bytes, mic : Bytes) -> Boolfn verify_mac(signing_key : Bytes, flags : UInt, seq_num : UInt, message : Bytes, signature : Bytes) -> Boolfn verify_mech_list_mic(session_base_key : Bytes, flags : UInt, mech_list_der : Bytes, mic : Bytes) -> Boolfn verify_mic(session_key : Bytes, type1 : Bytes, type2 : Bytes, type3 : Bytes) -> Boolfn verify_response(user : String, domain : String, password : String, server_challenge : Bytes, response : Bytes) -> Boolfn wrap_init(ntlm_token : Bytes) -> Bytesfn wrap_response(ntlm_token : Bytes) -> Bytesfn wrap_response_state(ntlm_token : Bytes, state : Int) -> Bytesfn wrap_targ_with_mech(ntlm_token : Bytes, state : NegState?, supported_mech : Bytes?, mech_list_mic : Bytes?) -> BytesInstall
Download zipNTLMv2 challenge-response for MoonBit, without FFI