-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
BytecodeViewer.java
859 lines (767 loc) · 33.5 KB
/
BytecodeViewer.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
package the.bytecode.club.bytecodeviewer;
import java.awt.event.KeyEvent;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;
import me.konloch.kontainer.io.DiskWriter;
import me.konloch.kontainer.io.HTTPRequest;
import org.apache.commons.io.FileUtils;
import org.objectweb.asm.tree.ClassNode;
import the.bytecode.club.bootloader.Boot;
import the.bytecode.club.bytecodeviewer.api.ClassNodeLoader;
import the.bytecode.club.bytecodeviewer.compilers.Compilers;
import the.bytecode.club.bytecodeviewer.gui.ClassViewer;
import the.bytecode.club.bytecodeviewer.gui.FileNavigationPane;
import the.bytecode.club.bytecodeviewer.gui.MainViewerGUI;
import the.bytecode.club.bytecodeviewer.gui.RunOptions;
import the.bytecode.club.bytecodeviewer.gui.SearchingPane;
import the.bytecode.club.bytecodeviewer.gui.SystemErrConsole;
import the.bytecode.club.bytecodeviewer.gui.WorkPane;
import the.bytecode.club.bytecodeviewer.obfuscators.mapping.Refactorer;
import the.bytecode.club.bytecodeviewer.plugin.PluginManager;
import the.bytecode.club.bytecodeviewer.util.*;
import static the.bytecode.club.bytecodeviewer.Constants.*;
/***************************************************************************
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
* Copyright (C) 2014 Kalen 'Konloch' Kinloch - http://bytecodeviewer.com *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
/**
* A lightweight Java Reverse Engineering suite, developed by Konloch - http://konloch.me
*
* All you have to do is add a jar or class file into the workspace,
* select the file you want then it will start decompiling the class in the background.
* When it's done it will show the Source code, Bytecode and Hexcode of the class file you chose.
*
* There is also a plugin system that will allow you to interact with the loaded classfiles.
* For example you can write a String deobfuscator, a malicious code searcher,
* or anything else you can think of.
*
* You can either use one of the pre-written plugins, or write your own. It supports java scripting.
* Once a plugin is activated, it will send a ClassNode ArrayList of every single class loaded in the
* file system to the execute function, this allows the user to handle it completely using ASM.
*
* Are you a Java Reverse Engineer? Or maybe you want to learn Java Reverse Engineering?
* Join The Bytecode Club, we're noob friendly, and censorship free.
* http://the.bytecode.club
*
* TODO:
* open as folder doesn't actually work
* smali compile
* Finish dragging code
* Finish right-click tab menu detection
* make it use that global last used inside of export as jar
* Add https://github.com/ptnkjke/Java-Bytecode-Editor visualize as a plugin
* Add https://github.com/exbin/bined as the replacement Hed Viewer/Editor
* make zipfile not include the decode shit
* add stackmapframes to bytecode decompiler
* make ez-injection plugin console show all sys.out calls
* add JEB decompiler optionally, requires them to add jeb library jar externally and disable update check ?
* add decompile as zip for krakatau-bytecode, jd-gui and smali for CLI
* add decompile all as zip for CLI
* fix hook inject for EZ-Injection
* fix classfile searcher
* make the decompilers launch in a separate process
*
* @author Konloch
* @author The entire BCV community
*/
public class BytecodeViewer
{
public static String[] args;
public static MainViewerGUI viewer = null;
public static ClassNodeLoader loader = new ClassNodeLoader(); //might be insecure due to assholes targeting BCV,
public static SecurityMan sm = new SecurityMan(); //might be insecure due to assholes targeting BCV, however
public static Refactorer refactorer = new Refactorer();
public static List<FileContainer> files = new ArrayList<>(); //all of BCV's loaded files/classes/etc
public static List<Process> createdProcesses = new ArrayList<>();
/**
* The version checker thread
*/
private static final Thread versionChecker = new Thread(new VersionChecker());
/**
* Pings back to bytecodeviewer.com to be added into the total running statistics
*/
private static final Thread pingBack = new Thread(() -> {
try {
new HTTPRequest(new URL("https://bytecodeviewer.com/add.php")).read();
} catch (Exception e) {
Configuration.pingback = false;
}
});
/**
* Downloads & installs the krakatau & enjarify zips
*/
private static final Thread installFatJar = new Thread(() -> {
try {
if (OFFLINE_MODE) {
Boot.dropKrakatau();
Boot.dropEnjarify();
} else {
Boot.populateUrlList();
Boot.populateLibsDirectory();
Boot.downloadZipsOnly();
Boot.checkKrakatau();
Boot.checkEnjarify();
}
} catch (Exception e) {
e.printStackTrace();
}
});
/**
* Used to check incase booting failed for some reason, this kicks in as a fail safe
*/
private static final Thread bootCheck = new Thread(new BootCheck());
/**
* Grab the byte array from the loaded Class object
*
* @param clazz
* @return
* @throws IOException
*/
public static byte[] getClassFile(Class<?> clazz) throws IOException {
InputStream is = clazz.getResourceAsStream("/" + clazz.getName().replace('.', '/') + ".class");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int r;
byte[] buffer = new byte[8192];
while ((r = Objects.requireNonNull(is).read(buffer)) >= 0) {
baos.write(buffer, 0, r);
}
return baos.toByteArray();
}
/**
* Main startup
*
* @param args files you want to open or CLI
*/
public static void main(String[] args) {
BytecodeViewer.args = args;
System.out.println("https://the.bytecode.club - Created by @Konloch - Bytecode Viewer " + VERSION + ", " + "Fat-Jar: " + FAT_JAR);
System.setSecurityManager(sm);
try {
UIManager.put("MenuItem.disabledAreNavigable", Boolean.FALSE);
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
if (PREVIEW_COPY && !CommandLineInput.containsCommand(args))
showMessage("WARNING: This is a preview/dev copy, you WON'T be alerted when " + VERSION + " is "
+ "actually out if you use this." + nl +
"Make sure to watch the repo: https://github.com/Konloch/bytecode-viewer for " + VERSION + "'s release");
viewer = new MainViewerGUI();
Settings.loadSettings();
int CLI = CommandLineInput.parseCommandLine(args);
if (CLI == CommandLineInput.STOP)
return;
if (!FAT_JAR) {
bootCheck.start();
Boot.boot(args, CLI != CommandLineInput.OPEN_FILE);
} else
installFatJar.start();
if (CLI == CommandLineInput.OPEN_FILE)
BytecodeViewer.boot(false);
else {
BytecodeViewer.boot(true);
CommandLineInput.executeCommandLine(args);
}
} catch (Exception e) {
new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e);
}
}
/**
* Boot after all of the libraries have been loaded
*
* @param cli is it running CLI mode or not
*/
public static void boot(boolean cli) {
cleanup();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
for (Process proc : createdProcesses)
proc.destroy();
Settings.saveSettings();
cleanup();
}));
viewer.calledAfterLoad();
resetRecentFilesMenu();
if (!Configuration.pingback) {
pingBack.start();
Configuration.pingback = true;
}
if (viewer.chckbxmntmNewCheckItem_12.isSelected())
versionChecker.start();
if (!cli)
viewer.setVisible(true);
System.out.println("Start up took " + ((System.currentTimeMillis() - Configuration.start) / 1000) + " seconds");
if (!cli)
if (args.length >= 1)
for (String s : args)
openFiles(new File[]{new File(s)}, true);
}
/**
* because Smali and Baksmali System.exit if it failed
*
* @param i
*/
public static void exit(int i) {
}
/**
* Returns the java command it can use to launch the decompilers
*
* @return
*/
public static synchronized String getJavaCommand() {
try {
sm.stopBlocking();
ProcessBuilder pb = new ProcessBuilder("java", "-version");
pb.start();
sm.setBlocking();
return "java"; //java is set
} catch (Exception e) { //ignore
sm.setBlocking();
boolean empty = Configuration.java.isEmpty();
while (empty) {
showMessage("You need to set your Java path, this requires the JRE to be downloaded." + nl +
"(C:/Program Files/Java/JDK_xx/bin/java.exe)");
viewer.selectJava();
empty = Configuration.java.isEmpty();
}
}
return Configuration.java;
}
/**
* Returns the currently opened ClassNode
*
* @return the currently opened ClassNode
*/
public static ClassNode getCurrentlyOpenedClassNode() {
return viewer.workPane.getCurrentViewer().cn;
}
/**
* Returns the ClassNode by the specified name
*
* @param name the class name
* @return the ClassNode instance
*/
public static ClassNode getClassNode(String name) {
for (FileContainer container : files)
for (ClassNode c : container.classes)
if (c.name.equals(name))
return c;
return null;
}
public static FileContainer getFileContainer(String name) {
for (FileContainer container : files)
if (container.name.equals(name))
return container;
return null;
}
public static List<FileContainer> getFiles() {
return files;
}
public static ClassNode getClassNode(FileContainer container, String name) {
for (ClassNode c : container.classes)
if (c.name.equals(name))
return c;
return null;
}
/**
* Grabs the file contents of the loaded resources.
*
* @param name the file name
* @return the file contents as a byte[]
*/
public static byte[] getFileContents(String name) {
for (FileContainer container : files) {
HashMap<String, byte[]> files = container.files;
if (files.containsKey(name))
return files.get(name);
}
return null;
}
/**
* Replaces an old node with a new instance
*
* @param oldNode the old instance
* @param newNode the new instance
*/
public static void updateNode(ClassNode oldNode, ClassNode newNode) {
for (FileContainer container : files) {
if (container.classes.remove(oldNode))
container.classes.add(newNode);
}
}
/**
* Gets all of the loaded classes as an array list
*
* @return the loaded classes as an array list
*/
public static ArrayList<ClassNode> getLoadedClasses() {
ArrayList<ClassNode> a = new ArrayList<>();
for (FileContainer container : files)
for (ClassNode c : container.classes)
if (!a.contains(c))
a.add(c);
return a;
}
/**
* Compile all of the compilable panes that're opened.
*
* @param message if it should send a message saying it's compiled sucessfully.
* @return true if no errors, false if it failed to compile.
*/
public static boolean compile(boolean message) {
BytecodeViewer.viewer.setIcon(true);
boolean actuallyTried = false;
for (java.awt.Component c : BytecodeViewer.viewer.workPane.getLoadedViewers()) {
if (c instanceof ClassViewer) {
ClassViewer cv = (ClassViewer) c;
if (cv.smali1 != null && cv.smali1.isEditable() ||
cv.smali2 != null && cv.smali2.isEditable() ||
cv.smali3 != null && cv.smali3.isEditable()) {
actuallyTried = true;
Object[] smali = cv.getSmali();
if (smali != null) {
ClassNode origNode = (ClassNode) smali[0];
String smaliText = (String) smali[1];
byte[] smaliCompiled =
Compilers.smali.compile(smaliText,
origNode.name);
if (smaliCompiled != null) {
try {
ClassNode newNode = JarUtils.getNode(smaliCompiled);
BytecodeViewer.updateNode(origNode, newNode);
} catch (Exception e) {
e.printStackTrace();
}
} else {
BytecodeViewer.showMessage("There has been an error with assembling your Smali code, "
+ "please check this. Class: " + origNode.name);
BytecodeViewer.viewer.setIcon(false);
return false;
}
}
}
if (cv.krakatau1 != null && cv.krakatau1.isEditable() ||
cv.krakatau2 != null && cv.krakatau2.isEditable() ||
cv.krakatau3 != null && cv.krakatau3.isEditable()) {
actuallyTried = true;
Object[] krakatau = cv.getKrakatau();
if (krakatau != null) {
ClassNode origNode = (ClassNode) krakatau[0];
String krakatauText = (String) krakatau[1];
byte[] krakatauCompiled =
Compilers.krakatau.compile(krakatauText,
origNode.name);
if (krakatauCompiled != null) {
try {
ClassNode newNode = JarUtils.getNode(krakatauCompiled);
BytecodeViewer.updateNode(origNode, newNode);
} catch (Exception e) {
e.printStackTrace();
}
} else {
BytecodeViewer.showMessage("There has been an error with assembling your Krakatau "
+ "Bytecode, please check this. Class: " + origNode.name);
BytecodeViewer.viewer.setIcon(false);
return false;
}
}
}
if (cv.java1 != null && cv.java1.isEditable() ||
cv.java2 != null && cv.java2.isEditable() ||
cv.java3 != null && cv.java3.isEditable()) {
actuallyTried = true;
Object[] java = cv.getJava();
if (java != null) {
ClassNode origNode = (ClassNode) java[0];
String javaText = (String) java[1];
SystemErrConsole errConsole = new SystemErrConsole("Java Compile Issues");
errConsole.setText("Error compiling class: " + origNode.name + nl + "Keep in mind most "
+ "decompilers cannot produce compilable classes" + nl + nl);
byte[] javaCompiled =
Compilers.java.compile(javaText,
origNode.name);
if (javaCompiled != null) {
try {
ClassNode newNode = JarUtils.getNode(javaCompiled);
BytecodeViewer.updateNode(origNode, newNode);
} catch (Exception e) {
e.printStackTrace();
}
errConsole.finished();
} else {
errConsole.pretty();
errConsole.setVisible(true);
errConsole.finished();
BytecodeViewer.viewer.setIcon(false);
return false;
}
}
}
}
}
if (message)
if (actuallyTried)
BytecodeViewer.showMessage("Compiled Successfully.");
else
BytecodeViewer.showMessage("You have no editable panes opened, make one editable and try again.");
BytecodeViewer.viewer.setIcon(false);
return true;
}
/**
* Opens a file, optional if it should append to the recent files menu
*
* @param files the file(s) you wish to open
* @param recentFiles if it should append to the recent files menu
*/
public static void openFiles(final File[] files, boolean recentFiles) {
if (recentFiles)
for (File f : files)
if (f.exists())
BytecodeViewer.addRecentFile(f);
BytecodeViewer.viewer.setIcon(true);
Configuration.needsReDump = true;
Thread t = new Thread(new OpenFile(files));
t.start();
}
/**
* Starts the specified plugin
*
* @param file the file of the plugin
*/
public static void startPlugin(File file) {
if (!file.exists())
return;
try {
PluginManager.runPlugin(file);
} catch (Throwable e) {
new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e);
}
addRecentPlugin(file);
}
/**
* Send a message to alert the user
*
* @param message the message you need to send
*/
public static void showMessage(String message) {
JOptionPane.showMessageDialog(viewer, message);
}
/**
* Resets the workspace with optional user input required
*
* @param ask if should require user input or not
*/
public static void resetWorkSpace(boolean ask) {
if (ask) {
JOptionPane pane = new JOptionPane(
"Are you sure you want to reset the workspace?\n\rIt will also reset your file navigator and "
+ "search.");
Object[] options = new String[]{"Yes", "No"};
pane.setOptions(options);
JDialog dialog = pane.createDialog(viewer,
"Bytecode Viewer - Reset Workspace");
dialog.setVisible(true);
Object obj = pane.getValue();
int result = -1;
for (int k = 0; k < options.length; k++)
if (options[k].equals(obj))
result = k;
if (result != 0)
return;
}
files.clear();
LazyNameUtil.reset();
Objects.requireNonNull(MainViewerGUI.getComponent(FileNavigationPane.class)).resetWorkspace();
Objects.requireNonNull(MainViewerGUI.getComponent(WorkPane.class)).resetWorkspace();
Objects.requireNonNull(MainViewerGUI.getComponent(SearchingPane.class)).resetWorkspace();
the.bytecode.club.bytecodeviewer.api.BytecodeViewer.getClassNodeLoader().clear();
}
private static final List<String> killList = new ArrayList<>();
/**
* Add the recent file
*
* @param f the recent file
*/
public static void addRecentFile(File f) {
for (int i = 0; i < recentFiles.size(); i++) { // remove dead strings
String s = recentFiles.get(i);
if (s.isEmpty() || i > maxRecentFiles)
killList.add(s);
}
if (!killList.isEmpty()) {
for (String s : killList)
recentFiles.remove(s);
killList.clear();
}
// already added on the list
recentFiles.remove(f.getAbsolutePath());
if (recentFiles.size() >= maxRecentFiles)
recentFiles.remove(maxRecentFiles - 1); // zero indexing
recentFiles.add(0, f.getAbsolutePath());
DiskWriter.replaceFile(filesName, MiscUtils.listToString(recentFiles), false);
resetRecentFilesMenu();
}
private static final List<String> killList2 = new ArrayList<>();
/**
* Add to the recent plugin list
*
* @param f the plugin file
*/
public static void addRecentPlugin(File f) {
for (int i = 0; i < recentPlugins.size(); i++) { // remove dead strings
String s = recentPlugins.get(i);
if (s.isEmpty() || i > maxRecentFiles)
killList2.add(s);
}
if (!killList2.isEmpty()) {
for (String s : killList2)
recentPlugins.remove(s);
killList2.clear();
}
// already added on the list
recentPlugins.remove(f.getAbsolutePath());
if (recentPlugins.size() >= maxRecentFiles)
recentPlugins.remove(maxRecentFiles - 1); // zero indexing
recentPlugins.add(0, f.getAbsolutePath());
DiskWriter.replaceFile(pluginsName, MiscUtils.listToString(recentPlugins), false);
resetRecentFilesMenu();
}
/**
* resets the recent files menu
*/
public static void resetRecentFilesMenu() {
viewer.recentFilesSecondaryMenu.removeAll();
for (String s : recentFiles)
if (!s.isEmpty()) {
JMenuItem m = new JMenuItem(s);
m.addActionListener(e -> {
JMenuItem m12 = (JMenuItem) e.getSource();
openFiles(new File[]{new File(m12.getText())}, true);
});
viewer.recentFilesSecondaryMenu.add(m);
}
viewer.recentPluginsSecondaryMenu.removeAll();
for (String s : recentPlugins)
if (!s.isEmpty()) {
JMenuItem m = new JMenuItem(s);
m.addActionListener(e -> {
JMenuItem m1 = (JMenuItem) e.getSource();
startPlugin(new File(m1.getText()));
});
viewer.recentPluginsSecondaryMenu.add(m);
}
}
/**
* Clears the temp directory
*/
public static void cleanup() {
File tempF = new File(tempDirectory);
try {
FileUtils.deleteDirectory(tempF);
} catch (Exception ignored) {
}
while (!tempF.exists()) // keep making dirs
tempF.mkdir();
}
/**
* Checks the hotkeys
*
* @param e
*/
public static void checkHotKey(KeyEvent e) {
if (System.currentTimeMillis() - Configuration.lastHotKeyExecuted <= (4000))
return;
if ((e.getKeyCode() == KeyEvent.VK_O) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
Configuration.lastHotKeyExecuted = System.currentTimeMillis();
JFileChooser fc = new JFileChooser();
try {
fc.setSelectedFile(new File(Configuration.lastDirectory));
} catch (Exception ignored) {
}
fc.setFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
if (f.isDirectory())
return true;
String extension = MiscUtils.extension(f.getAbsolutePath());
return extension.equals("jar") || extension.equals("zip")
|| extension.equals("class") || extension.equals("apk")
|| extension.equals("dex");
}
@Override
public String getDescription() {
return "APKs, DEX, Class Files or Zip/Jar Archives";
}
});
fc.setFileHidingEnabled(false);
fc.setAcceptAllFileFilterUsed(false);
int returnVal = fc.showOpenDialog(BytecodeViewer.viewer);
if (returnVal == JFileChooser.APPROVE_OPTION) {
Configuration.lastDirectory = fc.getSelectedFile().getAbsolutePath();
try {
BytecodeViewer.viewer.setIcon(true);
BytecodeViewer.openFiles(new File[]{fc.getSelectedFile()}, true);
BytecodeViewer.viewer.setIcon(false);
} catch (Exception e1) {
new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e1);
}
}
} else if ((e.getKeyCode() == KeyEvent.VK_N) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
Configuration.lastHotKeyExecuted = System.currentTimeMillis();
BytecodeViewer.resetWorkSpace(true);
} else if ((e.getKeyCode() == KeyEvent.VK_T) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
Configuration.lastHotKeyExecuted = System.currentTimeMillis();
Thread t = new Thread(() -> BytecodeViewer.compile(true));
t.start();
} else if ((e.getKeyCode() == KeyEvent.VK_R) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
Configuration.lastHotKeyExecuted = System.currentTimeMillis();
if (BytecodeViewer.getLoadedClasses().isEmpty()) {
BytecodeViewer.showMessage("First open a class, jar, zip, apk or dex file.");
return;
}
new RunOptions().setVisible(true);
} else if ((e.getKeyCode() == KeyEvent.VK_S) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
Configuration.lastHotKeyExecuted = System.currentTimeMillis();
if (BytecodeViewer.getLoadedClasses().isEmpty()) {
BytecodeViewer.showMessage("First open a class, jar, zip, apk or dex file.");
return;
}
Thread t = new Thread(() -> {
if (viewer.compileOnSave.isSelected() && !BytecodeViewer.compile(false))
return;
JFileChooser fc = new JFileChooser();
fc.setFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory() || MiscUtils.extension(f.getAbsolutePath()).equals("zip");
}
@Override
public String getDescription() {
return "Zip Archives";
}
});
fc.setFileHidingEnabled(false);
fc.setAcceptAllFileFilterUsed(false);
int returnVal = fc.showSaveDialog(viewer);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
if (!file.getAbsolutePath().endsWith(".zip"))
file = new File(file.getAbsolutePath() + ".zip");
if (file.exists()) {
JOptionPane pane = new JOptionPane(
"Are you sure you wish to overwrite this existing file?");
Object[] options = new String[]{"Yes", "No"};
pane.setOptions(options);
JDialog dialog = pane.createDialog(BytecodeViewer.viewer,
"Bytecode Viewer - Overwrite File");
dialog.setVisible(true);
Object obj = pane.getValue();
int result = -1;
for (int k = 0; k < options.length; k++)
if (options[k].equals(obj))
result = k;
if (result == 0) {
file.delete();
} else {
return;
}
}
final File file2 = file;
BytecodeViewer.viewer.setIcon(true);
Thread t1 = new Thread(() -> {
JarUtils.saveAsJar(BytecodeViewer.getLoadedClasses(),
file2.getAbsolutePath());
BytecodeViewer.viewer.setIcon(false);
});
t1.start();
}
});
t.start();
} else if ((e.getKeyCode() == KeyEvent.VK_W) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
Configuration.lastHotKeyExecuted = System.currentTimeMillis();
if (viewer.workPane.getCurrentViewer() != null)
viewer.workPane.tabs.remove(viewer.workPane.getCurrentViewer());
}
}
public static File[] dumpTempFile(FileContainer container) {
File[] files = new File[2];
//currently won't optimize if you've got two containers with the same name, will need to add this later
if (!LazyNameUtil.SAME_NAME_JAR_WORKSPACE) {
if (Configuration.krakatauTempJar != null && !Configuration.krakatauTempJar.exists()) {
Configuration.needsReDump = true;
}
if (Configuration.needsReDump && Configuration.krakatauTempJar != null) {
Configuration.krakatauTempDir = null;
Configuration.krakatauTempJar = null;
}
boolean passes = false;
if (BytecodeViewer.viewer.viewPane1.getGroup().isSelected(BytecodeViewer.viewer.viewPane1.getKrakatau().getJava().getModel()))
passes = true;
else if (BytecodeViewer.viewer.viewPane1.getGroup().isSelected(BytecodeViewer.viewer.viewPane1.getKrakatau().getBytecode().getModel()))
passes = true;
else if (BytecodeViewer.viewer.viewPane2.getGroup().isSelected(BytecodeViewer.viewer.viewPane2.getKrakatau().getJava().getModel()))
passes = true;
else if (BytecodeViewer.viewer.viewPane2.getGroup().isSelected(BytecodeViewer.viewer.viewPane2.getKrakatau().getBytecode().getModel()))
passes = true;
else if (BytecodeViewer.viewer.viewPane3.getGroup().isSelected(BytecodeViewer.viewer.viewPane3.getKrakatau().getJava().getModel()))
passes = true;
else if (BytecodeViewer.viewer.viewPane3.getGroup().isSelected(BytecodeViewer.viewer.viewPane3.getKrakatau().getBytecode().getModel()))
passes = true;
if (Configuration.krakatauTempJar != null || !passes) {
files[0] = Configuration.krakatauTempJar;
files[1] = Configuration.krakatauTempDir;
return files;
}
}
Configuration.currentlyDumping = true;
Configuration.needsReDump = false;
Configuration.krakatauTempDir = new File(tempDirectory + fs + MiscUtils.randomString(32) + fs);
Configuration.krakatauTempDir.mkdir();
Configuration.krakatauTempJar = new File(tempDirectory + fs + "temp" + MiscUtils.randomString(32) + ".jar");
//krakatauTempJar = new File(BytecodeViewer.tempDirectory + BytecodeViewer.fs + "temp" + MiscUtils
// .randomString(32) + ".jar."+container.name);
JarUtils.saveAsJarClassesOnly(container.classes, Configuration.krakatauTempJar.getAbsolutePath());
Configuration.currentlyDumping = false;
files[0] = Configuration.krakatauTempJar;
files[1] = Configuration.krakatauTempDir;
return files;
}
public synchronized static void rtCheck() {
if (Configuration.rt.isEmpty()) {
if (RT_JAR.exists()) {
Configuration.rt = RT_JAR.getAbsolutePath();
} else if (RT_JAR_DUMPED.exists()) {
Configuration.rt = RT_JAR_DUMPED.getAbsolutePath();
} else {
try {
JRTExtractor.extractRT(RT_JAR_DUMPED.getAbsolutePath());
Configuration.rt = RT_JAR_DUMPED.getAbsolutePath();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
}