-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathVTgrepGHIDRA.JAVA
709 lines (561 loc) · 20.7 KB
/
VTgrepGHIDRA.JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
import java.awt.BorderLayout;
import java.awt.Desktop;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Observable;
import javax.swing.*;
import org.apache.commons.io.IOUtils;
import docking.widgets.EmptyBorderButton;
import ghidra.app.plugin.core.instructionsearch.InstructionSearchPlugin;
import ghidra.app.plugin.core.instructionsearch.model.InstructionTableDataObject;
import ghidra.app.plugin.core.instructionsearch.model.InstructionTableModel;
import ghidra.app.plugin.core.instructionsearch.model.InstructionSearchData.UpdateType;
import ghidra.app.plugin.core.instructionsearch.ui.InstructionSearchDialog;
import ghidra.app.plugin.core.instructionsearch.ui.InstructionTable;
import ghidra.app.plugin.core.instructionsearch.ui.InstructionTablePanel;
import ghidra.app.plugin.core.instructionsearch.ui.AbstractInstructionTable.OperandState;
import ghidra.app.plugin.core.instructionsearch.util.InstructionSearchUtils;
import ghidra.app.script.GhidraScript;
import ghidra.program.model.lang.OperandType;
import ghidra.util.Msg;
import resources.ResourceManager;
// Author: Kasif Dekel (@kasifdekel)
// Based on GHIDRA's yara plugin
// Current known bugs: near branches gets masked out because OperandType.RELATIVE isn't correctly set by GHIDRA, as a workaround you can use the GUI panel to mask/unmask specific bytes.
public class VTgrepGHIDRA extends GhidraScript {
public static final int MIN_QUERY_LEN = 10;
public static final int MAX_QUERY_LEN = 4096;
private InstructionSearchPlugin plugin;
private InstructionSearchDialog dialog;
private String currentSTR;
@Override
protected void run() throws Exception {
plugin = InstructionSearchUtils.getInstructionSearchPlugin(state.getTool());
if (plugin == null) {
popup("Instruction Pattern Search plugin not installed! Please install and re-run script.");
return;
}
if (currentProgram == null) {
popup("Please open a program before running this script.");
return;
}
if (currentSelection == null) {
popup("Please make a valid selection in the program and select 'reload'. Or select the 'manual entry' option from the toolbar.");
}
dialog = new YaraDialog();
state.getTool().showDialog(dialog);
dialog.loadInstructions(plugin);
}
private String generateYaraString(String ruleName) {
StringBuilder yaraString = new StringBuilder("\n\nrule " + ruleName + "\n");
yaraString.append("{\n\tstrings:\n");
String fullStr = "";
String currStr = "";
String lc = "";
boolean isWildcards;
if (dialog == null || dialog.getSearchData() == null) {
return null;
}
String instrStr = dialog.getSearchData().getCombinedString();
for (int i = 0; i < instrStr.length(); i += 8) {
isWildcards = false;
String curByte = instrStr.length() >= 8 ? instrStr.substring(i, i + 8) : instrStr.substring(i);
String nibble1 = curByte.length() >= 4 ? curByte.substring(0, 4) : curByte.substring(0);
String nibble2 = curByte.length() >= 8 ? curByte.substring(4, 8)
: curByte.length() >= 4 ? curByte.substring(4) : "";
if (nibble1.contains(".") || nibble2.contains(".")) {
currStr = "??";
isWildcards = true;
}
else {
currStr = InstructionSearchUtils.toHex(nibble1, false).trim();
currStr += InstructionSearchUtils.toHex(nibble2, false).trim();
}
if(fullStr.isEmpty()) {
fullStr += currStr;
continue;
}
lc = fullStr.substring(fullStr.length() - 1);
fullStr += (lc.equals("?") ? (isWildcards ? currStr : " " + currStr) : (isWildcards ? " " + currStr : currStr));
}
currentSTR = fullStr;
println(currentSTR);
yaraString.append("\t\t$STR" + 1 + " = { " + fullStr + " }\n");
yaraString.append("\n\tcondition:\n");
yaraString.append("\t\t$STR1");
yaraString.append(" or $STR" + (1));
yaraString.append("\n}\n");
return yaraString.toString();
}
private class YaraDialog extends InstructionSearchDialog {
private JTextArea yaraTA;
JScrollPane scrollPane;
private JSplitPane verticalSplitter;
private int splitterSave = 200;
private YaraDialog() {
super(plugin, "Yara Rule Generator + VTgrepGHIDRA", null);
revalidate();
setPreferredSize(500, 400);
}
private class SearchVTAction extends AbstractAction {
InstructionTable instructionTable;
public SearchVTAction(String text, Icon icon, String desc, InstructionTable instructionTable) {
super(text, icon);
putValue(SHORT_DESCRIPTION, desc);
this.instructionTable = instructionTable;
}
@Override
public void actionPerformed(ActionEvent e) {
String toURL = sanitize(reduce_query(currentSTR));
println("DEBUG: toURL: " + toURL); //TODO: remove
if(toURL.length() < MIN_QUERY_LEN || toURL.length() > MAX_QUERY_LEN) {
popup("Error! minimum bytes query length should be at least 5 and below 2048!");
return;
}
dialog = new InstructionSearchDialog(plugin,"VT Search",null);
try {
toURL = URLEncoder.encode(":{ " + toURL + " }", StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e1) {
popup("Error encounted while submitting data to VT.");
e1.printStackTrace();
}
String url = "https://www.virustotal.com/gui/search/content"+ toURL + "/files" ;
OpenBrowser(url);
InstructionTableModel model = (InstructionTableModel) this.instructionTable.getModel();
model.fireTableDataChanged();
}
}
private class SimilarAction extends AbstractAction {
InstructionTable instructionTable;
public SimilarAction(String text, Icon icon, String desc, InstructionTable instructionTable) {
super(text, icon);
putValue(SHORT_DESCRIPTION, desc);
this.instructionTable = instructionTable;
}
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < this.instructionTable.getRowCount(); i++) {
for (int j = 0; j < this.instructionTable.getColumnCount(); j++) {
InstructionTableDataObject obj = this.instructionTable.getCellData(i, j);
if(obj == null || obj.getOperandCase() == null) {
continue;
}
if (OperandType.isDataReference(obj.getOperandCase().getOpType()) || OperandType.isScalar(obj.getOperandCase().getOpType()) || (OperandType.isCodeReference(obj.getOperandCase().getOpType()) && !OperandType.isRelative(obj.getOperandCase().getOpType()))) {
obj.setState(OperandState.MASKED, false);
}
}
}
InstructionTableModel model = (InstructionTableModel) this.instructionTable.getModel();
model.fireTableDataChanged();
}
}
private class StrictAction extends AbstractAction {
InstructionTable instructionTable;
public StrictAction(String text, Icon icon, String desc, InstructionTable instructionTable) {
super(text, icon);
putValue(SHORT_DESCRIPTION, desc);
this.instructionTable = instructionTable;
}
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < this.instructionTable.getRowCount(); i++) {
for (int j = 0; j < this.instructionTable.getColumnCount(); j++) {
InstructionTableDataObject obj = this.instructionTable.getCellData(i, j);
if(obj == null || obj.getOperandCase() == null) {
continue;
}
if (OperandType.isAddress(obj.getOperandCase().getOpType()) || OperandType.isDataReference(obj.getOperandCase().getOpType()) || OperandType.isScalar(obj.getOperandCase().getOpType()) || OperandType.isImmediate(obj.getOperandCase().getOpType()) || (OperandType.isCodeReference(obj.getOperandCase().getOpType()) && !OperandType.isRelative(obj.getOperandCase().getOpType()))) {
obj.setState(OperandState.MASKED, false);
}
}
}
InstructionTableModel model = (InstructionTableModel) this.instructionTable.getModel();
model.fireTableDataChanged();
}
}
private class CertAction extends AbstractAction {
InstructionTable instructionTable;
public CertAction(String text, Icon icon, String desc, InstructionTable instructionTable) {
super(text, icon);
putValue(SHORT_DESCRIPTION, desc);
this.instructionTable = instructionTable;
}
@Override
public void actionPerformed(ActionEvent e) {
check_cert();
InstructionTableModel model = (InstructionTableModel) this.instructionTable.getModel();
model.fireTableDataChanged();
}
}
public void check_cert() {
String path = currentProgram.getExecutablePath();
String stdout = "";
String stderr = "";
if(System.getProperty("os.name").toLowerCase().contains("win")) {
File f = new File(path);
if(!f.exists()) { //for whatever reason :)
popup("Something went wrong while processing this file");
return;
}
String command = "powershell.exe (Get-AuthenticodeSignature '"+f.getPath()+"').SignerCertificate.Thumbprint";
Process powerShellProcess;
try {
powerShellProcess = Runtime.getRuntime().exec(command);
powerShellProcess.getOutputStream().close();
stdout = IOUtils.toString(powerShellProcess.getInputStream(), StandardCharsets.UTF_8).trim();
stderr = IOUtils.toString(powerShellProcess.getErrorStream(), StandardCharsets.UTF_8).trim();
} catch (IOException e) {
popup("Something went wrong while processing this file");
return;
}
if(!stderr.isEmpty() || stdout.isEmpty() || !stdout.matches("^[0-9a-fA-F]+$")) {
popup("Something went wrong while processing this file");
return;
}
try {
stdout = URLEncoder.encode(":\""+stdout+"\"", StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e1) {
popup("Error encounted while submitting data to VT.");
e1.printStackTrace();
}
String url = "https://www.virustotal.com/gui/search/signature"+stdout+"/files" ;
OpenBrowser(url);
} else {
popup("Sorry, this feature isn't supported on OSes other than windows!");
}
}
private void OpenBrowser(String URL) {
if(Desktop.isDesktopSupported()){
Desktop desktop = Desktop.getDesktop();
try {
desktop.browse(new URI(URL));
} catch (IOException | URISyntaxException e) {
popup("Error!");
}
}else{
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("xdg-open " + URL);
} catch (IOException e) {
popup("Error encountered while searching VT");
}
}
}
@Override
protected JPanel createWorkPanel() {
// Create the main text area and give it a scroll bar.
yaraTA = new JTextArea(12, 0);
scrollPane = new JScrollPane(yaraTA);
yaraTA.setWrapStyleWord(true);
yaraTA.setLineWrap(true);
// Create the instruction table and set it as a listener of the table model, so
// this gui will be notified when changes have been made (when the user has adjusted
// the mask settings). This allows us to dynamically update the yara string as
// the user is changing things.
InstructionTablePanel instructionTablePanel =
new InstructionTablePanel(searchData.getMaxNumOperands(), plugin, this);
instructionTablePanel.getTable().getModel().addTableModelListener(e -> {
generateYara();
});
Icon VTscaledIcon = ResourceManager.getScaledIcon(ResourceManager.loadImage("images/magnifier.png"), 16, 16);
Action actionVT = new SearchVTAction("VTgrep", VTscaledIcon, "Search using VTgrep", instructionTablePanel.getTable());
EmptyBorderButton VTButton = new EmptyBorderButton();
VTButton.setAction(actionVT);
VTButton.setName("VTgrep");
VTButton.setHideActionText(true);
instructionTablePanel.getTable().getToolbar().add(VTButton);
Icon SimilarIcon = ResourceManager.getScaledIcon(ResourceManager.loadImage("images/checkmark_green.gif"), 16, 16);
Action actionSimilarCheck = new SimilarAction("SimilarCheck", SimilarIcon, "Generate Similar Query", instructionTablePanel.getTable());
EmptyBorderButton SimilarButton = new EmptyBorderButton();
SimilarButton.setAction(actionSimilarCheck);
SimilarButton.setName("VTgrep");
SimilarButton.setHideActionText(true);
instructionTablePanel.getTable().getToolbar().add(SimilarButton);
Icon StrictIcon = ResourceManager.getScaledIcon(ResourceManager.loadImage("images/notes.gif"), 16, 16);
Action actionStrictCheck = new StrictAction("StrictCheck", StrictIcon, "Generate Similar Query (Strict)", instructionTablePanel.getTable());
EmptyBorderButton StrictButton = new EmptyBorderButton();
StrictButton.setAction(actionStrictCheck);
StrictButton.setName("VTgrep");
StrictButton.setHideActionText(true);
instructionTablePanel.getTable().getToolbar().add(StrictButton);
Icon CertIcon = ResourceManager.getScaledIcon(ResourceManager.loadImage("images/key.png"), 16, 16);
Action actionCertCheck = new CertAction("CertCheck", CertIcon, "Find files signed by the same certificate", instructionTablePanel.getTable());
EmptyBorderButton CertButton = new EmptyBorderButton();
CertButton.setAction(actionCertCheck);
CertButton.setName("VTgrep");
CertButton.setHideActionText(true);
instructionTablePanel.getTable().getToolbar().add(CertButton);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
verticalSplitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT, instructionTablePanel.getWorkPanel(), scrollPane);
mainPanel.add(verticalSplitter);
searchData.registerForGuiUpdates(instructionTablePanel.getTable());
verticalSplitter.setDividerLocation(splitterSave);
return mainPanel;
}
private void generateYara() {
try {
yaraTA.setText(generateYaraString("<insert name>"));
}
catch (Exception e1) {
Msg.error(this, "Error generating yara string: " + e1);
}
}
@Override
public void update(Observable o, Object arg) {
// Before rebuilding the UI, remember the splitter location so we can reset it
// afterwards.
if (verticalSplitter != null) {
splitterSave = verticalSplitter.getDividerLocation();
}
if (arg instanceof UpdateType) {
UpdateType type = (UpdateType) arg;
switch (type) {
case RELOAD:
revalidate();
break;
case UPDATE:
// do nothing
}
}
}
public ArrayList<Object> generate_slices(String[] sslices) {
ArrayList<Object> slices = new ArrayList<Object>();
for(String sslice: sslices) {
if(sslice.contains("?")) {
slices.add(new Wildcards(sslice));
} else {
slices.add(new Bytes(sslice));
}
}
return slices;
}
public ArrayList<Object> reduce_query(String str) {
ArrayList<Object> query_slices = generate_slices(str.split(" "));
ArrayList<Object> reduced_list = new ArrayList<Object>();
int prev = 0;
for(Object current: query_slices) {
if(reduced_list.isEmpty()) {
reduced_list.add(current);
} else {
prev = reduced_list.size() - 1;
if(reduced_list.get(prev) instanceof Wildcards) {
if(((Wildcards)reduced_list.get(prev)).combinable(current)) {
reduced_list.set(prev, ((Wildcards)reduced_list.get(prev)).combine(current));
} else {
reduced_list.add(current);
}
}
if(reduced_list.get(prev) instanceof Bytes) {
if(((Bytes)reduced_list.get(prev)).combinable(current)) {
reduced_list.set(prev, ((Bytes)reduced_list.get(prev)).combine(current));
} else {
reduced_list.add(current);
}
}
}
String toURL = "";
for(Object curr: reduced_list) {
if(curr instanceof Wildcards) {
toURL += ((Wildcards) curr).get();
} else {
toURL += ((Bytes) curr).get();
}
}
}
return reduced_list;
}
public String sanitize(ArrayList<Object> query) {
boolean Modified = true;
String outputSTR = "";
int query_len;
int qslice_index;
int next_qslice_index;
Object next_qslice;
Object qslice;
while(Modified) {
Modified = false;
query_len = query.size();
qslice_index = 0;
for(; qslice_index < query_len; qslice_index++) {
next_qslice_index = qslice_index + 1;
if(next_qslice_index != query_len) {
next_qslice = query.get(next_qslice_index);
if((boolean)(qslice = check_combinable_and_combine(query.get(qslice_index), next_qslice)) == true) {
query.set(qslice_index, qslice);
query.remove(next_qslice_index);
Modified = true;
break;
}
} else {
if((boolean) check_combinable_and_combine(query.get(qslice_index), null)) {
query.remove(qslice_index);
Modified = true;
break;
}
}
}
}
for(Object curr: query) {
if(curr instanceof Wildcards) {
outputSTR += ((Wildcards) curr).get();
} else {
outputSTR += ((Bytes) curr).get();
}
}
return outputSTR;
}
public Object check_combinable_and_combine(Object slice, Object next_slice) {
if(slice instanceof Bytes) {
return check_combinable_and_combine(((Bytes)slice), next_slice);
}
return check_combinable_and_combine(((Wildcards)slice), next_slice);
}
public Object check_combinable_and_combine(Bytes slice, Object next_slice) {
if(slice.combinable(next_slice)) {
return slice.combine(next_slice);
}
return false;
}
public Object check_combinable_and_combine(Wildcards slice, Object next_slice) {
if(slice.combinable(next_slice)) {
slice.combine(next_slice);
return true;
}
return false;
}
@Override
protected void revalidate() {
removeWorkPanel();
addWorkPanel(createWorkPanel());
generateYara();
}
}
private class Bytes {
private String bytes_stream;
public Bytes(String str) {
this.bytes_stream = str;
}
public void append(Object qslice) {
if(qslice instanceof Bytes) {
this.bytes_stream += ((Bytes) qslice).get();
} else {
this.bytes_stream += qslice;
}
}
public String get() {
return this.bytes_stream;
}
public int len() {
return this.bytes_stream.length();
}
public boolean combinable(Object next_qslice) {
if(next_qslice != null) {
if(next_qslice instanceof Bytes == false && this.len() >= 8) {
return false;
}
} else if(this.len() >= 8) {
return false;
}
return true;
}
public Object combine(Object next_qslice) {
String wcs_stream;
if(next_qslice != null) {
if(next_qslice instanceof Bytes || next_qslice instanceof String) {
this.append(next_qslice);
return this;
}
wcs_stream = "?".repeat(this.len());
((Wildcards) next_qslice).append(wcs_stream);
return next_qslice;
}
return this;
}
}
private class Wildcards {
private String wcs_stream;
private boolean packed = false;
public Wildcards(String str) {
this.wcs_stream = str;
this.pack();
}
private void append(Object qslice) {
int wcs_len;
int wcs_count;
if(!this.packed && qslice instanceof Wildcards == false) {
this.wcs_stream += qslice;
this.pack();
} else {
if(qslice instanceof Wildcards) {
wcs_len = this.len() + ((Wildcards) qslice).len();
} else {
wcs_len = this.len() + ((String) qslice).length();
}
wcs_count = wcs_len / 2;
this.wcs_stream = "["+wcs_count+"]" + "?".repeat(wcs_len % 2);
this.packed = true;
}
}
private String get() {
return this.wcs_stream;
}
private int len() {
int str_len = 0;
String wcs_len;
int question_index;
if(this.packed) {
wcs_len = this.wcs_stream.replaceAll("^\\[", "").replaceAll("\\]$", "");
//TODO: check the index of "?"
question_index = this.wcs_stream.indexOf("?");
if(question_index != -1) {
str_len = Integer.parseInt(wcs_len.replaceAll("\\]?$", "")) * 2;
str_len++;
} else {
str_len = Integer.parseInt(wcs_len) * 2;
}
return str_len;
}
return this.wcs_stream.length();
}
private void pack() {
int wcs_len;
int wcs_count;
if(!this.packed) {
wcs_len = this.wcs_stream.length();
if(wcs_len > 3) {
wcs_count = (wcs_len / 2);
this.wcs_stream = "["+wcs_count+"]" + "?".repeat(wcs_len % 2);
this.packed = true;
}
}
}
private boolean combinable(Object next_slice) {
if(next_slice != null) {
if(next_slice instanceof Bytes && ((Bytes)next_slice).len() >= 8) {
return false;
}
}
return true; //????
}
private Object combine(Object next_slice) {
String wcs_stream;
if(next_slice != null) {
if(next_slice instanceof Bytes) {
wcs_stream = "?".repeat(((Bytes)next_slice).len());
this.append(wcs_stream);
} else {
this.append(next_slice);
}
}
return this;
}
}
}