Skip to content

Commit 0c0b2f2

Browse files
committed
added unit tests for cipher mode, obsolete kdf alg, weak symmetric alg
1 parent 2a0945c commit 0c0b2f2

9 files changed

Lines changed: 217 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TODO
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/security/cwe-327/ApprovedCipherMode.ql
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
$aes = [System.Security.Cryptography.Aes]::Create()
3+
4+
$aesManaged = New-Object "System.Security.Cryptography.AesManaged"
5+
6+
#Setting weak modes via CipherMode enum
7+
$badMode = [System.Security.Cryptography.CipherMode]::OBC
8+
$aes.Mode = $badMode
9+
$aesManaged.Mode = $badMode
10+
11+
$aes.Mode = [System.Security.Cryptography.CipherMode]::OBC
12+
$aesManaged.Mode = [System.Security.Cryptography.CipherMode]::OBC
13+
14+
# Setting weak modes directly
15+
$aes.Mode = "obc"
16+
$aesManaged.Mode = "obc"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| ObsoleteKDFAlgorithm.ps1:27:16:27:65 | Call to cryptderivekey | Use of obsolete Crypto API. Password-based key derivation should use the PBKDF2 algorithm with SHA-2 hashing |
2+
| ObsoleteKDFAlgorithm.ps1:56:16:56:65 | Call to cryptderivekey | Use of obsolete Crypto API. Password-based key derivation should use the PBKDF2 algorithm with SHA-2 hashing |
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# ObsoleteKDFAlgorithm.Tests.ps1
2+
# PowerShell version of ObsoleteKDFAlgorithm security tests
3+
# Tests for detection of obsolete key derivation algorithms
4+
5+
using namespace System.Security.Cryptography
6+
7+
<#
8+
.SYNOPSIS
9+
Test PasswordDeriveBytes CryptDeriveKey - BAD: Uses obsolete algorithm PBKDF1
10+
#>
11+
function Test-PasswordDeriveBytesCryptDeriveKey {
12+
[CmdletBinding()]
13+
param()
14+
15+
$password = "TestPassword123"
16+
$salt = New-Object byte[] 8
17+
$iv = New-Object byte[] 8
18+
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
19+
$rng.GetBytes($salt)
20+
$rng.GetBytes($iv)
21+
$rng.Dispose()
22+
23+
# BAD: Using PasswordDeriveBytes.CryptDeriveKey
24+
$pdb = New-Object System.Security.Cryptography.PasswordDeriveBytes($password, $salt)
25+
26+
try {
27+
$key = $pdb.CryptDeriveKey("TripleDES", "SHA1", 192, $iv)
28+
return $key
29+
}
30+
catch {
31+
Write-Warning "CryptDeriveKey not available: $_"
32+
return $null
33+
}
34+
finally {
35+
$pdb.Dispose()
36+
}
37+
}
38+
39+
<#
40+
.SYNOPSIS
41+
Test Rfc2898DeriveBytes usage - BAD: Uses obsolete algorithm PBKDF1
42+
#>
43+
function Test-Rfc2898DeriveBytes {
44+
[CmdletBinding()]
45+
param()
46+
47+
$password = "TestPassword123"
48+
$salt = New-Object byte[] 8
49+
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
50+
$rng.GetBytes($salt)
51+
$rng.Dispose()
52+
53+
$kdf = New-Object System.Security.Cryptography.Rfc2898DeriveBytes($password, $salt)
54+
55+
try {
56+
$key = $kdf.CryptDeriveKey("TripleDES", "SHA1", 192, $iv)
57+
return $key
58+
}
59+
catch {
60+
Write-Warning "CryptDeriveKey not available: $_"
61+
return $null
62+
}
63+
finally {
64+
$kdf.Dispose()
65+
}
66+
67+
return $key
68+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/security/cwe-327/ObsoleteKDFAlgorithm.ql
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
| WeakSymmetricAlgorithm.ps1:16:11:16:54 | Call to create | Use of weak symmetric cryptographic algorithm. Consider using AES instead. |
2+
| WeakSymmetricAlgorithm.ps1:19:11:19:74 | Call to create | Use of weak symmetric cryptographic algorithm. Consider using AES instead. |
3+
| WeakSymmetricAlgorithm.ps1:22:11:22:59 | Call to create | Use of weak symmetric cryptographic algorithm. Consider using AES instead. |
4+
| WeakSymmetricAlgorithm.ps1:25:11:25:103 | Call to create | Use of weak symmetric cryptographic algorithm. Consider using AES instead. |
5+
| WeakSymmetricAlgorithm.ps1:28:11:28:74 | Call to new-object | Use of weak symmetric cryptographic algorithm. Consider using AES instead. |
6+
| WeakSymmetricAlgorithm.ps1:31:11:31:76 | Call to createfromname | Use of weak symmetric cryptographic algorithm. Consider using AES instead. |
7+
| WeakSymmetricAlgorithm.ps1:67:12:67:75 | Call to new-object | Use of weak symmetric cryptographic algorithm. Consider using AES instead. |
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# WeakSymmetricAlgorithm.Tests.ps1
2+
# PowerShell version of WeakSymmetricAlgorithm security tests
3+
# Tests for detection of weak symmetric encryption algorithm usage
4+
5+
using namespace System.Security.Cryptography
6+
7+
<#
8+
.SYNOPSIS
9+
Test RC2 creation - BAD: RC2 is a weak symmetric algorithm
10+
#>
11+
function Test-CreateRC2 {
12+
[CmdletBinding()]
13+
param()
14+
15+
# BAD: RC2 created
16+
$r1 = [System.Security.Cryptography.RC2]::Create()
17+
18+
# BAD: RC2 created via SymmetricAlgorithm
19+
$r2 = [System.Security.Cryptography.SymmetricAlgorithm]::Create("RC2")
20+
21+
# BAD: RC2 created with explicit name
22+
$r3 = [System.Security.Cryptography.RC2]::Create("RC2")
23+
24+
# BAD: RC2 created with full type name
25+
$r4 = [System.Security.Cryptography.SymmetricAlgorithm]::Create("System.Security.Cryptography.RC2")
26+
27+
# BAD: RC2CryptoServiceProvider created
28+
$r5 = New-Object System.Security.Cryptography.RC2CryptoServiceProvider
29+
30+
# BAD: RC2 created using CryptoConfig.CreateFromName
31+
$r6 = [System.Security.Cryptography.CryptoConfig]::CreateFromName("RC2")
32+
33+
return $r5
34+
}
35+
36+
<#
37+
.SYNOPSIS
38+
Test AES creation - GOOD: AES is an approved symmetric algorithm
39+
#>
40+
function Test-CreateAES {
41+
[CmdletBinding()]
42+
param()
43+
44+
# GOOD: AES created
45+
$a1 = [System.Security.Cryptography.Aes]::Create()
46+
47+
# GOOD: AES created via SymmetricAlgorithm
48+
$a2 = [System.Security.Cryptography.SymmetricAlgorithm]::Create("AES")
49+
50+
# GOOD: AES created with full type name
51+
$a3 = [System.Security.Cryptography.SymmetricAlgorithm]::Create("System.Security.Cryptography.Aes")
52+
53+
return $a1
54+
}
55+
56+
<#
57+
.SYNOPSIS
58+
Test weak algorithm with encryption - BAD: Using DES for actual encryption
59+
#>
60+
function Test-EncryptWithDES {
61+
[CmdletBinding()]
62+
param(
63+
[string]$PlainText = "Test data to encrypt"
64+
)
65+
66+
# BAD: Using DES for encryption
67+
$des = New-Object System.Security.Cryptography.DESCryptoServiceProvider
68+
$des.GenerateKey()
69+
$des.GenerateIV()
70+
71+
$encryptor = $des.CreateEncryptor()
72+
$plainBytes = [System.Text.Encoding]::UTF8.GetBytes($PlainText)
73+
74+
$ms = New-Object System.IO.MemoryStream
75+
$cs = New-Object System.Security.Cryptography.CryptoStream($ms, $encryptor, [System.Security.Cryptography.CryptoStreamMode]::Write)
76+
$cs.Write($plainBytes, 0, $plainBytes.Length)
77+
$cs.FlushFinalBlock()
78+
79+
$encrypted = $ms.ToArray()
80+
81+
$cs.Dispose()
82+
$ms.Dispose()
83+
$encryptor.Dispose()
84+
$des.Dispose()
85+
86+
return $encrypted
87+
}
88+
89+
<#
90+
.SYNOPSIS
91+
Test approved algorithm with encryption - GOOD: Using AES for encryption
92+
#>
93+
function Test-EncryptWithAES {
94+
[CmdletBinding()]
95+
param(
96+
[string]$PlainText = "Test data to encrypt"
97+
)
98+
99+
# GOOD: Using AES for encryption
100+
$aes = [System.Security.Cryptography.Aes]::Create()
101+
$aes.GenerateKey()
102+
$aes.GenerateIV()
103+
104+
$encryptor = $aes.CreateEncryptor()
105+
$plainBytes = [System.Text.Encoding]::UTF8.GetBytes($PlainText)
106+
107+
$ms = New-Object System.IO.MemoryStream
108+
$cs = New-Object System.Security.Cryptography.CryptoStream($ms, $encryptor, [System.Security.Cryptography.CryptoStreamMode]::Write)
109+
$cs.Write($plainBytes, 0, $plainBytes.Length)
110+
$cs.FlushFinalBlock()
111+
112+
$encrypted = $ms.ToArray()
113+
114+
$cs.Dispose()
115+
$ms.Dispose()
116+
$encryptor.Dispose()
117+
$aes.Dispose()
118+
119+
return $encrypted
120+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/security/cwe-327/WeakSymmetricAlgorithm.ql

0 commit comments

Comments
 (0)