├── README.md └── deobfuscate.sh /README.md: -------------------------------------------------------------------------------- 1 | PHP Code Deobfuscator 2 | ===================== 3 | 4 | ## Quick Start 5 | 6 | * Remember: deobfuscate.sh is a **Linux commandline tool** 7 | * Make sure you have "php-cli" and "ltrace" installed on your system 8 | * Fetch a copy of [deobfuscate.sh](https://raw.github.com/technopagan/php-code-deobfuscator/master/deobfuscate.sh) and place it somewhere you deem a good place for 3rd party shellscripts, e.g. "/usr/local/bin". Make sure the location is in the PATH of the user(s) who will run deobfuscate.sh and ensure that the script is executable (chmod -x). 9 | * You can now run "bash deobfuscate.sh /path/to/obfuscated/php/script.php" to retrieve the original, human-readable version of the PHP source code 10 | 11 | ## Reasoning 12 | 13 | As freelance web-developers, software engineers or web performance consultants we are often called to work on existing projects with a diverse code base. Sadly, sometimes this includes obfuscated PHP code: 14 | 15 | 16 | 17 | The reason is that some 3rd-party plugin developers fear that people might steal their code and thus ruin their business. While some of these developers can be contacted and asked for a human-readable version of their code in order for us to debug it, others are either not around anymore or refuse to cooperate because fear is not a thing to be reasoned with. 18 | 19 | In order for us to still do our jobs of analyzing & optimizing code, I created this Bash script. 20 | 21 | ## How It Works 22 | 23 | Obfuscation simply makes PHP code less human-readable. It still needs to be parseable by PHP. So the first thing that happens in the background during runtime of an obfuscated script is its deobfuscation so it once again becomes plain PHP code that can be executed. During normal operation, this is a step that is not visible to the user as it happens in memory. Using ltrace, we can retrieve this unobfuscated state of the script from memory and save it as a file, thus enabling us to read & understand the actual code. 24 | -------------------------------------------------------------------------------- /deobfuscate.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ############################################################################### 4 | # 5 | # PHP Code De-Obfuscator 6 | # 7 | # Usage: bash deobfuscate.sh /path/to/obfuscated/php/script.php 8 | # 9 | ############################################################################### 10 | # 11 | # Tools that need to be pre-installed: 12 | # 13 | # * php5-cli 14 | # 15 | # * ltrace 16 | # 17 | ############################################################################### 18 | # 19 | # This software is published under the BSD licence 3.0 20 | # 21 | # Copyright (c) 2013, Tobias Baldauf 22 | # All rights reserved. 23 | # 24 | # Mail: kontakt@tobias-baldauf.de 25 | # Web: http://who.tobias.is/ 26 | # Twitter: @tbaldauf 27 | # 28 | # Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 29 | # 30 | # * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 31 | # 32 | # * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 33 | # 34 | # * Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. 35 | # 36 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37 | # 38 | ############################################################################### 39 | 40 | 41 | 42 | ############################################################################### 43 | # Configuration 44 | ############################################################################### 45 | 46 | # Accept the obfuscated PHP script as input parameter 47 | INPUTFILE="$1" 48 | 49 | # If the script cannot find the php commandline tool, define its name here 50 | # Possible values: autodetect, php5-cgi, php-cli etc. 51 | # Default: autodetect 52 | PHP5="autodetect" 53 | 54 | # If the script cannot find ltrace, define its CLI name here 55 | # Possible values: autodetect, ltrace etc. 56 | # Default: autodetect 57 | LTRACE="autodetect" 58 | 59 | 60 | 61 | ############################################################################### 62 | # MAIN PROGRAM 63 | ############################################################################### 64 | 65 | # Wrapping the main program to allow defering function definitions 66 | main() { 67 | 68 | # In case this deobfuscator is called without a script to process, 69 | # remind people how to use it & quit 70 | if [ ! -f "$INPUTFILE" ]; then 71 | echo "Missing an input file. Call this script like this: $0 /path/to/obfuscated/php/script.php" 72 | exit 1 73 | fi 74 | 75 | # Use our findcommandlinetool function to find the a handle for PHP 76 | findcommandlinetool PHP5 $PHP5 php5 php 77 | PHP5=${COMMANDLINETOOL} 78 | 79 | # Use our findcommandlinetool function to find a proper handle for ltrace 80 | findcommandlinetool LTrace $LTRACE ltrace 81 | LTRACE=${COMMANDLINETOOL} 82 | 83 | # Launch the actual deobfuscation process 84 | deobfuscate 85 | } 86 | 87 | 88 | 89 | 90 | ############################################################################### 91 | # FUNCTIONS 92 | ############################################################################### 93 | 94 | # Find the proper callname for the required commandline tool 95 | findcommandlinetool () { 96 | # Take the parameters given at function call as input 97 | NAME="$1" 98 | shift 99 | # For each possible name input of the tool, test if its actually available 100 | for i in "$@"; do 101 | COMMANDLINETOOL=$(type -p $i) 102 | # If 'type -p' returned something, we now have our proper handle 103 | if [ "$COMMANDLINETOOL" ]; then 104 | break 105 | fi 106 | done 107 | # In case none of the given inputs works, apologize & quit 108 | if [ ! "$COMMANDLINETOOL" ]; then 109 | echo "Unable to find ${NAME}. Please ensure that it is installed, set its CLI name in the configuration section of this script and then retry." 110 | exit 1 111 | fi 112 | } 113 | 114 | # Deobfuscate an obfuscated PHP code block by retrieving its unobfuscated state from memory during runtime 115 | deobfuscate () { 116 | # Use ltrace with a sufficiently large buffer to grab the memory block of PHP during its runtime and retrieve its deobfuscated state 117 | echo -e "$(${LTRACE} -e memcpy -s 524288 ${PHP5} ${INPUTFILE} 2>&1 > /dev/null | grep ', "[?%]><[?%]' | head -1 | sed 's/^memcpy([^"]*".>//; s/"[^"]*$//')" | sed 's/\r$//' > ${INPUTFILE}.decoded.php 118 | # Give a nice human-readable success notification 119 | echo "Successfully deobfuscated the script and saved the result as ${INPUTFILE}.decoded.php." 120 | } 121 | 122 | # Finally, launch the main program 123 | main 124 | 125 | 126 | 127 | ############################################################################### 128 | # EOF 129 | ############################################################################### 130 | --------------------------------------------------------------------------------