Skip to content

Commit d561610

Browse files
authored
Hmac allows hashlib hashes as digest (#105)
Signed-off-by: Eric Brown <eric.brown@securesauce.dev>
1 parent 9fb1285 commit d561610

36 files changed

Lines changed: 572 additions & 30 deletions

precli/rules/python/stdlib/hmac/hmac_weak_hash.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@
8181

8282

8383
WEAK_HASHES = ("md4", "md5", "ripemd160", "sha", "sha1")
84+
HASHLIB_WEAK_HASHES = (
85+
"hashlib.md4",
86+
"hashlib.md5",
87+
"hashlib.ripemd160",
88+
"hashlib.sha",
89+
"hashlib.sha1",
90+
)
8491

8592

8693
class HmacWeakHash(Rule):
@@ -108,33 +115,37 @@ def analyze(self, context: dict, **kwargs: dict) -> Result:
108115
"""
109116
hmac.new(key, msg=None, digestmod='')
110117
"""
111-
name = call.get_argument(position=2, name="digestmod").value
118+
argument = call.get_argument(position=2, name="digestmod")
119+
digestmod = argument.value
112120

113-
# TODO(ericwb): can hashlib.md5 be passed as digestmod?
114-
115-
if isinstance(name, str) and name.lower() in WEAK_HASHES:
121+
if (
122+
isinstance(digestmod, str) and digestmod.lower() in WEAK_HASHES
123+
) or digestmod in HASHLIB_WEAK_HASHES:
116124
return Result(
117125
rule_id=self.id,
118126
location=Location(
119127
file_name=context["file_name"],
120-
node=call.function_node,
128+
node=argument.node,
121129
),
122130
level=Level.ERROR,
123-
message=self.message.format(name),
131+
message=self.message.format(digestmod),
124132
)
125133
elif call.name_qualified in ["hmac.digest"]:
126134
"""
127135
hmac.digest(key, msg, digest)
128136
"""
129-
name = call.get_argument(position=2, name="digest").value
137+
argument = call.get_argument(position=2, name="digest")
138+
digest = argument.value
130139

131-
if isinstance(name, str) and name.lower() in WEAK_HASHES:
140+
if (
141+
isinstance(digest, str) and digest.lower() in WEAK_HASHES
142+
) or digest in HASHLIB_WEAK_HASHES:
132143
return Result(
133144
rule_id=self.id,
134145
location=Location(
135146
file_name=context["file_name"],
136-
node=call.function_node,
147+
node=argument.node,
137148
),
138149
level=Level.ERROR,
139-
message=self.message.format(name),
150+
message=self.message.format(digest),
140151
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import hashlib
2+
import hmac
3+
4+
5+
key = b"my-secret-key"
6+
message = b"Hello, world!"
7+
hmac.digest(key, message, digest=hashlib.blake2b)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import hashlib
2+
import hmac
3+
4+
5+
key = b"my-secret-key"
6+
message = b"Hello, world!"
7+
hmac.digest(key, message, digest=hashlib.blake2s)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import hashlib
2+
import hmac
3+
4+
5+
key = b"my-secret-key"
6+
message = b"Hello, world!"
7+
hmac.digest(key, message, digest=hashlib.md4)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import hashlib
2+
import hmac
3+
4+
5+
key = b"my-secret-key"
6+
message = b"Hello, world!"
7+
hmac.digest(key, message, digest=hashlib.md5)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import hashlib
2+
import hmac
3+
4+
5+
key = b"my-secret-key"
6+
message = b"Hello, world!"
7+
hmac.digest(key, message, digest=hashlib.ripemd160)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import hashlib
2+
import hmac
3+
4+
5+
key = b"my-secret-key"
6+
message = b"Hello, world!"
7+
hmac.digest(key, message, digest=hashlib.sha)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import hashlib
2+
import hmac
3+
4+
5+
key = b"my-secret-key"
6+
message = b"Hello, world!"
7+
hmac.digest(key, message, digest=hashlib.sha1)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import hashlib
2+
import hmac
3+
4+
5+
key = b"my-secret-key"
6+
message = b"Hello, world!"
7+
hmac.digest(key, message, digest=hashlib.sha224)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import hashlib
2+
import hmac
3+
4+
5+
key = b"my-secret-key"
6+
message = b"Hello, world!"
7+
hmac.digest(key, message, digest=hashlib.sha256)

0 commit comments

Comments
 (0)