View Javadoc
1   package com.jsql.view.swing.panel.preferences;
2   
3   import com.jsql.util.LogLevelUtil;
4   import com.jsql.util.StringUtil;
5   import com.jsql.view.swing.text.SyntaxTextArea;
6   import com.jsql.view.swing.text.listener.DocumentListenerEditing;
7   import com.jsql.view.swing.util.MediatorHelper;
8   import com.jsql.view.swing.util.UiUtil;
9   import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
10  import org.fife.ui.rtextarea.RTextScrollPane;
11  import org.yaml.snakeyaml.parser.ParserException;
12  import org.yaml.snakeyaml.scanner.ScannerException;
13  
14  import javax.swing.*;
15  
16  public class PanelExploit extends JPanel {
17  
18      private static final SyntaxTextArea TEXTAREA_REVSHELLS = new SyntaxTextArea("Reverse shell list");
19      private static final String TITLE = "Reverse shell server (connector)";
20      private static final String PATTERN = "<html><b>%s</b></html>";
21  
22      public PanelExploit() {
23          PanelExploit.TEXTAREA_REVSHELLS.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_YAML);
24          PanelExploit.applyTheme();
25  
26          var preferencesUtil = MediatorHelper.model().getMediatorUtils().getPreferencesUtil();
27          var labelOrigin = new JLabel(String.format(PanelExploit.PATTERN, PanelExploit.TITLE));
28          PanelExploit.TEXTAREA_REVSHELLS.getDocument().addDocumentListener(new DocumentListenerEditing() {
29              @Override
30              public void process() {
31                  try {
32                      preferencesUtil.parseReverseCommands(PanelExploit.TEXTAREA_REVSHELLS.getText());
33                      labelOrigin.setText(String.format(PanelExploit.PATTERN, PanelExploit.TITLE));
34                  } catch (
35                      ParserException | ScannerException  // yml
36                      | NullPointerException  // map missing key
37                      | ClassCastException  // incorrect model
38                      e
39                  ) {
40                      labelOrigin.setText(String.format(PanelExploit.PATTERN, StringUtil.formatReport(
41                          LogLevelUtil.COLOR_RED,
42                          PanelExploit.TITLE
43                          + ", YAML must follow the name and command template :<br>"
44                          // Fix #96073: NullPointerException on e.getMessage().replace()
45                          + e.toString().replace("\n", "<br>")  // toString to prevent NPE (jdk11)
46                      )));
47                  }
48              }
49          });
50          PanelExploit.TEXTAREA_REVSHELLS.setText(
51              preferencesUtil.getCommandsReverseYaml().replace("\r\n", "\n")  // required to prevent \ra\n issue
52          );
53          PanelExploit.TEXTAREA_REVSHELLS.setCaretPosition(0);
54  
55          var scrollPane = new RTextScrollPane(PanelExploit.TEXTAREA_REVSHELLS);
56  
57          labelOrigin.setBorder(PanelGeneral.MARGIN);
58  
59          var groupLayout = new GroupLayout(this);
60          this.setLayout(groupLayout);
61  
62          groupLayout.setHorizontalGroup(
63              groupLayout
64              .createSequentialGroup()
65              .addGroup(
66                  groupLayout
67                  .createParallelGroup(GroupLayout.Alignment.LEADING)
68                  .addComponent(labelOrigin)
69                  .addComponent(scrollPane)
70              )
71          );
72  
73          groupLayout.setVerticalGroup(
74              groupLayout
75              .createSequentialGroup()
76              .addGroup(
77                  groupLayout
78                  .createParallelGroup(GroupLayout.Alignment.BASELINE)
79                  .addComponent(labelOrigin)
80              )
81              .addGroup(
82                  groupLayout
83                  .createParallelGroup(GroupLayout.Alignment.BASELINE)
84                  .addComponent(scrollPane)
85              )
86          );
87      }
88  
89      public static void applyTheme() {
90          UiUtil.applySyntaxTheme(PanelExploit.TEXTAREA_REVSHELLS);
91      }
92  }