{"record":{"id":"26a313fd713b0455","repo":"shadow1ng/fscan","slug":"ms17010-invalid-padding","errorCode":null,"errorMessage":"ms17010_invalid_padding","messagePattern":"ms17010_invalid_padding","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"plugins/services/ms17010.go","lineNumber":193,"sourceCode":"\t}\n\n\tkeyBytes := []byte(key)\n\tblock, err := aes.NewCipher(keyBytes)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"%s: %w\", i18n.GetText(\"ms17010_aes_cipher_failed\"), err)\n\t}\n\n\tif len(cryptedBytes) < aes.BlockSize {\n\t\treturn \"\", fmt.Errorf(\"%s\", i18n.GetText(\"ms17010_ciphertext_too_short\"))\n\t}\n\n\tmode := cipher.NewCBCDecrypter(block, keyBytes[:aes.BlockSize])\n\tmode.CryptBlocks(cryptedBytes, cryptedBytes)\n\n\t// 移除PKCS7填充\n\tpadding := int(cryptedBytes[len(cryptedBytes)-1])\n\tif padding > len(cryptedBytes) || padding > aes.BlockSize {\n\t\treturn \"\", fmt.Errorf(\"%s\", i18n.GetText(\"ms17010_invalid_padding\"))\n\t}\n\n\tfor i := len(cryptedBytes) - padding; i < len(cryptedBytes); i++ {\n\t\tif cryptedBytes[i] != byte(padding) {\n\t\t\treturn \"\", fmt.Errorf(\"%s\", i18n.GetText(\"ms17010_padding_check_failed\"))\n\t\t}\n\t}\n\n\treturn string(cryptedBytes[:len(cryptedBytes)-padding]), nil\n}\n\n// 默认AES解密密钥 (从legacy代码复制)\nvar defaultKey = \"0123456789abcdef\"\n\n// SMB协议加密的请求数据 (从原始MS17010.go复制)\nvar (\n\tnegotiateProtocolRequestEnc  = \"G8o+kd/4y8chPCaObKK8L9+tJVFBb7ntWH/EXJ74635V3UTXA4TFOc6uabZfuLr0Xisnk7OsKJZ2Xdd3l8HNLdMOYZXAX5ZXnMC4qI+1d/MXA2TmidXeqGt8d9UEF5VesQlhP051GGBSldkJkVrP/fzn4gvLXcwgAYee3Zi2opAvuM6ScXrMkcbx200ThnOOEx98/7ArteornbRiXQjnr6dkJEUDTS43AW6Jl3OK2876Yaz5iYBx+DW5WjiLcMR+b58NJRxm4FlVpusZjBpzEs4XOEqglk6QIWfWbFZYgdNLy3WaFkkgDjmB1+6LhpYSOaTsh4EM0rwZq2Z4Lr8TE5WcPkb/JNsWNbibKlwtNtp94fIYvAWgxt5mn/oXpfUD\"\n\tsessionSetupRequestEnc       = \"52HeCQEbsSwiSXg98sdD64qyRou0jARlvfQi1ekDHS77Nk/8dYftNXlFahLEYWIxYYJ8u53db9OaDfAvOEkuox+p+Ic1VL70r9Q5HuL+NMyeyeN5T5el07X5cT66oBDJnScs1XdvM6CBRtj1kUs2h40Z5Vj9EGzGk99SFXjSqbtGfKFBp0DhL5wPQKsoiXYLKKh9NQiOhOMWHYy/C+Iwhf3Qr8d1Wbs2vgEzaWZqIJ3BM3z+dhRBszQoQftszC16TUhGQc48XPFHN74VRxXgVe6xNQwqrWEpA4hcQeF1+QqRVHxuN+PFR7qwEcU1JbnTNISaSrqEe8GtRo1r2rs7+lOFmbe4qqyUMgHhZ6Pwu1bkhrocMUUzWQBogAvXwFb8\"","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/shadow1ng/fscan/blob/95cc12e753bf43de7004e5aef42a9ffba3934303/plugins/services/ms17010.go#L175-L211","documentation":"After CBC decryption, aesDecrypt reads the last byte as the PKCS7 padding length and rejects it with \"ms17010_invalid_padding\" when padding is larger than the buffer or larger than aes.BlockSize. This guards against nonsense padding values that would make the subsequent unpad loop read out of bounds or be meaningless.","triggerScenarios":"aesDecrypt decrypts data with a wrong key, wrong IV (note: the code uses keyBytes[:aes.BlockSize] as IV, so a key/IV mismatch corrupts the final block), or data encrypted with a different padding scheme (zero padding, no padding, ISO/IEC 7816-4).","commonSituations":"Key mismatch between producer and consumer, ciphertext from a non-PKCS7 encryptor, or bit-flipped ciphertext due to transport corruption — the last plaintext byte then decodes to an impossible padding value.","solutions":["Verify the decryption key exactly matches the encryption key; wrong keys almost always produce invalid padding.","Confirm the plaintext was padded with PKCS7 before encryption; switch to padding-free block-aligned input or add PKCS7 on the encrypt side.","Ensure the IV scheme matches: this code derives the IV from the first 16 key bytes, so data encrypted with a random IV will fail here.","Check ciphertext integrity (checksum/MAC) — corruption in the last block triggers this error."],"exampleFix":"// before: assuming random IV\ndata := iv || ciphertext (encrypted with random IV)\nplain, err := aesDecrypt(b64(data), key) // fails: invalid padding\n// after: match the plugin's fixed-IV scheme or change the decryptor\ncipherOnly := ciphertext // plugin uses key[:16] as IV\nplain, err := aesDecrypt(b64(cipherOnly), key)","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"plain, err := aesDecrypt(payload, key)\nif err != nil {\n    if strings.Contains(err.Error(), \"invalid_padding\") || strings.Contains(err.Error(), \"padding_check\") {\n        return fmt.Errorf(\"decryption produced garbage: check key/IV/padding scheme\")\n    }\n    return err\n}","preventionTips":["Use identical key and IV derivation on encrypt and decrypt sides","Always PKCS7-pad plaintext before CBC encryption","Authenticate ciphertext (HMAC or AES-GCM) to catch corruption early"],"tags":["go","aes","cbc","pkcs7","padding","ms17010"],"backgroundTag":"invalid-argument-value","analyzedSha":"95cc12e753bf43de7004e5aef42a9ffba3934303","analyzedAt":"2026-09-06T17:07:30.094Z","contentChangedAt":"2026-09-06T17:07:30.094Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}