Skip to content

Code Snippets

ashe848 edited this page Apr 14, 2018 · 15 revisions

Removing comments

//Should be
for(Node node : CompilationUnit?Something.getAllNodes?) {
	node.setComment(null);
}

//May have to recurse through child nodes if can only get root
removeComments(Node n) {
	n.setComment(null);
	for(Node child : n.getChildNodes()){
		removeComments(child);
	}
}

//If node.setComment() doesn't work might be ... ?
node.setBlockComment(null); 
node.setLineComment(null);
//Or to get the comments themselves
if(node.getClass() == BlockComment.class or LineComment.class or JavadocComment.class) {
	remove the node
}

\n and \t removals

/**
@author Abby Shen
**/
public class LayoutObfuscator {
	
	//Currently works if all // style comments are removed
	public String fileToOneLine(File javaSource) throws IOException {
		BufferedReader br = new BufferedReader(new FileReader(javaSource));
		StringBuilder oneLineFile = new StringBuilder();
		
		while(line=br.readLine() != null){
			line = line.trim(); //remove tabs
			oneLineFile.append(line); //appends without line separator
		}
		br.close(); //should probably go into a try finally block
		
		return oneLineFile.toString();
	}
}

Do a check for if(charAt(0)==‘@‘){sb.append(system.something line separator);}

XML Modifier

/**
Need to first read whole XML file into a string 
Afterwards, write modifiedXML back to the file

Changes to class names by ProGuard or Java Btyecode obfuscator won't be in the map: the tools will need to be able to modify XML?

@author Abby Shen
**/
public class XMLModifier{  
	public String readXML(File fileXML) {
		String stringXML;
		//TODO buffered reader with line separators?
		return stringXML;
	}

	public String updateClassNames(String stringXML, Map<String, String> classNameChanges){  
		for(String className : classNameChanges.keySet()) {
			String modifiedXML = stringXML.replace(className, classNameChanges.get(className));
		}
		
		return modifiedXML;
	}
	
	public void writeXML(File fileXML) throws IOException {
		String modifiedXML;
		BufferedWriter writer = new BufferedWriter(new FileWriter(fileXML));
		writer.write(modifiedXML); 
		writer.close();
	}
}

Opaque predicates

string[] truePredicatePool of statements that always evaluate to true
string[] falsePredicatePool of statements that always evaluate to false
//make these predicates look similar too, ie just nots of each other

for each statement s
in BlockStmt of MethodDeclaration 
that !=MethodCallExpr.class and !=ReturnStmt.class {
	sScrewed = s but screwed up in some way so it looks like s but is wrong

	random boolean to determine inserting true or false predicate
	new IfStmt

	if inserting true predicate
		cond = truePredicatePool.get(a random true condition)
		setCondition to cond
		setThenStmt to s
		setElseStmt to sScrewed
		
	else inserting false predicate
		cond = falsePredicatePool.get(a random false condition)
		setCondition to cond
		setThenStmt to sScrewed
		setElseStmt to s
		
	setStatement of BlockStmt to this new IfStmt 
	//uh oh I'm trying to modify and use s and BlockStmt at the same time... save the statements first then
}

Clone this wiki locally