• Stars
    star
    196
  • Rank 198,553 (Top 4 %)
  • Language
    Rust
  • License
    MIT License
  • Created over 3 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python 2.7 bytecode d̶e̶o̶b̶f̶u̶s̶c̶a̶t̶o̶r unfucker

unfuck

Have fucked Python 2.7 bytecode? Let's unfuck it.

Overview

unfuck is a utility and library for deobfuscating obfuscated Python 2.7 bytecode. It is essentially a reimplementation of the Python VM with taint tracking. Some of the things unfuck can do:

  1. Remove opaque predicates
  2. Dead code elimination
  3. Restore some lost function names
  4. Cleanup obfuscated variable names

#1 and #2 are the two biggest items that Python decompilers trip over when attempting to reconstruct original Python source code.

unfuck basically makes your bytecode go from this to this:

Obfuscated code Deobfuscated code

Or from this to this:

Obfuscated vs deobfuscated code hex dump

Yes, these are real-world examples.

Useful Wiki Resources

Usage

unfuck can either be used as a library or a command-line utility.

unfuck 0.2.0

USAGE:
    unfuck [FLAGS] [OPTIONS] <input-obfuscated-file> <output-path> [graphs-dir] [SUBCOMMAND]

FLAGS:
        --dry        Dry run only -- do not write any files
    -g               Enable outputting code graphs to dot format
    -h, --help       Prints help information
    -q               Disable all logging
    -V, --version    Prints version information
    -v               Enable verbose logging

OPTIONS:
        --decompiler <decompiler>    Your favorite Python 2.7 bytecode decompiler. This program assumes the decompiler's
                                     first positional argument is the file to decompile, and it prints the decompiled
                                     output to stdout [env: UNFUCK_DECOMPILER=]  [default: uncompyle6]

ARGS:
    <input-obfuscated-file>    Input obfuscated file
    <output-path>              Output file name or directory name. If this path is a directory, a file will be
                               created with the same name as the input. When the `strings-only` subcommand is
                               applied, this will be where the output strings file is placed
    <graphs-dir>               An optional directory for graphs to be written to [default: .]

SUBCOMMANDS:
    help            Prints this message or the help of the given subcommand(s)
    strings-only

To unfuck a single file:

# deobfuscated.pyc can also be a directory
unfuck obfuscated.pyc deobfuscated.pyc

You can also provide additional flags to dump strings to a file, or dump dot graphs that can be viewed in graphviz:

# -g is for printing graphs
unfuck -g obfuscated.pyc deobfuscated.pyc
# use the strings-only subcommand for dumping just dumping strings -- no deobfuscation is performed
unfuck deobfuscated.pyc ./strings.csv strings-only

Building

unfuck requires Python 2.7 in your system's PATH. After ensuring it's present, you should be able to just cargo build. If for some reason the correct interpreter cannot be found, try setting the PYTHON_SYS_EXECUTABLE env var to your Python 2.7 interpreter path.

Installing

cargo install --force unfuck

Library Usage

NOTE: unfuck was not originally designed with library usage in mind, and therefore brings its own multithreading platform (in this case, Rayon).

Usage is fairly straightforward:

use std::convert::TryInto;
use std::fs::File;

let mut pyc_contents = vec![];
let pyc_file = File::open("obfuscated.pyc")?;
pyc_file.read_to_end(&mut pyc_contents)?;

// magic/moddate are specific to the PYC header and are required to be
// a valid PYC file
let magic = u32::from_le_bytes(pyc_contents[0..4].try_into().unwrap());
let moddate = u32::from_le_bytes(pyc_contents[4..8].try_into().unwrap());

let pyc_contents = &pyc_contents[8..];

// Use a standard Python 2.7 opcode table
let deobfuscator = unfuck::Deobfuscator::<pydis::opcode::py27::Standard>::new(pyc_contents);
let deobfuscator = if enable_graphs {
    deobfuscator.enable_graphs()
} else {
    deobfuscator
};

let deobfuscated_code = deobfuscator.deobfuscate()?;

let mut deobfuscated_file = File::create("deobfuscated.pyc")?;
deobfuscated_file.write_all(&magic.to_le_bytes()[..])?;
deobfuscated_file.write_all(&moddate.to_le_bytes()[..])?;
deobfuscated_file.write_all(deobfuscated_code.data.as_slice())?;

greetz

gabe_k, yrp, lpcvoid, folks from the WD disc, squif, ian, pie doom, saruhan

More Repositories

1

pdbview

dump all available information from PDBs
Rust
122
star
2

fazi

drop-in replacement for libfuzzer
Rust
64
star
3

Up

FATX file system (modified version of FAT, intended for the Xbox 360) explorer for Windows, Linux, and OS X
C++
43
star
4

porkchop

Decryption utility for Yaesu ham radio firmware images
Rust
29
star
5

deoplete-swift

Swift completions in Vim using deoplete
Python
24
star
6

lain

A structured fuzzing framework built in Rust
Rust
21
star
7

qrest

A super quick to setup RESTful JSON server
Go
21
star
8

rust-proc-macro-without-dependencies

Creating a proc macro in rust without any dependencies
Rust
19
star
9

wowsdeob

Rust
18
star
10

deoplete-d

D completion for deoplete
Python
17
star
11

pydis

Disassembler for Python bytecode
Rust
16
star
12

detweet

delete tweets en masse
Python
15
star
13

sourcesecrets

Search for secrets left in git history
Rust
12
star
14

go-fuzz-issue-generator

Generates a GitHub issue description for go-fuzz findings
Go
11
star
15

hsc

Halo 3 scripting lexer, parser, compiler (some day)
D
9
star
16

svod_unpacker

Unpacks SVOD packages used on the Xbox 360 (Games on Demand, Applications, games installed to hard disks)
C++
6
star
17

png-crc-fix

Corrects the CRC-32s in a PNG
Go
6
star
18

pingg

Rust
6
star
19

party-buffalo

Automatically exported from code.google.com/p/party-buffalo
C#
6
star
20

wows-toolkit

Rust
6
star
21

makebuzz

Generating BuzzFeed-like headlines using Markov chains
Go
4
star
22

bgrep

binary grep
Rust
4
star
23

LaunchCode

A collection of patches which are (supposed to be) executed upon an Xbox 360 development's startup.
C++
3
star
24

binja-snippets

Snippets I've written for Binary Ninja
Python
3
star
25

wowsunpack

Rust
3
star
26

acceleration

the first derivative of Velocity
Rust
2
star
27

O7

World of Warships sound mod for O7 and Friends
PowerShell
2
star
28

pbo

Package for getting the details of and extracting Arma 3 PBO files
Go
2
star
29

blackhawkdown

Rust
1
star
30

wowsreplay

Rust
1
star
31

xval

Xbox 360 xval decryption package
Go
1
star
32

pboextractor

Extractor for Arma 3 PBO files
Go
1
star
33

nfsync

Automatically synchronize local files to a remote host
Go
1
star
34

crypto-exercises

Some crypto exercises from the book "Understanding Cryptography" by Springer
D
1
star
35

binja-py27

Rust
1
star
36

cleanup

Rust
1
star
37

osutil

Utilities for Go that should be in the standard library but aren't
Go
1
star
38

paatoimg

Converts a PAA file (Arma 3 MipMap basically) to an image with stitching, layering, etc.
Go
1
star
39

tale-zola

HTML
1
star
40

landaire.net

CSS
1
star
41

d4graph

Rust
1
star
42

twitch_clip_mirror_bot

Discord bot for mirroring twitch clips
Rust
1
star