generated from ossf/project-template
-
Notifications
You must be signed in to change notification settings - Fork 184
42 code example where tested with various SAST tools detecting flaws: #653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 0 additions & 83 deletions
83
docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-502/compliant02.py
This file was deleted.
Oops, something went wrong.
88 changes: 88 additions & 0 deletions
88
docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-502/example01.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# SPDX-FileCopyrightText: OpenSSF project contributors | ||
# SPDX-License-Identifier: MIT | ||
""" Compliant Code Example """ | ||
import hashlib | ||
import hmac | ||
import platform | ||
import pickle | ||
import secrets | ||
|
||
|
||
class Message(object): | ||
"""Sample Message Object""" | ||
sender_id = 42 | ||
text = "Some text" | ||
|
||
def printout(self): | ||
"""prints content to stdout to demonstrate active content""" | ||
print(f"Message:sender_id={self.sender_id} text={self.text}") | ||
|
||
|
||
class Preserver(object): | ||
"""Demonstrating deserialisation""" | ||
def __init__(self, _key): | ||
self._key = _key | ||
|
||
def can(self, _message: Message) -> tuple: | ||
"""Serializes a Message object. | ||
Parameters: | ||
_message (Message): Message object | ||
Returns: | ||
_digest (String): HMAC digest string | ||
_jar (bytes): pickled jar as string | ||
""" | ||
_jar = pickle.dumps(_message) | ||
_digest = hmac.new(self._key, _jar, hashlib.sha256).hexdigest() | ||
return _digest, _jar | ||
|
||
def uncan(self, _expected_digest, _jar) -> Message: | ||
"""Verifies and de-serializes a Message object. | ||
Parameters: | ||
_expected_digest (String): Message HMAC digest | ||
_jar (bytes): Pickled jar | ||
Returns: | ||
(Message): Message object | ||
""" | ||
_digest = hmac.new(self._key, _jar, hashlib.sha256).hexdigest() | ||
if _expected_digest != _digest: | ||
raise ValueError("Integrity of jar compromised") | ||
return pickle.loads(_jar) | ||
|
||
|
||
# serialization of a normal package | ||
key = secrets.token_bytes() | ||
print(f"key={key}") | ||
p1 = Preserver(key) | ||
message = Message() | ||
message.printout() | ||
digest, jar = p1.can(message) | ||
|
||
# sending or storing would happen here | ||
p2 = Preserver(key) | ||
message = None | ||
message = p2.uncan(digest, jar) | ||
message.printout() | ||
|
||
##################### | ||
# exploiting above code example | ||
##################### | ||
print("-" * 10) | ||
print("Attacker trying to read the message") | ||
message = pickle.loads(jar) | ||
message.printout() | ||
|
||
print("-" * 10) | ||
if platform.system() == "Windows": | ||
PAYLOAD = b"""cos | ||
system | ||
(S'calc.exe' | ||
tR.""" | ||
else: | ||
PAYLOAD = b"""cos | ||
system | ||
(S'whoami;uptime;uname -a;ls -la /etc/shadow' | ||
tR.""" | ||
print("Attacker trying to inject PAYLOAD") | ||
p3 = Preserver(b"dont know") | ||
message = None | ||
message = p3.uncan(digest, PAYLOAD) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-665/example01.py
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with the proposal to move this code into documentation.