Skip to content

Instantly share code, notes, and snippets.

@xcvista
Created September 16, 2013 17:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xcvista/6583906 to your computer and use it in GitHub Desktop.
Save xcvista/6583906 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# IOCCC entry deobfuscator
# Copyright (c) 2013 Maxthon Chan <xcvista@me.com>
#
# This tool tries to deobfuscate code of C programming language using tools from
# LLVM project, namely clang and clang-format.
#
if [ "$1" == "--help" -o "$1" == "-h" -o "$1" == "" ]; then
echo IOCCC Entry Deobfuscator, Version 1.0
echo
echo USAGE: `basename $0` "<file-names>" ...
echo
echo Copyright "(c)" 2013 Maxthon T. Chan "<xcvista@me.com>"
echo You are free to use, copy, modify and distribute this code.
exit 1
fi
if [ ! -x "`which clang`" -o ! -x "`which clang-format`" ]; then
echo This tool depend on LLVM tools clang and clang-format.
exit 1
fi
while [ "$1" != "" ]; do
SOURCE="$1"
EXT="${SOURCE##*.}"
NAME="${SOURCE%.*}"
# Preserve the headers
HEADER="$NAME-preserve.h"
cat "$SOURCE" | grep \\#include > "$HEADER"
# Remove the include directives
DEHEAD="$NAME-nohead.$EXT"
cat "$SOURCE" | sed /\\#include/d > "$DEHEAD"
# Preprocess the file
PREPROCESS="$NAME-preprocessed.$EXT"
clang -E "$DEHEAD" -o - | sed /\\#/d > "$PREPROCESS"
# Reformat the file
REFORMAT="$NAME-reformatted.$EXT"
clang-format "$PREPROCESS" > "$REFORMAT"
# Rebuild the file
RESULT="$NAME-deobfuscated.$EXT"
cat "$HEADER" > "$RESULT"
cat "$REFORMAT" >> "$RESULT"
# Clean up
rm "$HEADER" "$DEHEAD" "$PREPROCESS" "$REFORMAT"
# Next
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment