Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from BartyBoi1128/CWE-838
Browse files Browse the repository at this point in the history
CWE-838: Inappropriate Encoding for Output Context
  • Loading branch information
BartyBoi1128 authored Mar 20, 2024
2 parents d8e076a + 8a68a67 commit 2aa0b9e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
25 changes: 25 additions & 0 deletions CWE-707/CWE-838/compliant01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Compliant Code Example"""

import base64


def report_record_attack(stream: bytearray):
try:
decoded_text = stream.decode("utf-8")
except UnicodeDecodeError as e:
# Encode the stream using Base64 if there is an exception
encoded_payload = base64.b64encode(stream).decode("utf-8")
# Logging encoded payload for forensic analysis
print("Base64 Encoded Payload for Forensic Analysis:", encoded_payload)
print("Error decoding payload:", e)
else:
print("Important text:", decoded_text)


#####################
# attempting to exploit above code example
#####################
payload = bytearray("user: 毛泽东先生 attempted a directory traversal".encode("utf-8"))
# Introducing an error in the encoded text, a byte
payload[3] = 128
report_record_attack(payload)
12 changes: 12 additions & 0 deletions CWE-707/CWE-838/noncompliant01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
""" Non-compliant Code Example """

def report_record_attack(stream: bytearray):
print("important text:", stream.decode("utf-8"))

#####################
# attempting to exploit above code example
#####################
payload = bytearray("user: 毛泽东先生 attempted a directory traversal".encode("utf-8"))
# Introducing an error in the encoded text, a byte
payload[3] = 128
report_record_attack(payload)

0 comments on commit 2aa0b9e

Please sign in to comment.