-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathVTgrepGHIDRA.java
1441 lines (1236 loc) · 58.4 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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
//Launches a GUI allowing users to generate VTGREP search strings based on a set of selected instructions and query VT.
// Based on GHIDRA's yara plugin
// This version is experimental, might be unstable.
// 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.
//@author: Kasif Dekel (@kasifdekel)
//@category Search.VTGREP
//@keybinding ctrl alt F9
//@menupath Search.VTgrepGHIDRA
//@toolbar vtgrepbutton.jpg
import docking.widgets.EmptyBorderButton;
import generic.continues.RethrowContinuesFactory;
import ghidra.app.plugin.core.instructionsearch.InstructionSearchPlugin;
import ghidra.app.plugin.core.instructionsearch.model.InstructionSearchData.UpdateType;
import ghidra.app.plugin.core.instructionsearch.model.InstructionTableDataObject;
import ghidra.app.plugin.core.instructionsearch.model.InstructionTableModel;
import ghidra.app.plugin.core.instructionsearch.ui.AbstractInstructionTable.OperandState;
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.util.InstructionSearchUtils;
import ghidra.app.script.GhidraScript;
import ghidra.app.util.bin.ByteProvider;
import ghidra.app.util.bin.MemoryByteProvider;
import ghidra.app.util.bin.format.pe.*;
import ghidra.app.util.bin.format.pe.PortableExecutable.SectionLayout;
import ghidra.program.model.address.Address;
import ghidra.program.model.lang.OperandType;
import ghidra.program.model.mem.Memory;
import ghidra.util.MD5Utilities;
import ghidra.util.Msg;
import org.apache.commons.io.IOUtils;
import resources.ResourceManager;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Observable;
import java.util.HashMap;
import java.util.Map;
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'.");
}
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;
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("\n}\n");
return yaraString.toString();
}
interface Slice {
void append(Slice slice);
String get();
int len();
boolean combinable(Slice next);
Slice combine(Slice next);
Slice mask();
boolean canCombine();
}
private class YaraDialog extends InstructionSearchDialog {
JScrollPane scrollPane;
private JTextArea yaraTA;
private JSplitPane verticalSplitter;
private int splitterSave = 200;
private YaraDialog() {
super(plugin, "Yara Rule Generator + VTgrepGHIDRA", null);
revalidate();
setPreferredSize(500, 400);
}
String GetSerial(String text) throws Exception {
int f = text.indexOf("Serial : ");
if(f == -1) {
throw new Exception("Please verify that osslsigncode is installed.");
}
String middle = text.substring(f + 9);
return middle.substring(0, middle.indexOf("\n"));
}
public void check_cert() {
String path = currentProgram.getExecutablePath();
String stdout = "";
String stderr = "";
String command = "";
String serial = "";
String Encoded = "";
Boolean isWindows = false;
File f = new File(path);
if (!f.exists()) { //for whatever reason :)
popup("Something went wrong while processing this file");
return;
}
if (System.getProperty("os.name").toLowerCase().contains("win")) {
isWindows = true;
command = "powershell.exe (Get-AuthenticodeSignature '" + f.getPath() + "').SignerCertificate.SerialNumber";
} else {
command = "osslsigncode verify -in " + f.getPath() + " -CAfile /dev/null";
}
Process shellProcess;
try {
shellProcess = Runtime.getRuntime().exec(command);
shellProcess.getOutputStream().close();
stdout = IOUtils.toString(shellProcess.getInputStream(), StandardCharsets.UTF_8).trim();
stderr = IOUtils.toString(shellProcess.getErrorStream(), StandardCharsets.UTF_8).trim();
} catch (IOException e) {
popup("Something went wrong while processing this file");
return;
}
if (!stderr.isEmpty() || stdout.isEmpty()) {
popup("Something went wrong while processing this file.");
return;
}
if(isWindows) {
if(stdout.matches("^[0-9a-fA-F]+$")) {
serial = stdout;
}
} else {
try {
serial = GetSerial(stdout);
} catch (Exception e) {
popup(e.getMessage());
}
}
if(serial.isEmpty()) {
popup("Something went wrong while processing this file");
return;
}
try {
Encoded = URLEncoder.encode(":\"" + serial + "\"", 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" + Encoded + "/files";
OpenBrowser(url);
}
private void gen_imphash() throws IOException {
String imports = "";
String lib = "";
String imphash = "";
String func = "";
Memory memory = currentProgram.getMemory();
Address baseAddr = memory.getMinAddress();
ByteProvider provider = new MemoryByteProvider(memory, baseAddr);
PortableExecutable pe = null;
try {
pe = PortableExecutable.createPortableExecutable(RethrowContinuesFactory.INSTANCE, provider, SectionLayout.MEMORY, false, false);
} catch (Exception e) {
popup("Unable to create PE from current program");
provider.close();
return;
}
NTHeader nth = pe.getNTHeader();
if (nth == null) {
popup("NT Header not found");
provider.close();
return;
}
OptionalHeader oph = nth.getOptionalHeader();
if (oph == null) {
popup("OP Header not found");
provider.close();
return;
}
try {
oph.processDataDirectories(monitor);
} catch (Exception e) {
System.out.println("only partial results!");
}
DataDirectory[] datadirs = oph.getDataDirectories();
if (datadirs == null) {
popup("Could not find any data directories");
provider.close();
return;
}
ImportDataDirectory idd = (ImportDataDirectory) datadirs[1];
if (idd == null) {
popup("Could not find the import dir");
provider.close();
return;
}
if (!idd.parse()) {
popup("Could not parse import dir");
provider.close();
return;
}
ImportInfo[] import_entries = idd.getImports();
if (import_entries.length == 0) {
popup("No imports found!");
provider.close();
return;
}
for (int i = 0; i < import_entries.length; i++) {
lib = import_entries[i].getDLL().toLowerCase();
func = import_entries[i].getName().toLowerCase();
if (lib.endsWith(".dll") || lib.endsWith(".sys") || lib.endsWith(".ocx")) {
lib = lib.split("\\.")[0];
}
if (Maps.maps.containsKey(lib)) {
func = Maps.maps.get(lib).get(func).toLowerCase();
} else {
func = func.replaceAll("ordinal_", "ord");
}
imports += lib + "." + func;
imports += (i != import_entries.length - 1 ? "," : "");
}
imphash = MD5Utilities.getMD5Hash(new ByteArrayInputStream(imports.getBytes(StandardCharsets.UTF_8)));
try {
imphash = URLEncoder.encode(":\"" + imphash + "\"", 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/imphash" + imphash + "/files";
OpenBrowser(url);
}
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);
Icon ImpHashIcon = ResourceManager.getScaledIcon(ResourceManager.loadImage("images/pencil.png"), 16, 16);
Action actionImpHashCheck = new ImpHashAction("ImpHashCheck", ImpHashIcon, "Search by ImpHash", instructionTablePanel.getTable());
EmptyBorderButton ImpHashButton = new EmptyBorderButton();
ImpHashButton.setAction(actionImpHashCheck);
ImpHashButton.setName("VTgrep");
ImpHashButton.setHideActionText(true);
instructionTablePanel.getTable().getToolbar().add(ImpHashButton);
Icon MultiIcon = ResourceManager.getScaledIcon(ResourceManager.loadImage("images/unknown.gif"), 16, 16);
Action actionMultiCheck = new MultiAction("MultiCheck", MultiIcon, "find similar files by different approaches", instructionTablePanel.getTable());
EmptyBorderButton MultiButton = new EmptyBorderButton();
MultiButton.setAction(actionMultiCheck);
MultiButton.setName("VTgrep");
MultiButton.setHideActionText(true);
instructionTablePanel.getTable().getToolbar().add(MultiButton);
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);
instructionTablePanel.getTable().getToolbar().remove(7);
instructionTablePanel.getTable().getToolbar().remove(8);
/*Component[] components = instructionTablePanel.getTable().getToolbar().getComponents();
for(Component component : components) {
if(component.getName() != null && component.getName().equals("manual entry")) {
instructionTablePanel.getTable().getToolbar().remove(component);
break;
}
}*/
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<Slice> generate_slices(String[] sslices) {
ArrayList<Slice> slices = new ArrayList<Slice>();
for (String sslice : sslices) {
if (sslice.contains("?")) {
slices.add(new Wildcards(sslice));
} else {
slices.add(new Bytes(sslice));
}
}
return slices;
}
public ArrayList<Slice> reduce_query(String str) {
ArrayList<Slice> query_slices = generate_slices(str.split(" "));
ArrayList<Slice> reduced_list = new ArrayList<Slice>();
int prev = 0;
for (Slice current : query_slices) {
if (reduced_list.isEmpty()) {
reduced_list.add(current);
} else {
prev = reduced_list.size() - 1;
if (reduced_list.get(prev).combinable(current)) {
reduced_list.set(prev, reduced_list.get(prev).combine(current));
} else {
reduced_list.add(current);
}
}
}
return reduced_list;
}
public String sanitize(ArrayList<Slice> query) {
boolean Modified = true;
String outputSTR = "";
int query_len;
int qslice_index;
int next_qslice_index;
Slice next_qslice;
Slice 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);
qslice = check_combinable_and_combine(query.get(qslice_index), next_qslice);
if (qslice != null) {
query.set(qslice_index, qslice);
query.remove(next_qslice_index);
Modified = true;
break;
}
} else {
if (check_combinable_and_combine(query.get(qslice_index), null) != null) {
query.remove(qslice_index);
Modified = true;
break;
}
}
}
}
for (Slice curr : query) {
outputSTR += curr.get();
}
return outputSTR;
}
public Slice check_combinable_and_combine(Slice slice, Slice next_slice) {
if (slice.combinable(next_slice)) {
return slice.combine(next_slice);
}
return null;
}
@Override
protected void revalidate() {
removeWorkPanel();
addWorkPanel(createWorkPanel());
generateYara();
}
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));
if (toURL.length() < MIN_QUERY_LEN || toURL.length() > MAX_QUERY_LEN) {
popup("Error! minimum bytes query length should be at least " + MIN_QUERY_LEN + " and below " + MAX_QUERY_LEN + "!");
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();
}
}
private class ImpHashAction extends AbstractAction {
InstructionTable instructionTable;
public ImpHashAction(String text, Icon icon, String desc, InstructionTable instructionTable) {
super(text, icon);
putValue(SHORT_DESCRIPTION, desc);
this.instructionTable = instructionTable;
}
@Override
public void actionPerformed(ActionEvent e) {
try {
gen_imphash();
} catch (IOException e1) {
popup("Something went wrong while trying to calculate IMPHASH.");
e1.printStackTrace();
}
InstructionTableModel model = (InstructionTableModel) this.instructionTable.getModel();
model.fireTableDataChanged();
}
}
private class MultiAction extends AbstractAction {
InstructionTable instructionTable;
public MultiAction(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 url = "https://www.virustotal.com/gui/search/similar-to:" + currentProgram.getExecutableSHA256() + "/files";
OpenBrowser(url);
InstructionTableModel model = (InstructionTableModel) this.instructionTable.getModel();
model.fireTableDataChanged();
}
}
}
static class Bytes implements Slice {
private String bytes_stream;
public Bytes(String str) {
this.bytes_stream = str;
}
public void append(Slice qslice) {
this.bytes_stream += qslice.get();
}
public String get() {
return this.bytes_stream;
}
public int len() {
return this.bytes_stream.length();
}
public boolean combinable(Slice next_qslice) {
if (next_qslice != null) {
return !(next_qslice instanceof Wildcards) || this.len() < 8;
} else return this.len() < 8;
}
public Slice combine(Slice next_qslice) {
Slice wcs_stream;
if (next_qslice != null) {
if (next_qslice instanceof Bytes) {
this.append(next_qslice);
return this;
}
wcs_stream = this.mask();
next_qslice.append(wcs_stream);
return next_qslice;
}
return this;
}
public Slice mask() {
return new Bytes("?".repeat(this.len()));
}
public boolean canCombine() {
return !(this.len() >= 8);
}
}
static class Wildcards implements Slice {
private String wcs_stream;
private boolean packed = false;
public Wildcards(String str) {
this.wcs_stream = str;
this.pack();
}
public void append(Slice qslice) {
int wcs_len;
int wcs_count;
if (!this.packed && !(qslice instanceof Wildcards)) {
this.wcs_stream += qslice.get();
this.pack();
} else {
wcs_len = this.len() + qslice.len();
wcs_count = wcs_len / 2;
this.wcs_stream = "[" + wcs_count + "]" + "?".repeat(wcs_len % 2);
this.packed = true;
}
}
public String get() {
return this.wcs_stream;
}
public int len() {
int str_len = 0;
String wcs_len;
int question_index;
if (this.packed) {
wcs_len = this.wcs_stream.replaceAll("^\\[", "").replaceAll("\\]$", "");
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;
}
}
}
public boolean combinable(Slice next_slice) {
if (next_slice != null) {
return next_slice.canCombine();
}
return true;
}
public Slice combine(Slice next_slice) {
if (next_slice != null) {
this.append(next_slice.mask());
}
return this;
}
public Slice mask() {
return this;
}
public boolean canCombine() {
return true;
}
}
public static final class Maps { // this should be a part of GHIDRA tho, but o well...
public static final Map<String, String> oleaut32;
public static final Map<String, String> ws2_32;
public static final Map<String, Map<String, String>> maps;
static {
oleaut32 = new HashMap<>();
oleaut32.put("ordinal_2", "SysAllocString");
oleaut32.put("ordinal_3", "SysReAllocString");
oleaut32.put("ordinal_4", "SysAllocStringLen");
oleaut32.put("ordinal_5", "SysReAllocStringLen");
oleaut32.put("ordinal_6", "SysFreeString");
oleaut32.put("ordinal_7", "SysStringLen");
oleaut32.put("ordinal_8", "VariantInit");
oleaut32.put("ordinal_9", "VariantClear");
oleaut32.put("ordinal_10", "VariantCopy");
oleaut32.put("ordinal_11", "VariantCopyInd");
oleaut32.put("ordinal_12", "VariantChangeType");
oleaut32.put("ordinal_13", "VariantTimeToDosDateTime");
oleaut32.put("ordinal_14", "DosDateTimeToVariantTime");
oleaut32.put("ordinal_15", "SafeArrayCreate");
oleaut32.put("ordinal_16", "SafeArrayDestroy");
oleaut32.put("ordinal_17", "SafeArrayGetDim");
oleaut32.put("ordinal_18", "SafeArrayGetElemsize");
oleaut32.put("ordinal_19", "SafeArrayGetUBound");
oleaut32.put("ordinal_20", "SafeArrayGetLBound");
oleaut32.put("ordinal_21", "SafeArrayLock");
oleaut32.put("ordinal_22", "SafeArrayUnlock");
oleaut32.put("ordinal_23", "SafeArrayAccessData");
oleaut32.put("ordinal_24", "SafeArrayUnaccessData");
oleaut32.put("ordinal_25", "SafeArrayGetElement");
oleaut32.put("ordinal_26", "SafeArrayPutElement");
oleaut32.put("ordinal_27", "SafeArrayCopy");
oleaut32.put("ordinal_28", "DispGetParam");
oleaut32.put("ordinal_29", "DispGetIDsOfNames");
oleaut32.put("ordinal_30", "DispInvoke");
oleaut32.put("ordinal_31", "CreateDispTypeInfo");
oleaut32.put("ordinal_32", "CreateStdDispatch");
oleaut32.put("ordinal_33", "RegisterActiveObject");
oleaut32.put("ordinal_34", "RevokeActiveObject");
oleaut32.put("ordinal_35", "GetActiveObject");
oleaut32.put("ordinal_36", "SafeArrayAllocDescriptor");
oleaut32.put("ordinal_37", "SafeArrayAllocData");
oleaut32.put("ordinal_38", "SafeArrayDestroyDescriptor");
oleaut32.put("ordinal_39", "SafeArrayDestroyData");
oleaut32.put("ordinal_40", "SafeArrayRedim");
oleaut32.put("ordinal_41", "SafeArrayAllocDescriptorEx");
oleaut32.put("ordinal_42", "SafeArrayCreateEx");
oleaut32.put("ordinal_43", "SafeArrayCreateVectorEx");
oleaut32.put("ordinal_44", "SafeArraySetRecordInfo");
oleaut32.put("ordinal_45", "SafeArrayGetRecordInfo");
oleaut32.put("ordinal_46", "VarParseNumFromStr");
oleaut32.put("ordinal_47", "VarNumFromParseNum");
oleaut32.put("ordinal_48", "VarI2FromUI1");
oleaut32.put("ordinal_49", "VarI2FromI4");
oleaut32.put("ordinal_50", "VarI2FromR4");
oleaut32.put("ordinal_51", "VarI2FromR8");
oleaut32.put("ordinal_52", "VarI2FromCy");
oleaut32.put("ordinal_53", "VarI2FromDate");
oleaut32.put("ordinal_54", "VarI2FromStr");
oleaut32.put("ordinal_55", "VarI2FromDisp");
oleaut32.put("ordinal_56", "VarI2FromBool");
oleaut32.put("ordinal_57", "SafeArraySetIID");
oleaut32.put("ordinal_58", "VarI4FromUI1");
oleaut32.put("ordinal_59", "VarI4FromI2");
oleaut32.put("ordinal_60", "VarI4FromR4");
oleaut32.put("ordinal_61", "VarI4FromR8");
oleaut32.put("ordinal_62", "VarI4FromCy");
oleaut32.put("ordinal_63", "VarI4FromDate");
oleaut32.put("ordinal_64", "VarI4FromStr");
oleaut32.put("ordinal_65", "VarI4FromDisp");
oleaut32.put("ordinal_66", "VarI4FromBool");
oleaut32.put("ordinal_67", "SafeArrayGetIID");
oleaut32.put("ordinal_68", "VarR4FromUI1");
oleaut32.put("ordinal_69", "VarR4FromI2");
oleaut32.put("ordinal_70", "VarR4FromI4");
oleaut32.put("ordinal_71", "VarR4FromR8");
oleaut32.put("ordinal_72", "VarR4FromCy");
oleaut32.put("ordinal_73", "VarR4FromDate");
oleaut32.put("ordinal_74", "VarR4FromStr");
oleaut32.put("ordinal_75", "VarR4FromDisp");
oleaut32.put("ordinal_76", "VarR4FromBool");
oleaut32.put("ordinal_77", "SafeArrayGetVartype");
oleaut32.put("ordinal_78", "VarR8FromUI1");
oleaut32.put("ordinal_79", "VarR8FromI2");
oleaut32.put("ordinal_80", "VarR8FromI4");
oleaut32.put("ordinal_81", "VarR8FromR4");
oleaut32.put("ordinal_82", "VarR8FromCy");
oleaut32.put("ordinal_83", "VarR8FromDate");
oleaut32.put("ordinal_84", "VarR8FromStr");
oleaut32.put("ordinal_85", "VarR8FromDisp");
oleaut32.put("ordinal_86", "VarR8FromBool");
oleaut32.put("ordinal_87", "VarFormat");
oleaut32.put("ordinal_88", "VarDateFromUI1");
oleaut32.put("ordinal_89", "VarDateFromI2");
oleaut32.put("ordinal_90", "VarDateFromI4");
oleaut32.put("ordinal_91", "VarDateFromR4");
oleaut32.put("ordinal_92", "VarDateFromR8");
oleaut32.put("ordinal_93", "VarDateFromCy");
oleaut32.put("ordinal_94", "VarDateFromStr");
oleaut32.put("ordinal_95", "VarDateFromDisp");
oleaut32.put("ordinal_96", "VarDateFromBool");