shadow1ng/fscan · error

ms17010_invalid_padding

Error message

ms17010_invalid_padding

What it means

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.

Source

Thrown at plugins/services/ms17010.go:193

	}

	keyBytes := []byte(key)
	block, err := aes.NewCipher(keyBytes)
	if err != nil {
		return "", fmt.Errorf("%s: %w", i18n.GetText("ms17010_aes_cipher_failed"), err)
	}

	if len(cryptedBytes) < aes.BlockSize {
		return "", fmt.Errorf("%s", i18n.GetText("ms17010_ciphertext_too_short"))
	}

	mode := cipher.NewCBCDecrypter(block, keyBytes[:aes.BlockSize])
	mode.CryptBlocks(cryptedBytes, cryptedBytes)

	// 移除PKCS7填充
	padding := int(cryptedBytes[len(cryptedBytes)-1])
	if padding > len(cryptedBytes) || padding > aes.BlockSize {
		return "", fmt.Errorf("%s", i18n.GetText("ms17010_invalid_padding"))
	}

	for i := len(cryptedBytes) - padding; i < len(cryptedBytes); i++ {
		if cryptedBytes[i] != byte(padding) {
			return "", fmt.Errorf("%s", i18n.GetText("ms17010_padding_check_failed"))
		}
	}

	return string(cryptedBytes[:len(cryptedBytes)-padding]), nil
}

// 默认AES解密密钥 (从legacy代码复制)
var defaultKey = "0123456789abcdef"

// SMB协议加密的请求数据 (从原始MS17010.go复制)
var (
	negotiateProtocolRequestEnc  = "G8o+kd/4y8chPCaObKK8L9+tJVFBb7ntWH/EXJ74635V3UTXA4TFOc6uabZfuLr0Xisnk7OsKJZ2Xdd3l8HNLdMOYZXAX5ZXnMC4qI+1d/MXA2TmidXeqGt8d9UEF5VesQlhP051GGBSldkJkVrP/fzn4gvLXcwgAYee3Zi2opAvuM6ScXrMkcbx200ThnOOEx98/7ArteornbRiXQjnr6dkJEUDTS43AW6Jl3OK2876Yaz5iYBx+DW5WjiLcMR+b58NJRxm4FlVpusZjBpzEs4XOEqglk6QIWfWbFZYgdNLy3WaFkkgDjmB1+6LhpYSOaTsh4EM0rwZq2Z4Lr8TE5WcPkb/JNsWNbibKlwtNtp94fIYvAWgxt5mn/oXpfUD"
	sessionSetupRequestEnc       = "52HeCQEbsSwiSXg98sdD64qyRou0jARlvfQi1ekDHS77Nk/8dYftNXlFahLEYWIxYYJ8u53db9OaDfAvOEkuox+p+Ic1VL70r9Q5HuL+NMyeyeN5T5el07X5cT66oBDJnScs1XdvM6CBRtj1kUs2h40Z5Vj9EGzGk99SFXjSqbtGfKFBp0DhL5wPQKsoiXYLKKh9NQiOhOMWHYy/C+Iwhf3Qr8d1Wbs2vgEzaWZqIJ3BM3z+dhRBszQoQftszC16TUhGQc48XPFHN74VRxXgVe6xNQwqrWEpA4hcQeF1+QqRVHxuN+PFR7qwEcU1JbnTNISaSrqEe8GtRo1r2rs7+lOFmbe4qqyUMgHhZ6Pwu1bkhrocMUUzWQBogAvXwFb8"

View on GitHub (pinned to 95cc12e753)

Solutions

  1. Verify the decryption key exactly matches the encryption key; wrong keys almost always produce invalid padding.
  2. Confirm the plaintext was padded with PKCS7 before encryption; switch to padding-free block-aligned input or add PKCS7 on the encrypt side.
  3. 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.
  4. Check ciphertext integrity (checksum/MAC) — corruption in the last block triggers this error.

Example fix

// before: assuming random IV
data := iv || ciphertext (encrypted with random IV)
plain, err := aesDecrypt(b64(data), key) // fails: invalid padding
// after: match the plugin's fixed-IV scheme or change the decryptor
cipherOnly := ciphertext // plugin uses key[:16] as IV
plain, err := aesDecrypt(b64(cipherOnly), key)
Defensive patterns

Strategy: try-catch

Try / catch

plain, err := aesDecrypt(payload, key)
if err != nil {
    if strings.Contains(err.Error(), "invalid_padding") || strings.Contains(err.Error(), "padding_check") {
        return fmt.Errorf("decryption produced garbage: check key/IV/padding scheme")
    }
    return err
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of shadow1ng/fscan@95cc12e753 (2026-09-06). Data as JSON: /api/errors/26a313fd713b0455. Report an issue: GitHub.