AES CBC PKCS5Padding helper function update (#3287)

* Added DSL helper functions for CVE + misc

* Added aes_cbc with pkcspadding

* Misc

* Misc

* Misc

* Removed debug statement

* Misc

* Misc

* Fixed tests
This commit is contained in:
Ice3man 2023-02-09 20:22:42 +05:30 committed by GitHub
parent 518944f6e8
commit 7e7bb1ed0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 19 deletions

View File

@ -773,22 +773,14 @@ func init() {
return argStr[start:end], nil return argStr[start:end], nil
}, },
), ),
"aes_cbc": makeDslFunction(2, func(args ...interface{}) (interface{}, error) { "aes_cbc": makeDslFunction(3, func(args ...interface{}) (interface{}, error) {
key := []byte(types.ToString(args[0])) bKey := []byte(args[1].(string))
cleartext := []byte(types.ToString(args[1])) bIV := []byte(args[2].(string))
block, _ := aes.NewCipher(key) bPlaintext := pkcs5padding([]byte(args[0].(string)), aes.BlockSize, len(args[0].(string)))
blockSize := block.BlockSize() block, _ := aes.NewCipher(bKey)
n := blockSize - len(cleartext)%blockSize ciphertext := make([]byte, len(bPlaintext))
temp := bytes.Repeat([]byte{byte(n)}, n) mode := cipher.NewCBCEncrypter(block, bIV)
cleartext = append(cleartext, temp...) mode.CryptBlocks(ciphertext, bPlaintext)
iv := make([]byte, 16)
if _, err := crand.Read(iv); err != nil {
return nil, err
}
blockMode := cipher.NewCBCEncrypter(block, iv)
ciphertext := make([]byte, len(cleartext))
blockMode.CryptBlocks(ciphertext, cleartext)
ciphertext = append(iv, ciphertext...)
return ciphertext, nil return ciphertext, nil
}), }),
"aes_gcm": makeDslFunction(2, func(args ...interface{}) (interface{}, error) { "aes_gcm": makeDslFunction(2, func(args ...interface{}) (interface{}, error) {
@ -1199,6 +1191,12 @@ func toChunks(input string, chunkSize int) []string {
return chunks return chunks
} }
func pkcs5padding(ciphertext []byte, blockSize int, after int) []byte {
padding := (blockSize - len(ciphertext)%blockSize)
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
type CompilationError struct { type CompilationError struct {
DslSignature string DslSignature string
WrappedError error WrappedError error

View File

@ -93,7 +93,7 @@ func TestDslFunctionSignatures(t *testing.T) {
} }
func TestGetPrintableDslFunctionSignatures(t *testing.T) { func TestGetPrintableDslFunctionSignatures(t *testing.T) {
expected := ` aes_cbc(arg1, arg2 interface{}) interface{} expected := ` aes_cbc(arg1, arg2, arg3 interface{}) interface{}
aes_gcm(arg1, arg2 interface{}) interface{} aes_gcm(arg1, arg2 interface{}) interface{}
base64(arg1 interface{}) interface{} base64(arg1 interface{}) interface{}
base64_decode(arg1 interface{}) interface{} base64_decode(arg1 interface{}) interface{}
@ -177,7 +177,7 @@ func TestGetPrintableDslFunctionSignatures(t *testing.T) {
assert.Equal(t, expected, signatures) assert.Equal(t, expected, signatures)
coloredSignatures := GetPrintableDslFunctionSignatures(false) coloredSignatures := GetPrintableDslFunctionSignatures(false)
require.Contains(t, coloredSignatures, `[93maes_cbc(arg1, arg2 interface{}) interface{}`, "could not get colored signatures") require.Contains(t, coloredSignatures, `[93maes_cbc(arg1, arg2, arg3 interface{}) interface{}`, "could not get colored signatures")
} }
func TestDslExpressions(t *testing.T) { func TestDslExpressions(t *testing.T) {

View File

@ -135,7 +135,7 @@ func generateDNSPayload(URL string) []byte {
buffer.WriteString(string(rune(len(hostname)))) buffer.WriteString(string(rune(len(hostname))))
buffer.WriteString(hostname) buffer.WriteString(hostname)
middle, _ := hex.DecodeString("74000071007E0005740005") middle, _ := hex.DecodeString("74000071007E0005740004")
buffer.Write(middle) buffer.Write(middle)
buffer.WriteString(parsed.Scheme) buffer.WriteString(parsed.Scheme)