-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeObfuscatorVersion1.java
More file actions
880 lines (742 loc) · 18.3 KB
/
CodeObfuscatorVersion1.java
File metadata and controls
880 lines (742 loc) · 18.3 KB
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
import javax.swing.*;
import java.awt.event.*;
import static java.lang.System.*;
import java.awt.*;
import javax.swing.border.*;
import java.io.*;
import java.util.*;
import java.util.regex.*;
import javax.swing.tree.*;
public class CodeObfuscatorVersion1
{
public static void main(String... args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
TFrame tf1=new TFrame();
tf1.setVisible(true);
tf1.setDefaultCloseOperation(tf1.EXIT_ON_CLOSE);
}
});
}
}
class TFrame extends JFrame
{
private JTextArea jta1,jta2;
private JEditorPane jep1;
private JFileChooser jfc1;
private String path;
private Font f1;
TFrame()
{
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension d=kit.getScreenSize();
out.println(d.height);
setSize(d);
//new Dimension(800,600));
setLayout(null);
JMenuBar jmb1=new JMenuBar();
setJMenuBar(jmb1);
Dimension d1=calculateSize(d);
if(path==null)
out.println("hello");
JMenu file=new JMenu("File");
jmb1.add(file);
JMenuItem open_menu=new JMenuItem("Open",'O');
file.add(open_menu);
//jmi1.setEnabled(false);
JMenuItem get_menu=new JMenuItem("get");
file.add(get_menu);
JPanel jp1=new JPanel();
jp1.setLayout(new BorderLayout());
Border b1=BorderFactory.createEtchedBorder();
Border b2=BorderFactory.createTitledBorder(b1,"Original Code");
jp1.setBorder(b2);
add(jp1);
jp1.setBounds(0,0,d1.width-10,d1.height-90);
jp1.setBackground(Color.GRAY);
final JPanel jp2=new JPanel();
jp2.setLayout(new BorderLayout());
b2=BorderFactory.createTitledBorder(b1,"Obfuscated Code");
jp2.setBorder(b2);
add(jp2);
jp2.setBounds(d1.width,0,d1.width-10,d1.height-90);
jp2.setBackground(Color.WHITE);
f1=new Font("SansSerif",Font.PLAIN,15);
jta1=new JTextArea();
//jta1.setText("what the hell");
jta1.setFont(f1);
//jta1.setBounds(0,0,d1.width-10,d1.height-90);
jta1.setLineWrap(true);
JScrollPane jsp1=new JScrollPane(jta1);
jp1.add(jta1,BorderLayout.CENTER);
jta2=new JTextArea();
jta2.setFont(f1);
jta2.setLineWrap(true);
JScrollPane jsp2=new JScrollPane(jta2);
//jp2.add(jsp2,BorderLayout.CENTER);
/* Adding Menu Items*/
jfc1=new JFileChooser();
jfc1.setCurrentDirectory(new File("."));
open_menu.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int result=jfc1.showOpenDialog(TFrame.this);
if(result==jfc1.APPROVE_OPTION)
path=jfc1.getSelectedFile().getPath();
}
});
final DefaultMutableTreeNode root=new DefaultMutableTreeNode("program"); //general purpose node in a tree data structure
DefaultTreeModel model=new DefaultTreeModel(root);
JTree jt=new JTree(model); //jtree to provide view of the data.
//KeywordId kid=new KeywordId(list);
jp2.add(jt,BorderLayout.CENTER);
get_menu.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String textdata=jta1.getText();
Separator sep=new Separator(textdata);
ArrayList<String> list=(ArrayList<String>)sep.getProgramLines();
//we need a separate thread to use this coz otherwise the application will become very slow.
MakeTree mt=new MakeTree(list,root);
//code to be edited DefaultTreeModel model=mt.getModel();
jp2.setBackground(Color.GRAY);
//FunctionIdentification fi1=new FunctionIdentification(list);
}
});
}
public Dimension calculateSize(Dimension d)
{
return new Dimension(d.width/2,d.height);
}
}
class Separator
{
private ArrayList<String> list;
private String tosplit;
Separator(String testString)
{
tosplit=testString;
out.println(tosplit);
splitString(tosplit);
}
public ArrayList splitString(String textdata)
{
Scanner in=new Scanner(textdata);
//out.println(textdata);
in.useDelimiter(Pattern.compile("\n"));
list=new ArrayList<String>();
while(in.hasNext())
{
list.add(in.next());
}
out.println("the list "+list);
ListIterator li=list.listIterator();
//this part merely shows the separated lines in command prompt u can modify it to show it in the second window.
/*while(li.hasNext())
{
out.println(list.size());
out.println(li.next());
//li.remove();
}*/
return list;
}
public ArrayList getProgramLines()
{
return list;
}
}
class MakeTree
{
DefaultTreeModel theTreeModel;
DefaultMutableTreeNode root;
ArrayList<String> prog;
DefineNode define;
IncludeNode include;
PragmaNode pragma;
LineNode line;
ErrorNode error;
int rj;
MakeTree(ArrayList<String> aprog,DefaultMutableTreeNode root)//calls makeRoot()
{
include=new IncludeNode("include");
define=new DefineNode("Define");
pragma = new PragmaNode("pragma");
line = new LineNode("line");
error = new ErrorNode("error");
prog=aprog;
//this will only add main root node and #include Section
this.root=root;
makeRoot();
}
void makeRoot()// make root calls getInclude()
{
HashNode preprocessor=new HashNode("#");
root.add(preprocessor);
preprocess(preprocessor);
//DefaultMutableTreeNode include=new DefaultMutableTreeNode("include");
//preprocessor.add(include);
//addInInclude(include);
}
void preprocess(HashNode hash) //this will iterate through all the strings till main() is encountered
{
boolean foundmain=false;//to check if main has been found
for(rj=0;rj<prog.size();rj++)
{
String g=prog.get(rj);
if(g.charAt(0)=='#')
{
//search for the type of preprocessor node
if(g.indexOf("include")!=(-1))
{
out.println(g.indexOf("include"));
hash.add(include);
addInInclude(include,g);
}
else if(g.indexOf("define")!=(-1))
{
hash.add(define);
addInDefine(define,g);
}
else if(g.indexOf("pragma") != (-1))
{
hash.add(pragma);
addInPragma(pragma,g);
}
else if (g.indexOf("line") != (-1))
{
hash.add(line);
addInLine(line,g);
}
else if (g.indexOf("error") != (-1))
{
hash.add(error);
addInError(error,g);
}
}
else // this is where we identify for func node , structnode and mainnode
{
//first break the string into tokens and we need only first token to identify if it's a or union and rest will be used if it's not
//it could be a function declaration or definition or declaration
//finally we could have encountered main
Scanner in=new Scanner(g);
String token=in.next();
if(token.equals("struct"))
{
StructNode st=new StructNode("struct");
root.add(st);
String checkstructname=new String(in.next());
StructTree try1=new StructTree();
try1.addStruct(st,rj);//passing structnode and the current index of ArrayList
out.println("back");
}
else if(!foundmain&&!token.equals("main()"))//this was the part that was causing the problems
{
Identify idobj=new Identify();
idobj.id(rj);
}
else if(token.equals("main()")&&!foundmain)//changes made here
{
out.println("inside main");
MainNode mn=new MainNode("MainNode");//mainnode being instantiated and makemainnode being called to make the mainnode
root.add(mn);
MakeMainNode makeMain=new MakeMainNode(mn,rj);
foundmain=true;
}
}
}
/*for(int i=rj+1;i<prog.size();i++)
{
Scanner ini=new Scanner(prog.get(i));
if(ini.findInLine("main()").length()!=0)
{
MainNode mn=new MainNode("MainNode");//mainnode being instantiated and makemainnode being called to make the mainnode
root.add(mn);
MakeMainNode makeMain=new MakeMainNode(mn,i);
}
}*/
}
class MakeMainNode
{
MakeMainNode(MainNode node,int index)
{
for(int i=index;i<prog.size();i++)
{
DataNode dn=new DataNode(prog.get(i));
node.add(dn);
}
}
}
/*class FunctionNode extends AbstractNode
{
private String returnType;
private ArrayList<Parameter> param;//parameters will always be the function parameters
private boolean declared;// if it's false that means it's a predefined function
public FunctionNode(){};
public FunctionNode(Object o)
{
super(o);
}
public ArrayList getParamList()
{
return param;
}
public void setParamList(ArrayList<Parameter> par)
{
param=par;
}
public String getReturnType()
{
return returnType;
}
public void setReurnType(String ret)
{
returnType=ret;
}
public boolean getDeclared()
{
return declared;
}
public void setDecalred(boolean ans)
{
declared=ans;
}
}*/
class Parameter extends AbstractNode
{
String name;
String type;
String value;
public Parameter(){}
public Parameter(Object o){super(o);}
public String getName()
{
return name;
}
public void setName(String n)
{
name=n;
}
public void setType(String t)
{
type=t;
}
public String getType()
{
return type;
}
public String getValue()
{
return value;
}
public void setValue(String set)
{
value=set;
}
}
class Identify
{
void id(int index)
{
boolean found=false;//checks whether a func has been found
//
int equali=-1,bracei=-1;//this checks if '=' or '(' has been found , if so their index values are compared and we see what comes first telling us about //the variable
String g=prog.get(index);
out.println(g);
if(g.indexOf("=")!=-1)
{
equali=g.indexOf("=");
}
if(g.indexOf("(")!=-1)
{
bracei=g.indexOf("(");
}
if(equali!=-1||bracei!=-1)
{
found=true;
}
if(equali<bracei||bracei==-1&&equali!=-1)//variable has been found
{
Scanner in=new Scanner(g);
String temp=g;
DataNode dn=new DataNode("Global Variable");
root.add(dn);
dn.add(new DataNode(g));
}
}
}
class StructTree
{
public void addStruct(StructNode st,int index)
{
Stack myStack=new Stack();
boolean found=false;
Scanner in;
ArrayList<DataNode> bodyNode=st.getBodyNode();
int i;
for(i=index;i<4+index&&!found;i++,out.println(i+"woohoo"))//can't be more than 3 lines long ,we search for '{',put it in a stack then copy the whole of the struct statements inalistof datanodes
{
out.println(i);
in=new Scanner(prog.get(i));
while(in.hasNext())
{
String str=in.next();
if(str.equals("{"))
{
found=true;
}
else
{
DataNode dn=new DataNode(str);
st.add(dn);
}
}
}
//wat "found" signifies is that we found "{" and we will use found so that once a complete struct is found the program doesn't keep on iteraating
found=false;
out.println(i+"index");
for(int j=i-1;j<prog.size()+(i-1)&&!found;j++)
{
int d;
String str=prog.get(j);
out.println(str);
for(int k=0;k<str.length();k++)
{
if((d=str.indexOf("{"))!=-1)
{
out.println(str.substring(d));
myStack.push('{');
DataNode dn=new DataNode(str.substring(d));
st.add(dn);
bodyNode.add(dn); //adding to the body node
break;
}
else if((d=str.indexOf("}"))!=-1)
{
if(!myStack.empty())
{
if(((Character)myStack.peek())=='{')
{
myStack.pop();
out.println("flag again");
}
if(myStack.empty())
{
st.add(new DataNode(str.substring(0)));/*this will check if the closing '}' is within the statement itself*/
out.println(str.substring(0,d));
found=true;
break;
} //so that the loop ends
bodyNode.add(new DataNode(str.substring(0,d-1))); //adding to the body node{turned out to be unnecessary}
}
}
else
{
st.add(new DataNode(prog.get(j)));
bodyNode.add(new DataNode(prog.get(j)));
break;
}
}
}
}
}
void addInDefine(DefineNode define,String g)
{
int i=g.indexOf("define");
String s=g.substring(i+6);
define.add(new DataNode(s));
}
void addInInclude(IncludeNode include,String g)
{
String s=identify(g);
DataNode n1=new DataNode(s);
include.add(n1);
}
void addInPragma(PragmaNode include,String g)
{
int i = g.indexOf("pragma");
String s= g.substring(i+6);
include.add(new DataNode(s));
}
void addInLine(LineNode include, String g)
{
int i = g.indexOf("line");
String s= g.substring(i+4);
include.add(new DataNode(s));
}
void addInError(ErrorNode include, String g)
{
int i = g.indexOf("error");
String s= g.substring(i+5);
include.add(new DataNode(s));
}
String identify(String g)
{
int i;
String s=null;
i=g.indexOf("include");
s=g.substring(i+7);
out.println(s);
return s;
}
}
//the make tree class is suitable for preprocessing only coz we need more string processing in other cases which will not include '#' so preprocess function creates object of the class below //and makes the rest of the tree
class StructNode extends AbstractNode
{
private boolean type;//whether it's a new data type
private ArrayList<DataNode> bodyNode;
StructNode(Object o)
{
super(o);
bodyNode=new ArrayList<DataNode>();
}
public ArrayList getBodyNode()
{
return bodyNode;
}
}
/*class FunctionNode extends AbstractNode
{
private String returnType;
private ArrayList<Parameter> param;//parameters will always be the function parameters
private boolean declared;// if it's false that means it's a predefined function
public FunctionNode(){};
public FunctionNode(Object o)
{
super(o);
}
public ArrayList getParamList()
{
return param;
}
public void setParamList(ArrayList<Parameter> par)
{
param=par;
}
public String getReturnType()
{
return returnType;
}
public void setReurnType(String ret)
{
returnType=ret;
}
public boolean getDeclared()
{
return declared;
}
public void setDecalred(boolean ans)
{
declared=ans;
}
}*/
//this is where we start defining all the nodes
class AbstractNode extends DefaultMutableTreeNode
{
private int flag;
public AbstractNode(){}
public AbstractNode(Object o)
{
super(o);
}
public int getFlag()
{
return flag;
}
public void setFlag(int flag)
{
this.flag=flag;
}
}
class DataNode extends AbstractNode
{
private String data;
public DataNode(){}
public DataNode(Object o)
{
super(o);
}
public String getData()
{
return data;
}
public void setData(String s)
{
data=s;
}
}
class Prog extends AbstractNode
{
public Prog(){}
public Prog(Object o){super(o);}
}
class HashNode extends AbstractNode
{
public HashNode(){};
public HashNode(Object o){super(o);}
}
class IncludeNode extends AbstractNode
{
public IncludeNode(){}
public IncludeNode(Object o){super(o);}
}
/*class BodyNode extends AbstractNode
{
ArrayList<DataNode> body;
public BodyNode(Object o)
{
super(o);
}
}*/
class DefineNode extends AbstractNode
{
DefineNode(Object o)
{
super(o);
}
}
class PragmaNode extends AbstractNode
{
PragmaNode( Object o)
{
super(o);
}
}
class LineNode extends AbstractNode
{
LineNode( Object o)
{
super (o);
}
}
class ErrorNode extends AbstractNode
{
ErrorNode (Object o)
{
super (o);
}
}
class KeywordId
{
private HashSet<String> keyword;//this is a hashset which contains keywords and hopefully the constants and functions
private Iterator it1;
KeywordId(ArrayList<String> list)
{
keyword =new HashSet<String>();
addKeywords();
}
KeywordId(File f) /*auto*/
{
}
public void addKeywords()
{
try
{
Scanner in=new Scanner(new File("keyword.txt"));
while(in.hasNext())
{
//out.println("hello");
keyword.add(in.next());
}
//out.println(keyword); this merely prints out the keywords from a file in the c-language
}
catch(FileNotFoundException e)
{
out.println("file not found");
}
}
public boolean checkWhetherKeyword(String x) //this function is needed to return whether the wor being hecked is a keyword or not
{
return keyword.contains(x);
}
}
/*1. this class checks whether the function is userdefined or has been taken from a library ,example printf would be returned as false while xyz() as true . It should take the strings from the arraylist and check for functions . If a function is found it is added to hashset and then using regular expressions we see whether it is user defined or not
2. It is temporarily being called by the Innerclass TFrame$2
3. It takes the parameters arraylist of strings and a keywordId object as parameter*/
class FunctionIdentification
{
private HashSet<String> allfunctions,userdefined;
private StringBuilder str;
private KeywordId k1;
private Stack myStack;
private Character left,right,bracket,quote;
FunctionIdentification(ArrayList<String> list)
{
left=new Character('(');
right=new Character(')');
quote=new Character('"');
myStack=new Stack<Character>();
allfunctions=new HashSet<String>();
addFunctions(list);
}
/* 1. xyz() - done
2. xyz(x,y,z,x,c,k,b,,n,m,,f,d,s,s) - done
3. xyz("void(),xyz(),kzy()") - done
4. xyz("hello",wow()) - done
5. left.equals(str.charAt(i))||quote.equals(str.charAt(i)))&&test==true */
public void addFunctions(ArrayList<String> list)
{
int index=0,current=0,len;
boolean test;
char previous;
for(String g: list)
{
test=true;//this will prevent the function from testing the arguments inside string like "hello()"
str=new StringBuilder(g);
out.println(str);
len=str.length();
out.println(len+"fdhgfkghfhgflgjhs;fth");
for(int i=0;i<len;i++)
{
if(left.equals(str.charAt(i))) //this will push '(' on the stack
{
myStack.push(str.charAt(i));
out.println("push "+myStack.peek());
if(test)
{
current=i;
test=false;
}
}
else if(right.equals(str.charAt(i))||quote.equals(str.charAt(i)))
{ // xyz("hello",wow()) |(|"
if(!myStack.isEmpty())
{
if(quote.equals(myStack.peek())||right.equals(str.charAt(i)))
{
out.println("pop "+myStack.pop());
if(myStack.isEmpty())
{
allfunctions.add(str.substring(index,current));
}
}
else if(!quote.equals(myStack.peek()))
{
myStack.push(str.charAt(i));
out.println("push "+myStack.peek());
}
}
/*if(left.equals((Character)myStack.peek())||quote.equals((Character)myStack.peek()))
{
myStack.pop();
if(myStack.isEmpty())
{
}
}*/
}
}
}//9953295028 -sharon's number
out.println(allfunctions);
}
}
class MainNode extends DefaultMutableTreeNode
{
MainNode(Object o)
{
super(o);
}
}