Skip to content

Instantly share code, notes, and snippets.

@MasterEx
Created April 4, 2013 10:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MasterEx/5309265 to your computer and use it in GitHub Desktop.
Save MasterEx/5309265 to your computer and use it in GitHub Desktop.
A more advanced implementation of the original coolphpobfuscator (https://gist.github.com/MasterEx/1171816) described in http://masterex.github.com/archive/2011/08/27/php-protect-the-code.html . Credit goes to Dan.
Usage
java -jar coolphpobfuscator.jar input_directory output_directory
package coolphpobfuscator;
import java.io.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author periklis master_ex ntanasis - pntanasis@gmail.com
* @forked Dan 1NTT - March 2013 - 1ntt.net USE: java -jar coolphpobfuscator
* input_directory output_directory NOTE: if PHP files includes javascript, the
* ; is required to terminate end line
*/
public class Coolphpobfuscator {
// found here: http://snippets.dzone.com/posts/show/3686
public static String md5(String s) throws Exception {
MessageDigest m = MessageDigest.getInstance("MD5");
m.update(s.getBytes(), 0, s.length());
return new BigInteger(1, m.digest()).toString(16);
}
public static void main(String[] args) throws FileNotFoundException, NoSuchAlgorithmException, Exception {
if (args.length < 2) {
System.err.println("Arguments are missing");
System.exit(1);
}
if (args[0].equals(args[1])) {
System.err.println("Input and Output directory can't be the same");
System.exit(1);
}
String path = args[0];
String pathout = args[1];
String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
files = listOfFiles[i].getName();
if (files.endsWith(".php") || files.endsWith(".PHP")) {
File input = new File(folder, files);
Scanner in = new Scanner(input);
/**
* Here we'll start erasing and altering stuff
*/
String code = "";
String temp = "";
while (in.hasNext()) {
temp = in.nextLine();
// Hack step1 to preserve http://, files://, "//, '//
temp = temp.replace("://", "tHiShAcKiSuGlY0");
temp = temp.replace("\'//", "tHiShAcKiSuGlY1");
temp = temp.replace("\"//", "tHiShAcKiSuGlY2");
// remove single line comments
temp = temp.replaceAll("//.+", "");
code += temp + "\n";
}
// remove the deep comments
code = code.replaceAll("(?:/\\*(?:[^*]|(?:\\*+[^*/]))*\\*+/)|(?://.*)", "");
// remove collections of spaces, new lines, returns, tabs and breaks
code = code.replaceAll("\\s+", " ");
code = code.replaceAll("; ", ";");
// Hack step2 to restore http://, files://, etc
code = code.replaceAll("tHiShAcKiSuGlY0", "://");
code = code.replaceAll("tHiShAcKiSuGlY1", "'//");
code = code.replaceAll("tHiShAcKiSuGlY2", "\"//");
// matching variables, but ignoring: $_, $(, $$, $/, $this-
Pattern MY_PATTERN = Pattern.compile("\\$[^_|^\\(|^$|^/|^this-](\\w|\\d)+");
Matcher m = MY_PATTERN.matcher(code);
ArrayList<String> variables = new ArrayList<String>();
// place every variable name in an array
int index = 0;
while (m.find(index)) {
if (!variables.contains(m.group())) {
variables.add(m.group());
}
index = m.end();
}
//Sorting list, longest variables first to prevent rename collision
Collections.sort(variables, new MyComparator());
// rename the variables with their md5hash (with a leading a in-front to
// ensure that their name will start with letter)
Iterator<String> itr = variables.iterator();
while (itr.hasNext()) {
String variablename = itr.next();
code = code.replaceAll("\\" + variablename, "\\$a" + Coolphpobfuscator.md5(variablename));
}
// Write new files to output directory
File fileout = new File(pathout, files);
try {
// Create file
FileWriter fstream = new FileWriter(fileout);
BufferedWriter out = new BufferedWriter(fstream);
out.write(code.trim());
out.close();
} //Catch error exception, if any
catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
}
}
}
package coolphpobfuscator;
import java.util.Comparator;
// Samuel_xL http://stackoverflow.com/questions/3408976/sort-array-first-by-length-then-alphabetically-in-java
public class MyComparator implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
if (o1.length() > o2.length()) {
return -1;
} else if (o1.length() < o2.length()) {
return 1;
}
return o1.compareTo(o2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment