View Javadoc
1   /*******************************************************************************
2    * Copyhacked (H) 2012-2025.
3    * This program and the accompanying materials
4    * are made available under no term at all, use it like
5    * you want, but share and discuss it
6    * every time possible with every body.
7    *
8    * Contributors:
9    *      ron190 at ymail dot com - initial implementation
10   ******************************************************************************/
11  package com.jsql.view.swing.manager;
12  
13  import com.jsql.util.I18nUtil;
14  import com.jsql.util.LogLevelUtil;
15  import com.jsql.util.bruter.ActionCoder;
16  import com.jsql.view.swing.manager.util.ActionBruteForce;
17  import com.jsql.view.swing.manager.util.JButtonStateful;
18  import com.jsql.view.swing.manager.util.ModelBrute;
19  import com.jsql.view.swing.manager.util.ModelSpinner;
20  import com.jsql.view.swing.panel.preferences.listener.SpinnerMouseWheelListener;
21  import com.jsql.view.swing.text.*;
22  import com.jsql.view.swing.util.I18nViewUtil;
23  import org.apache.commons.lang3.StringUtils;
24  import org.apache.logging.log4j.LogManager;
25  import org.apache.logging.log4j.Logger;
26  
27  import javax.swing.*;
28  import java.awt.*;
29  import java.util.Arrays;
30  import java.util.concurrent.atomic.AtomicReference;
31  
32  /**
33   * Manager to brute force a hash of various types.
34   */
35  public class ManagerBruteForce extends JPanel {
36  
37      private static final Logger LOGGER = LogManager.getRootLogger();
38  
39      public static final String BRUTEFORCE_RUN_BUTTON_TOOLTIP = "BRUTEFORCE_RUN_BUTTON_TOOLTIP";
40      public static final String BRUTEFORCE_HASH_TOOLTIP = "BRUTEFORCE_HASH_TOOLTIP";
41      public static final String BRUTEFORCE_EXCLUDE_TOOLTIP = "BRUTEFORCE_EXCLUDE_TOOLTIP";
42  
43      private JButtonStateful run;
44      private JTextField hash;
45      private JComboBox<String> hashTypes;
46      private final AtomicReference<JCheckBox> lowerCaseCharacters = new AtomicReference<>();
47      private final AtomicReference<JCheckBox> upperCaseCharacters = new AtomicReference<>();
48      private final AtomicReference<JCheckBox> numericCharacters = new AtomicReference<>();
49      private final AtomicReference<JCheckBox> specialCharacters = new AtomicReference<>();
50      private JTextField exclude;
51      private final AtomicReference<JSpinner> minimumLength = new AtomicReference<>();
52      private final AtomicReference<JSpinner> maximumLength = new AtomicReference<>();
53      private final JTextPane result;
54      
55      /**
56       * Animated GIF displayed during attack.
57       */
58      private JProgressBar progressBar;
59      private final Component horizontalGlue = Box.createHorizontalGlue();
60  
61      /**
62       * Create a panel to run brute force attack.
63       */
64      public ManagerBruteForce() {
65          super(new BorderLayout());
66  
67          JPanel panelOptions = this.initOptionsPanel();
68          this.add(panelOptions, BorderLayout.NORTH);
69  
70          var placeholder = new JTextPanePlaceholder(I18nUtil.valueByKey("BRUTEFORCE_RESULT"));
71          this.result = new JPopupTextPane(placeholder).getProxy();
72          I18nViewUtil.addComponentForKey("BRUTEFORCE_RESULT", placeholder);
73          this.result.setName("managerBruterResult");
74          this.result.setEditable(false);
75          this.add(new JScrollPane(this.result), BorderLayout.CENTER);
76  
77          JPanel panelButton = this.initPanelButton();
78          this.add(panelButton, BorderLayout.SOUTH);
79      }
80  
81      private JPanel initPanelButton() {
82          var lastLine = new JPanel();
83          lastLine.setLayout(new BoxLayout(lastLine, BoxLayout.X_AXIS));
84  
85          var tooltip = new AtomicReference<>(new JToolTipI18n(I18nUtil.valueByKey(ManagerBruteForce.BRUTEFORCE_RUN_BUTTON_TOOLTIP)));
86          this.run = new JButtonStateful("BRUTEFORCE_RUN_BUTTON_LABEL") {
87              @Override
88              public JToolTip createToolTip() {
89                  return tooltip.get();
90              }
91          };
92          I18nViewUtil.addComponentForKey("BRUTEFORCE_RUN_BUTTON_LABEL", this.run);
93          I18nViewUtil.addComponentForKey(ManagerBruteForce.BRUTEFORCE_RUN_BUTTON_TOOLTIP, tooltip.get());
94          this.run.setToolTipText(I18nUtil.valueByKey(ManagerBruteForce.BRUTEFORCE_RUN_BUTTON_TOOLTIP));
95  
96          this.run.setName("managerBruterRun");
97          this.run.addActionListener(new ActionBruteForce(this));
98  
99          this.progressBar = new JProgressBar();
100         this.progressBar.setIndeterminate(true);
101         this.progressBar.setVisible(false);
102         this.progressBar.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
103 
104         lastLine.add(this.horizontalGlue);
105         lastLine.add(this.progressBar);
106         lastLine.add(this.run);
107         return lastLine;
108     }
109 
110     public void showLoader(boolean isVisible) {
111         this.progressBar.setVisible(isVisible);
112         this.horizontalGlue.setVisible(!isVisible);
113     }
114 
115     private JPanel initOptionsPanel() {
116         var options = new JPanel(new BorderLayout());
117         JPanel firstLine = this.initFirstLine();
118         final JPanel secondLine = this.initSecondLine();
119         JPanel thirdLine = this.initThirdLine();
120         
121         final var secondAndThirdLine = new JPanel(new BorderLayout());
122         secondAndThirdLine.add(secondLine, BorderLayout.NORTH);
123         secondAndThirdLine.add(thirdLine, BorderLayout.SOUTH);
124 
125         options.add(firstLine, BorderLayout.NORTH);
126         options.add(secondAndThirdLine, BorderLayout.SOUTH);
127         return options;
128     }
129 
130     private JPanel initFirstLine() {
131         var tooltip = new AtomicReference<>(new JToolTipI18n(I18nUtil.valueByKey(ManagerBruteForce.BRUTEFORCE_HASH_TOOLTIP)));
132         var placeholder = new JTextFieldPlaceholder(I18nUtil.valueByKey("BRUTEFORCE_HASH_LABEL")) {
133             @Override
134             public JToolTip createToolTip() {
135                 return tooltip.get();
136             }
137         };
138         this.hash = new JPopupTextField(placeholder).getProxy();
139         I18nViewUtil.addComponentForKey(ManagerBruteForce.BRUTEFORCE_HASH_TOOLTIP, tooltip.get());
140         I18nViewUtil.addComponentForKey("BRUTEFORCE_HASH_LABEL", this.hash);
141         this.hash.setName("managerBruterHash");
142         this.hash.setToolTipText(I18nUtil.valueByKey(ManagerBruteForce.BRUTEFORCE_HASH_TOOLTIP));
143 
144         var firstLine = new JPanel(new BorderLayout());
145         firstLine.add(this.hash, BorderLayout.CENTER);
146         return firstLine;
147     }
148 
149     private JPanel initSecondLine() {
150         final var secondLine = new JPanel();
151         secondLine.setLayout(new BoxLayout(secondLine, BoxLayout.X_AXIS));
152 
153         this.hashTypes = new JComboBox<>(ActionCoder.getHashes().toArray(String[]::new));
154         this.hashTypes.setSelectedIndex(6);
155         this.hashTypes.setToolTipText(I18nUtil.valueByKey("BRUTEFORCE_HASH_TYPE_TOOLTIP"));
156         I18nViewUtil.addComponentForKey("BRUTEFORCE_HASH_TYPE_TOOLTIP", this.hashTypes);
157         secondLine.add(this.hashTypes);
158 
159         Arrays.asList(
160             new ModelBrute(this.lowerCaseCharacters, "a-z", "BRUTEFORCE_LCASE_TOOLTIP"),
161             new ModelBrute(this.upperCaseCharacters, "A-Z", "BRUTEFORCE_UCASE_TOOLTIP"),
162             new ModelBrute(this.numericCharacters, "0-9", "BRUTEFORCE_NUM_TOOLTIP"),
163             new ModelBrute(this.specialCharacters, "Special", "BRUTEFORCE_SPEC_TOOLTIP")
164         ).forEach(modelBrute -> {
165             var tooltip = new AtomicReference<>(new JToolTipI18n(I18nUtil.valueByKey(modelBrute.i18nTooltip())));
166             var checkbox = new JCheckBox(modelBrute.text(), true) {
167                 @Override
168                 public JToolTip createToolTip() {
169                     return tooltip.get();
170                 }
171             };
172             checkbox.setName(modelBrute.text());
173             checkbox.setToolTipText(I18nUtil.valueByKey(modelBrute.i18nTooltip()));
174             I18nViewUtil.addComponentForKey(modelBrute.i18nTooltip(), tooltip.get());
175             secondLine.add(Box.createHorizontalStrut(5));
176             secondLine.add(checkbox);
177             modelBrute.checkbox().set(checkbox);
178         });
179 
180         return secondLine;
181     }
182 
183     private JPanel initThirdLine() {
184         var thirdLine = new JPanel();
185         thirdLine.setLayout(new BoxLayout(thirdLine, BoxLayout.X_AXIS));
186 
187         final var tooltip = new AtomicReference<>(new JToolTipI18n(I18nUtil.valueByKey(ManagerBruteForce.BRUTEFORCE_EXCLUDE_TOOLTIP)));
188         var placeholderTooltip = new JTextFieldPlaceholder(I18nUtil.valueByKey("BRUTEFORCE_EXCLUDE_LABEL")) {
189             @Override
190             public JToolTip createToolTip() {
191                 return tooltip.get();
192             }
193         };
194         this.exclude = new JPopupTextField(placeholderTooltip).getProxy();
195         this.exclude.setToolTipText(I18nUtil.valueByKey(ManagerBruteForce.BRUTEFORCE_EXCLUDE_TOOLTIP));
196         I18nViewUtil.addComponentForKey("BRUTEFORCE_EXCLUDE_LABEL", this.exclude);
197         I18nViewUtil.addComponentForKey(ManagerBruteForce.BRUTEFORCE_EXCLUDE_TOOLTIP, tooltip.get());
198         thirdLine.add(this.exclude);
199 
200         Arrays.asList(
201             new ModelSpinner(1, this.minimumLength, "BRUTEFORCE_MIN_LABEL", "BRUTEFORCE_MIN_TOOLTIP"),
202             new ModelSpinner(5, this.maximumLength, "BRUTEFORCE_MAX_LABEL", "BRUTEFORCE_MAX_TOOLTIP")
203         ).forEach(model -> SwingUtilities.invokeLater(() -> {  // #96099: JSpinner not fully initialized
204             final var tooltipMax = new AtomicReference<>(new JToolTipI18n(I18nUtil.valueByKey(model.i18n())));
205             JSpinner spinner;
206             try {  // Fixes #96099 #96407: NullPointerException on new JSpinner
207                 spinner = new JSpinner() {
208                     @Override
209                     public JToolTip createToolTip() {
210                         return tooltipMax.get();
211                     }
212                 };
213             } catch (NullPointerException e) {
214                 LOGGER.log(LogLevelUtil.CONSOLE_JAVA, "Spinner creation failed, restart app or check your jre", e);
215                 return;
216             }
217             spinner.setModel(new SpinnerNumberModel(model.value(), 1, 10000, 1));
218             spinner.addMouseWheelListener(new SpinnerMouseWheelListener());
219             spinner.setToolTipText(I18nUtil.valueByKey(model.i18n()));
220             I18nViewUtil.addComponentForKey(model.i18n(), tooltipMax.get());
221             spinner.setPreferredSize(new Dimension(
222                 (int) (spinner.getPreferredSize().width/1.8),
223                 spinner.getPreferredSize().height
224             ));
225 
226             var label = new JLabel(StringUtils.SPACE + I18nUtil.valueByKey(model.label()), SwingConstants.RIGHT);
227             label.setMaximumSize(new Dimension(label.getPreferredSize().width, label.getPreferredSize().height));
228             thirdLine.add(Box.createHorizontalStrut(5));
229             thirdLine.add(label);
230             I18nViewUtil.addComponentForKey(model.label(), label);
231 
232             thirdLine.add(spinner);
233             model.spinner().set(spinner);
234         }));
235 
236         return thirdLine;
237     }
238 
239     
240     // Getter and setter
241 
242     public JButtonStateful getRun() {
243         return this.run;
244     }
245 
246     public JTextField getHash() {
247         return this.hash;
248     }
249 
250     public JComboBox<String> getHashTypes() {
251         return this.hashTypes;
252     }
253 
254     public JCheckBox getLowerCaseCharacters() {
255         return this.lowerCaseCharacters.get();
256     }
257 
258     public JCheckBox getUpperCaseCharacters() {
259         return this.upperCaseCharacters.get();
260     }
261 
262     public JCheckBox getNumericCharacters() {
263         return this.numericCharacters.get();
264     }
265 
266     public JCheckBox getSpecialCharacters() {
267         return this.specialCharacters.get();
268     }
269 
270     public JTextField getExclude() {
271         return this.exclude;
272     }
273 
274     public JSpinner getMinimumLength() {
275         return this.minimumLength.get();
276     }
277 
278     public JSpinner getMaximumLength() {
279         return this.maximumLength.get();
280     }
281 
282     public JTextPane getResult() {
283         return this.result;
284     }
285 }