Skip to content

Commit c209db4

Browse files
committed
Instruction Printing On Malware Scanner
1 parent 007955b commit c209db4

7 files changed

Lines changed: 72 additions & 56 deletions

File tree

‎src/main/java/the/bytecode/club/bytecodeviewer/decompilers/bytecode/InstructionPrinter.java‎

Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public class InstructionPrinter {
6969

7070
protected List<AbstractInsnNode> matchedInsns;
7171
protected Map<LabelNode, Integer> labels;
72+
private boolean firstLabel = false;
73+
private ArrayList<String> info = new ArrayList<>();
7274

7375
public InstructionPrinter(MethodNode m, TypeAndName[] args) {
7476
this.args = args;
@@ -99,64 +101,17 @@ public InstructionPrinter(MethodNode m, InstructionPattern pattern,
99101
* @return The print as an ArrayList
100102
*/
101103
public ArrayList<String> createPrint() {
102-
ArrayList<String> info = new ArrayList<>();
104+
firstLabel = false;
105+
info.clear();
103106
ListIterator<?> it = mNode.instructions.iterator();
104-
boolean firstLabel = false;
105107
while (it.hasNext()) {
106108
AbstractInsnNode ain = (AbstractInsnNode) it.next();
107-
String line = "";
108-
if (ain instanceof VarInsnNode) {
109-
line = printVarInsnNode((VarInsnNode) ain);
110-
} else if (ain instanceof IntInsnNode) {
111-
line = printIntInsnNode((IntInsnNode) ain);
112-
} else if (ain instanceof FieldInsnNode) {
113-
line = printFieldInsnNode((FieldInsnNode) ain);
114-
} else if (ain instanceof MethodInsnNode) {
115-
line = printMethodInsnNode((MethodInsnNode) ain);
116-
} else if (ain instanceof LdcInsnNode) {
117-
line = printLdcInsnNode((LdcInsnNode) ain);
118-
} else if (ain instanceof InsnNode) {
119-
line = printInsnNode((InsnNode) ain);
120-
} else if (ain instanceof JumpInsnNode) {
121-
line = printJumpInsnNode((JumpInsnNode) ain);
122-
} else if (ain instanceof LineNumberNode) {
123-
line = printLineNumberNode();
124-
} else if (ain instanceof LabelNode) {
125-
if (firstLabel
126-
&& BytecodeViewer.viewer.appendBracketsToLabels
127-
.isSelected())
128-
info.add("}");
129-
130-
line = printLabelnode((LabelNode) ain);
131-
132-
if (BytecodeViewer.viewer.appendBracketsToLabels.isSelected()) {
133-
if (!firstLabel)
134-
firstLabel = true;
135-
line += " {";
136-
}
137-
} else if (ain instanceof TypeInsnNode) {
138-
line = printTypeInsnNode((TypeInsnNode) ain);
139-
} else if (ain instanceof FrameNode) {
140-
line = printFrameNode((FrameNode) ain);
141-
} else if (ain instanceof IincInsnNode) {
142-
line = printIincInsnNode((IincInsnNode) ain);
143-
} else if (ain instanceof TableSwitchInsnNode) {
144-
line = printTableSwitchInsnNode((TableSwitchInsnNode) ain);
145-
} else if (ain instanceof LookupSwitchInsnNode) {
146-
line = printLookupSwitchInsnNode((LookupSwitchInsnNode) ain);
147-
} else if (ain instanceof InvokeDynamicInsnNode) {
148-
line = printInvokeDynamicInsNode((InvokeDynamicInsnNode) ain);
149-
} else if (ain instanceof MultiANewArrayInsnNode) {
150-
line = printMultiANewArrayInsNode((MultiANewArrayInsnNode) ain);
151-
} else {
152-
line += "UNADDED OPCODE: " + nameOpcode(ain.getOpcode()) + " "
153-
+ ain;
154-
}
109+
String line = printInstruction(ain);
155110
if (!line.isEmpty()) {
156111
if (match)
157112
if (matchedInsns.contains(ain))
158113
line = " -> " + line;
159-
114+
160115
info.add(line);
161116
}
162117
}
@@ -165,6 +120,60 @@ public ArrayList<String> createPrint() {
165120
info.add("}");
166121
return info;
167122
}
123+
124+
public String printInstruction(AbstractInsnNode ain)
125+
{
126+
String line = "";
127+
if (ain instanceof VarInsnNode) {
128+
line = printVarInsnNode((VarInsnNode) ain);
129+
} else if (ain instanceof IntInsnNode) {
130+
line = printIntInsnNode((IntInsnNode) ain);
131+
} else if (ain instanceof FieldInsnNode) {
132+
line = printFieldInsnNode((FieldInsnNode) ain);
133+
} else if (ain instanceof MethodInsnNode) {
134+
line = printMethodInsnNode((MethodInsnNode) ain);
135+
} else if (ain instanceof LdcInsnNode) {
136+
line = printLdcInsnNode((LdcInsnNode) ain);
137+
} else if (ain instanceof InsnNode) {
138+
line = printInsnNode((InsnNode) ain);
139+
} else if (ain instanceof JumpInsnNode) {
140+
line = printJumpInsnNode((JumpInsnNode) ain);
141+
} else if (ain instanceof LineNumberNode) {
142+
line = printLineNumberNode();
143+
} else if (ain instanceof LabelNode) {
144+
if (firstLabel
145+
&& BytecodeViewer.viewer.appendBracketsToLabels
146+
.isSelected())
147+
info.add("}");
148+
149+
line = printLabelnode((LabelNode) ain);
150+
151+
if (BytecodeViewer.viewer.appendBracketsToLabels.isSelected()) {
152+
if (!firstLabel)
153+
firstLabel = true;
154+
line += " {";
155+
}
156+
} else if (ain instanceof TypeInsnNode) {
157+
line = printTypeInsnNode((TypeInsnNode) ain);
158+
} else if (ain instanceof FrameNode) {
159+
line = printFrameNode((FrameNode) ain);
160+
} else if (ain instanceof IincInsnNode) {
161+
line = printIincInsnNode((IincInsnNode) ain);
162+
} else if (ain instanceof TableSwitchInsnNode) {
163+
line = printTableSwitchInsnNode((TableSwitchInsnNode) ain);
164+
} else if (ain instanceof LookupSwitchInsnNode) {
165+
line = printLookupSwitchInsnNode((LookupSwitchInsnNode) ain);
166+
} else if (ain instanceof InvokeDynamicInsnNode) {
167+
line = printInvokeDynamicInsNode((InvokeDynamicInsnNode) ain);
168+
} else if (ain instanceof MultiANewArrayInsnNode) {
169+
line = printMultiANewArrayInsNode((MultiANewArrayInsnNode) ain);
170+
} else {
171+
line += "UNADDED OPCODE: " + nameOpcode(ain.getOpcode()) + " "
172+
+ ain;
173+
}
174+
175+
return line;
176+
}
168177

169178
protected String printVarInsnNode(VarInsnNode vin) {
170179
StringBuilder sb = new StringBuilder();

‎src/main/java/the/bytecode/club/bytecodeviewer/malwarescanner/MalwareCodeScanner.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.objectweb.asm.tree.*;
44
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
5+
import the.bytecode.club.bytecodeviewer.decompilers.bytecode.InstructionPrinter;
56
import the.bytecode.club.bytecodeviewer.malwarescanner.util.SearchableString;
67

78
/**
@@ -12,6 +13,7 @@
1213
*/
1314
public abstract class MalwareCodeScanner implements CodeScanner
1415
{
16+
private final InstructionPrinter instructionPrinter = new InstructionPrinter(null, null);
1517
public MalwareScanModule module;
1618

1719
public abstract void scanFieldString(MalwareScan scan, ClassNode cn, FieldNode field, SearchableString string);
@@ -79,6 +81,11 @@ public String methodToString(ClassNode cn, MethodNode method)
7981
return cn.name + "." + method.name + "(" + method.desc + ")";
8082
}
8183

84+
public String instructionToString(AbstractInsnNode instruction)
85+
{
86+
return instructionPrinter.printInstruction(instruction).trim();
87+
}
88+
8289
public String header()
8390
{
8491
String header = String.format("%30s", (module.getReadableName() + " ->\t"));

‎src/main/java/the/bytecode/club/bytecodeviewer/malwarescanner/impl/AWTRobotScanner.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void scanMethodInstruction(MalwareScan scan, ClassNode cn, MethodNode met
3838
{
3939
final MethodInsnNode min = (MethodInsnNode) instruction;
4040
if (min.owner.startsWith("java/awt/Robot"))
41-
foundMethod(scan, methodToString(cn, method) + nl);
41+
foundMethod(scan, instructionToString(instruction) + " at " + methodToString(cn, method) + nl);
4242
}
4343
}
4444
}

‎src/main/java/the/bytecode/club/bytecodeviewer/malwarescanner/impl/JavaIOScanner.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void scanMethodInstruction(MalwareScan scan, ClassNode cn, MethodNode met
2626
{
2727
final MethodInsnNode min = (MethodInsnNode) instruction;
2828
if (min.owner.startsWith("java/io"))
29-
foundMethod(scan, methodToString(cn, method) + nl);
29+
foundMethod(scan, instructionToString(instruction) + " at " + methodToString(cn, method) + nl);
3030
}
3131
}
3232
}

‎src/main/java/the/bytecode/club/bytecodeviewer/malwarescanner/impl/JavaNetScanner.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void scanMethodInstruction(MalwareScan scan, ClassNode cn, MethodNode met
2727
{
2828
final MethodInsnNode min = (MethodInsnNode) instruction;
2929
if (min.owner.startsWith("java/net"))
30-
foundMethod(scan, methodToString(cn, method) + nl);
30+
foundMethod(scan, instructionToString(instruction) + " at " + methodToString(cn, method) + nl);
3131
}
3232
}
3333
}

‎src/main/java/the/bytecode/club/bytecodeviewer/malwarescanner/impl/JavaRuntimeScanner.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void scanMethodInstruction(MalwareScan scan, ClassNode cn, MethodNode met
3838
{
3939
final MethodInsnNode min = (MethodInsnNode) instruction;
4040
if (min.owner.startsWith("java/lang/Runtime"))
41-
foundMethod(scan, methodToString(cn, method) + nl);
41+
foundMethod(scan, instructionToString(instruction) + " at " + methodToString(cn, method) + nl);
4242
}
4343
}
4444
}

‎src/main/java/the/bytecode/club/bytecodeviewer/malwarescanner/impl/ReflectionScanner.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void scanMethodInstruction(MalwareScan scan, ClassNode cn, MethodNode met
2929
{
3030
final MethodInsnNode min = (MethodInsnNode) instruction;
3131
if (min.owner.startsWith("java/lang/reflect"))
32-
foundMethod(scan, methodToString(cn, method) + nl);
32+
foundMethod(scan, instructionToString(instruction) + " at " + methodToString(cn, method) + nl);
3333
}
3434
}
3535
}

0 commit comments

Comments
 (0)