Coder Social home page Coder Social logo

extremecoders-re / bnpy Goto Github PK

View Code? Open in Web Editor NEW
29.0 8.0 3.0 110 KB

An architecture plugin for binary ninja to disassemble raw python bytecode

Home Page: https://0xec.blogspot.com/2017/11/bnpy-python-architecture-plugin-for.html

License: MIT License

Python 100.00%
python reverse-engineering binary-ninja disassembly

bnpy's Introduction

bnpy

A binary ninja architecture plugin to disassemble raw python bytecode.

Installation

Clone the repo to the Binary ninja plugins directory.

$ git clone https://github.com/extremecoders-re/bnpy.git

Usage

Lets say we have a file example.pyc. The first step is to extract raw instruction stream to a file

import marshal

# Open the pyc file
pyc_file = open('example.pyc', 'rb')

# Skip magic and timestamp
pyc_file.seek(8)

# Unmarshal
code_object = marshal.load(pyc_file)
code_string = code_object.co_code

# Dump to file
open('py_code.bin', 'wb').write(code_string)

The produced file then be loaded in Binary Ninja. Press P to select the architecure as Python.

This way you can disassemble the root code object. A pyc file however may contain multiple nested code objects embedded in co_consts. To dump all of them you can use the following recursive approach.

import marshal
import types

file_num = 0

def parse(code_object):
	global file_num
	open('py_code_' + str(file_num) + '.bin', 'wb').write(code_object.co_code)
	file_num += 1

	for const in code_object:
		if instanceof(const, types.CodeType):
			parse(code_object)

# Open the pyc file
pyc_file = open('example.pyc', 'rb')

# Skip magic and timestamp
pyc_file.seek(8)

# Unmarshal
code_object = marshal.load(pyc_file)
parse(code_object)

TODO

  • Implement a full fledged BinaryView to support disassembling of pyc files. Currently, this is difficult to do due to limitations in the Binary Ninja API (See issues #133, #551, #728)
  • IL of bytecode instructions
  • Inline co_consts, co_names within the disassembly rather than referring them by indices

Screenshot

Graph view

Linear view

License

The MIT License

bnpy's People

Contributors

extremecoders-re avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bnpy's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.