Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: disassembler.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- # For use with example_functions.py: https://pastebin.com/edit/vhDzGwHQ
- """
- Python Disassembler Script
- This Python script provides a tool for dynamically disassembling functions within a specified Python script.
- It aims to assist developers in inspecting the bytecode of functions to better understand their internal workings.
- The script dynamically generates disassembler code for each function found in the target script and saves the output to a text file.
- Requirements:
- - Python 3
- Usage:
- 1. Ensure the target Python script is in the current working directory and has a '.py' extension.
- 2. Run the 'disassembler.py' script in a terminal or command prompt.
- 3. Enter the name of the target script (without the '.py' extension) when prompted.
- 4. The script will check if the target script exists. If not, it will create an 'example.py' script with predefined functions.
- 5. The script will then generate disassembler code for each function in the target script and save the output to a text file named '<script_name>_disassembled.txt'.
- 6. View the list of functions and their disassembled bytecode in the terminal, and find the detailed output in the generated text file.
- """
- import os
- import sys
- import dis
- import inspect
- def get_functions_from_script(script_path):
- """
- Extracts the list of functions from the specified Python script.
- Parameters:
- - script_path (str): The path to the Python script.
- Returns:
- - list: A list of function names found in the script.
- """
- functions = []
- with open(script_path, 'r', encoding='utf-8') as script_file:
- script_content = script_file.read()
- script_module = compile(script_content, script_path, 'exec')
- for item in script_module.co_consts:
- if inspect.iscode(item):
- functions.append(item.co_name)
- return functions
- def disassemble_functions(script_path, functions, output_file):
- """
- Generates disassembler code for each function in the specified script and saves the output to a text file.
- Parameters:
- - script_path (str): The path to the Python script.
- - functions (list): A list of function names.
- - output_file (str): The path to the output text file.
- """
- with open(script_path, 'r', encoding='utf-8') as script_file:
- script_content = script_file.read()
- script_module = compile(script_content, script_path, 'exec')
- with open(output_file, 'w', encoding='utf-8') as output:
- sys.stdout = output # Redirect stdout to the output file
- for item in script_module.co_consts:
- if inspect.iscode(item) and item.co_name in functions:
- print(f"\nDisassembly for function '{item.co_name}':")
- dis.dis(item)
- sys.stdout = sys.__stdout__ # Reset stdout to its original value
- def create_example_script(script_path):
- """
- Creates an example Python script with predefined functions if the specified script is not found.
- Parameters:
- - script_path (str): The path to the Python script.
- """
- with open(script_path, 'w', encoding='utf-8') as example_file:
- example_file.write("""
- # example.py
- def print_hello_world():
- print("Hello, World!")
- def print_hello_world_uppercase():
- print("HELLO, WORLD!")
- def print_hello_world_reverse():
- print("dlroW ,olleH"[::-1])
- if __name__ == "__main__":
- print_hello_world()
- print_hello_world_uppercase()
- print_hello_world_reverse()
- """)
- def main():
- """
- The main function that orchestrates the execution of the disassembler script.
- """
- cwd = os.getcwd()
- # Assume the script is in the cwd and has a .py extension edit "example" below to the name of your file.
- script_name = "example_functions"
- script_path = os.path.join(cwd, f"{script_name}.py")
- output_file = os.path.join(cwd, f"{script_name}_disassembled.txt")
- # Create Hello World Example Script To Disassemble If No File Is Found
- if not os.path.exists(script_path):
- print(f"Error: Script '{script_name}.py' not found in the current working directory.\nCreating Example Script...")
- create_example_script(script_path)
- functions = get_functions_from_script(script_path)
- if not functions:
- print("No functions found in the script.")
- return
- print("List of functions in the script:")
- for function in functions:
- print(f"- {function}")
- disassemble_functions(script_path, functions, output_file)
- print(f"\nDisassembled output saved to '{output_file}'.")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement