Skip to content

Instantly share code, notes, and snippets.

@jershmagersh
Last active February 26, 2024 01:54
Show Gist options
  • Save jershmagersh/2c8f55e94248a623d87d617dd83f2ab1 to your computer and use it in GitHub Desktop.
Save jershmagersh/2c8f55e94248a623d87d617dd83f2ab1 to your computer and use it in GitHub Desktop.

Can you write Python that extracts the copied_alphabet string from the __builtin_strncpy in this function and uses it to decrypt data wtihin the dec_sc_buff variable using the binary ninja API? 👤

🤖 To extract the copied_alphabet string and decrypt data within the dec_sc_buff variable using the Binary Ninja API, you can follow these steps: Extract the copied_alphabet string directly from the provided HLIL. Implement the decryption logic based on the operation described in the HLIL for modifying dec_sc_buff. Use the Binary Ninja API for any additional operations needed. Here is a Python example that accomplishes this:

import binaryninja

from binaryninja import BinaryViewType

# Assuming 'bv' is your BinaryView for the binary
# Load the binary first
bv = BinaryViewType.get_view_of_file('/path/to/your/binary')

# The copied_alphabet string as extracted from the HLIL
copied_alphabet = "@AlzsQ1DSS>I9XX7kB7M1MT3?CH8B1ggtV_!RTX0zJSbzmUYpW5H2n@o$"
# The decryption function based on the HLIL logic
def decrypt_data(encrypted_data):
    decrypted_data = bytearray()
    for i in range(len(encrypted_data)):
        key_char = copied_alphabet[i % len(copied_alphabet)]
        decrypted_byte = encrypted_data[i] ^ ord(key_char)
        decrypted_data.append(decrypted_byte)
    return decrypted_data
    
# Example usage
# Assuming you have a way to obtain the encrypted 'dec_sc_buff' data
# This could be from extracting the data at runtime, from a dump, etc.

encrypted_data = b'\x00\x01\x02\x03'  # Placeholder for the actual encrypted data
decrypted_data = decrypt_data(encrypted_data)

print("Decrypted data:", decrypted_data)

Please replace '/path/to/your/binary' with the actual path to your binary and encrypted_data with the actual data you wish to decrypt. The decryption logic is based on the operation described in the HLIL, specifically the line that modifies dec_sc_buff using the copied_alphabet string.

Can you programmatically extract copied_alphabet using HLIL with the binary ninja API? 👤

🤖 To programmatically extract the copied_alphabet string using the Binary Ninja API and HLIL (High-Level Intermediate Language), you can follow these general steps. This example assumes you have the function's starting address and want to parse the HLIL to find the __builtin_strncpy call that initializes copied_alphabet.

import binaryninja

from binaryninja import BinaryViewType
# Load your binary
bv = BinaryViewType.get_view_of_file('/path/to/your/binary')
# Function starting address
func_start_addr = 0x691bc000
# Get the function object by its start address

func = bv.get_function_at(func_start_addr)
# Ensure the function is analyzed and HLIL is available
if func is not None:
    # Get the HLIL representation of the function
    hlil = func.hlil
    # Iterate through all instructions in the HLIL
    for instr in hlil.instructions:
        # Check if the instruction is a call
        if instr.operation == binaryninja.HighLevelILOperation.HLIL_CALL:
            # Check if the called function is __builtin_strncpy
            # This requires checking the called function's name if available
            called_func = instr.dest
            if called_func.operation == binaryninja.HighLevelILOperation.HLIL_CONST_PTR:
                target_func = bv.get_function_at(called_func.constant)
                if target_func and target_func.name == '__builtin_strncpy':
                    # Assuming the second argument is the string literal
                    string_arg = instr.params[1]
                    if string_arg.operation == binaryninja.HighLevelILOperation.HLIL_CONST_PTR:
                        # Extract the string from the binary
                        copied_alphabet = bv.get_ascii_string_at(string_arg.constant)
                        if copied_alphabet:
                            print("Copied Alphabet:", copied_alphabet.value)
                            break

Please note the following: Replace '/path/to/your/binary' with the actual path to your binary. This script assumes that the __builtin_strncpy call related to copied_alphabet initialization is directly visible in the HLIL and that the second parameter is a constant pointer to the string. Adjustments might be needed based on the actual HLIL structure and obfuscation level. The script checks if the operation is HLIL_CONST_PTR to find the address of the string literal, which is then used to extract the string using bv.get_ascii_string_at. This script is a starting point and might require adjustments based on the specific binary and the context of the __builtin_strncpy call.

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