View Javadoc
1   package com.jsql.util.bruter;
2   
3   import com.jsql.util.I18nUtil;
4   import com.jsql.util.LogLevelUtil;
5   import org.apache.logging.log4j.LogManager;
6   import org.apache.logging.log4j.Logger;
7   
8   import java.util.ArrayList;
9   import java.util.List;
10  
11  public class Bruter {
12      
13      private static final Logger LOGGER = LogManager.getRootLogger();
14      public static final String PATTERN_PERIOD = "%s: %s %s %s %s %s %s %s %s";
15  
16      protected final List<String> characters = new ArrayList<>();
17       
18      protected boolean found = false;
19       
20      protected int maxLength;
21      protected int minLength;
22       
23      protected int count;
24       
25      protected long starttime;
26      protected long endtime;
27       
28      private static final char[] specialCharacters = {
29          '~', '`', '!', '@', '#', '$', '%', '^',
30          '&', '*', '(', ')', '_', '-', '+', '=', '{', '}', '[', ']', '|', '\\',
31          ';', ':', '\'', '"', '<', '.', ',', '>', '/', '?', ' '
32      };
33       
34      protected boolean done = false;
35  
36      public long getRemainder() {
37          return this.getNumberOfPossibilities() - this.count;
38      }
39  
40      public long getNumberOfPossibilities() {
41          long possibilities = 0;
42          for (int i = this.minLength ; i <= this.maxLength ; i++) {
43              possibilities += (long) Math.pow(this.characters.size(), i);
44          }
45          return possibilities;
46      }
47  
48      public void addLowerCaseLetters() {
49          for (var c = 'a' ; c <= 'z' ; c++) {
50              this.characters.add(String.valueOf(c));
51          }
52      }
53  
54      public void addDigits() {
55          for (var c = 0 ; c <= 9 ; c++) {
56              this.characters.add(String.valueOf(c));
57          }
58      }
59  
60      public void addUpperCaseLetters() {
61          for (var c = 'A' ; c <= 'Z' ; c++) {
62              this.characters.add(String.valueOf(c));
63          }
64      }
65  
66      public void addSpecialCharacters() {
67          for (char c: Bruter.specialCharacters) {
68              this.characters.add(String.valueOf(c));
69          }
70      }
71  
72      public void excludeChars(String s) {
73          char[] arrayChars = s.toCharArray();
74          for (char arrayChar: arrayChars) {
75              this.characters.remove(Character.toString(arrayChar));
76          }
77      }
78  
79      public int getPerSecond() {
80          int i;
81          try {
82              i = (int) (this.count / this.calculateTimeDifference());
83          } catch (Exception e) {
84              LOGGER.log(LogLevelUtil.IGNORE, e);
85              return 0;  // Ignore division by zero
86          }
87          return i;
88      }
89  
90      public String calculateTimeElapsed() {
91          long timeTaken = this.calculateTimeDifference();
92          int seconds = (int) timeTaken;
93          
94          var minutes = seconds / 60;
95          seconds = seconds % 60;
96          var hours = minutes / 60;
97          minutes = minutes % 60;
98          var days = hours / 24;
99          hours = hours % 24;
100 
101         return String.format(
102             Bruter.PATTERN_PERIOD,
103             "Time elapsed",
104             days, I18nUtil.valueByKey("BRUTEFORCE_DAYS"),
105             hours, I18nUtil.valueByKey("BRUTEFORCE_HOURS"),
106             minutes, I18nUtil.valueByKey("BRUTEFORCE_MINUTES"),
107             seconds, I18nUtil.valueByKey("BRUTEFORCE_SECONDS")
108         );
109     }
110 
111     private long calculateTimeDifference() {
112         return (long) ((this.endtime - this.starttime) * Math.pow(10, -9));
113     }
114     
115     
116     // Getter and setter
117 
118     public synchronized void setEndtime(long endtime) {
119         this.endtime = endtime;
120     }
121 
122     public void setMaxLength(int maxLength) {
123         this.maxLength = maxLength;
124     }
125 
126     public void setMinLength(int minLength) {
127         this.minLength = minLength;
128     }
129 
130     public boolean isFound() {
131         return this.found;
132     }
133 
134     public void setFound(boolean found) {
135         this.found = found;
136     }
137 
138     public int getCounter() {
139         return this.count;
140     }
141 
142     public void setIsDone(Boolean done) {
143         this.done = done;
144     }
145 
146     public boolean isDone() {
147         return this.done;
148     }
149 }