Skip to content
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

base64 strings #43

Closed
RandomRobbieBF opened this issue May 22, 2020 · 2 comments
Closed

base64 strings #43

RandomRobbieBF opened this issue May 22, 2020 · 2 comments
Assignees

Comments

@RandomRobbieBF
Copy link

Hey,

I was looking to use this for decoding some base64 strings inside json and it did not see to find the following when using refang.

  },
      "data": {
        ".dockerconfigjson": "ewoJImF1dGhzIjogewoJCSJjZGUtZG9ja2VyLXJlZ2lzdHJ5LmVpYy5mdWxsc3RyZWFtLmFpIjogewoJCQkiYXV0aCI6ICJZMlJsTFhKbFoybHpkSEo1T21Oa1pTMXlaV2RwYzNSeWVRPT0iCgkJfQoJfQp9"
      },

Any way to improve this at all?

@cmmorrow cmmorrow self-assigned this May 22, 2020
@cmmorrow
Copy link
Contributor

Hi @RandomRobbieBF, I'll look into this and see what I can do.

battleoverflow added a commit that referenced this issue Mar 15, 2023
Now allows for base64 encoded strings parsing when working with embedded JSON structures
@battleoverflow battleoverflow self-assigned this Mar 15, 2023
@battleoverflow
Copy link
Contributor

battleoverflow commented Mar 15, 2023

I've included a very simple version of this in the next release which does a shallow search within the JSON structure, but for your specific case, you can do something like this:

import json, base64

content = \
"""
[
    {
        "data": {
            ".dockerconfigjson": "ewoJImF1dGhzIjogewoJCSJjZGUtZG9ja2VyLXJlZ2lzdHJ5LmVpYy5mdWxsc3RyZWFtLmFpIjogewoJCQkiYXV0aCI6ICJZMlJsTFhKbFoybHpkSEo1T21Oa1pTMXlaV2RwYzNSeWVRPT0iCgkJfQoJfQp9"
        }
    }
]
"""

def validate_base64(data):
    try:
        if isinstance(data, str):
            base64_bytes = bytes(data, 'ascii')
        elif isinstance(data, bytes):
            base64_bytes = data
        else:
            raise ValueError("Data type should be a string or bytes")
        
        return base64.b64encode(base64.b64decode(base64_bytes)) == base64_bytes
    except Exception:
        return False

base64_db = []

def extract_embedded_base64(data):
    for d in json.loads(data):
        for _, value in d.items():
            if validate_base64(value):
                base64_db.append(base64.b64decode(value).decode("ascii"))
            else:
                for _, value in value.items():
                    if validate_base64(value):
                        base64_db.append(base64.b64decode(value).decode("ascii"))

                    for _, value in json.loads(base64.b64decode(value).decode("ascii")).items():
                        if validate_base64(value):
                            base64_db.append(base64.b64decode(value).decode("ascii"))
                        
                        for _, value in value.items():
                            if validate_base64(value):
                                base64_db.append(base64.b64decode(value).decode("ascii"))
                            
                            for _, value in value.items():
                                if validate_base64(value):
                                    base64_db.append(base64.b64decode(value).decode("ascii"))
    
    # Final decoded value
    return base64_db[1] # Remove the "[1]" to view all values collected

print(extract_embedded_base64(content))

Albeit a bit recursive and repetitive, but it should validate values that are base64 encoded and iterate a few objects within the JSON data structure. This script is specific to your use case, so it may not work perfectly for every condition.

If you'd like to try the iocextract feature in the future, you can use it like this once the new version is out, but this search is a little tamer and less specific:

import iocextract

content = \
"""
[
    {
        "data": "ewoJImF1dGhzIjogewoJCSJjZGUtZG9ja2VyLXJlZ2lzdHJ5LmVpYy5mdWxsc3RyZWFtLmFpIjogewoJCQkiYXV0aCI6ICJZMlJsTFhKbFoybHpkSEo1T21Oa1pTMXlaV2RwYzNSeWVRPT0iCgkJfQoJfQp9"
    }
]
"""

print(list(iocextract.extract_encoded_urls(content, parse_json=True)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants