Skip to content

Commit aba9de9

Browse files
committed
Java Bytecode Manipulation Added
1 parent 1d0943f commit aba9de9

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed
+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*******************************************************************************
2+
Copyright (c) 2017 Venish Joe Clarence (http://venishjoe.net)
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
22+
******************************************************************************/
23+
24+
import javassist.ClassPool;
25+
import javassist.CtClass;
26+
import javassist.CtMethod;
27+
import javassist.CtNewMethod;
28+
import javassist.bytecode.CodeAttribute;
29+
import javassist.bytecode.CodeIterator;
30+
import javassist.bytecode.InstructionPrinter;
31+
import javassist.bytecode.Mnemonic;
32+
33+
import java.io.FileInputStream;
34+
import java.io.PrintStream;
35+
36+
public class ByteCodeEditor {
37+
public static void main (String args[]) throws Exception {
38+
ByteCodeEditor _byteCodeEditor = new ByteCodeEditor();
39+
40+
//Load test class
41+
ClassPool _classPool = ClassPool.getDefault();
42+
CtClass _ctClass = _classPool.makeClass(new FileInputStream("ByteCodeEditorTest.class"));
43+
44+
//Call Manipulate method to change OPCODE
45+
_byteCodeEditor.manipulateComparision(_ctClass);
46+
47+
//Other basic static analyis and manipulation functions
48+
_byteCodeEditor.addNewMethod(_ctClass);
49+
_byteCodeEditor.insertCodeInMethod(_ctClass);
50+
_byteCodeEditor.printMethodCode(_ctClass);
51+
_byteCodeEditor.printByteCode(_ctClass);
52+
}
53+
54+
public static void manipulateComparision (CtClass _ctClass) throws Exception {
55+
for(CtMethod _ctMethods:_ctClass.getDeclaredMethods()){
56+
System.out.println("Starting Byte Code Editor. Inside Method: " + _ctMethods.getName());
57+
CodeAttribute _codeAttribute = _ctMethods.getMethodInfo().getCodeAttribute();
58+
CodeIterator _codeIterator = _codeAttribute.iterator();
59+
while (_codeIterator.hasNext()) {
60+
int _indexOfCode = _codeIterator.next();
61+
int _valueOfIndex8Bit = _codeIterator.byteAt(_indexOfCode);
62+
if(_valueOfIndex8Bit==153 && _indexOfCode==6) {
63+
System.out.println("Trying to update " + _valueOfIndex8Bit + " on index " + _indexOfCode);
64+
_codeIterator.writeByte(154, _indexOfCode);
65+
}
66+
}
67+
}
68+
_ctClass.writeFile();
69+
System.out.println("Update Successful!\n");
70+
}
71+
72+
public static void addNewMethod (CtClass _ctClass) throws Exception {
73+
_ctClass.defrost();
74+
System.out.println("Starting to add new method");
75+
CtMethod _ctMethod = CtNewMethod.make("public int newMethodFromJA() { return 1; }", _ctClass);
76+
_ctClass.writeFile();
77+
System.out.println("Addition Successful!\n");
78+
}
79+
80+
public static void printMethodCode (CtClass _ctClass) throws Exception {
81+
PrintStream ps = new PrintStream(System.out);
82+
InstructionPrinter instructionPrinter = new InstructionPrinter(ps);
83+
for(CtMethod method:_ctClass.getDeclaredMethods()){
84+
System.out.println("Method: " + method.getName());
85+
instructionPrinter.print(method);
86+
}
87+
}
88+
89+
public static void printByteCode (CtClass _ctClass) throws Exception {
90+
for(CtMethod _ctMethods:_ctClass.getDeclaredMethods()){
91+
_ctClass.defrost();
92+
System.out.println("Method: " +_ctMethods.getName());
93+
CodeAttribute _codeAttribute = _ctMethods.getMethodInfo().getCodeAttribute();
94+
CodeIterator _codeIterator = _codeAttribute.iterator();
95+
while (_codeIterator.hasNext()) {
96+
int _indexOfInstruction = _codeIterator.next();
97+
int _indexValue8Bit = _codeIterator.byteAt(_indexOfInstruction);
98+
System.out.println(Mnemonic.OPCODE[_indexValue8Bit]);
99+
}
100+
}
101+
}
102+
103+
public static void insertCodeInMethod (CtClass _ctClass) throws Exception {
104+
System.out.println("Starting to insert new code");
105+
for(CtMethod method:_ctClass.getDeclaredMethods()){
106+
_ctClass.defrost();
107+
method.insertBefore("System.out.println(\"Before every method call....\");");
108+
_ctClass.writeFile();
109+
}
110+
System.out.println("Addition Successful!\n");
111+
}
112+
113+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*******************************************************************************
2+
Copyright (c) 2017 Venish Joe Clarence (http://venishjoe.net)
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
22+
******************************************************************************/
23+
24+
public class ByteCodeEditorTest {
25+
public static void main (String args[]) {
26+
System.out.println(new ByteCodeEditorTest().checkStatus(args[0]));
27+
}
28+
29+
//Simple method to check if input matches MAGIC
30+
public String checkStatus(String _inputString){
31+
if (_inputString.equals("MAGIC"))
32+
return "Right!";
33+
34+
return "Wrong";
35+
}
36+
}

0 commit comments

Comments
 (0)