package exploitpack;

import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;

/**
 *
 * @author jsacco
 */
public class ClientsideWizard extends javax.swing.JFrame {

    XMLTreenode NewXMLNode = new XMLTreenode();
    private int TotalModulesLength;
    private Object LastTreeItemSelected;
    Exploit exploit = new Exploit();

    /**
     * Creates new form ratWizard
     */
    public ClientsideWizard() {
        initComponents();

        int lebar = this.getWidth() / 2;
        int tinggi = this.getHeight() / 2;
        int x = (Toolkit.getDefaultToolkit().getScreenSize().width / 2) - lebar;
        int y = (Toolkit.getDefaultToolkit().getScreenSize().height / 2) - tinggi;
        this.setLocation(x, y);
        this.setVisible(true);
        setDefaultCloseOperation(ExploitWizard.DISPOSE_ON_CLOSE);
        getContentPane().setBackground(Color.WHITE);
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/exploitpack/resources/bug.png")));
        DefaultTreeCellRenderer rendererExploits = new DefaultTreeCellRenderer();
        rendererExploits.setClosedIcon(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/exploitpack/resources/foldernormal.png"))));
        rendererExploits.setOpenIcon(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/exploitpack/resources/foldernormal2.png"))));
        rendererExploits.setLeafIcon(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/exploitpack/resources/scriptnormal.png"))));
        jTreeExploits.setCellRenderer(rendererExploits);
        DefaultTreeModel model = (DefaultTreeModel) jTreeExploits.getModel();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getChild(model.getRoot(), 0);
        root.add(new DefaultMutableTreeNode("Navigate your exploit Packs.."));
        model.reload(root);
        jTreeExploits.expandRow(0);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField3 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldSearch = new javax.swing.JTextField();
        jButtonSearch = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTreeExploits = new javax.swing.JTree();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaQuickInfo = new javax.swing.JTextArea();
        jSeparator2 = new javax.swing.JSeparator();

        jTextField3.setText("jTextField1");

        jTextField5.setText("jTextField1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Exploit Pack - Exploit Wizard");
        setResizable(false);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/exploitpack/resources/up2.png"))); // NOI18N

        jButton1.setText("Cancel");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Next");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Back");
        jButton3.setEnabled(false);

        jLabel2.setText("<html>This module wizard helps you create custom clientside exploits. The created files <br>could also include custom shellcodes or be modified later on, you will find the <br>modules under the output folder in your installation folder.</html>");

        jLabel3.setText("Search exploit:");

        jTextFieldSearch.setText("Exploit name, author, platform, program, etc..");
        jTextFieldSearch.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextFieldSearchMouseClicked(evt);
            }
        });
        jTextFieldSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldSearchActionPerformed(evt);
            }
        });
        jTextFieldSearch.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldSearchKeyPressed(evt);
            }
        });

        jButtonSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/exploitpack/resources/arrow-right-16.png"))); // NOI18N
        jButtonSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchActionPerformed(evt);
            }
        });

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("JTree");
        javax.swing.tree.DefaultMutableTreeNode treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Search");
        treeNode1.add(treeNode2);
        jTreeExploits.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jTreeExploits.setRootVisible(false);
        jTreeExploits.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTreeExploitsMouseClicked(evt);
            }
        });
        jTreeExploits.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jTreeExploitsValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jTreeExploits);

        jTextAreaQuickInfo.setBackground(java.awt.Color.black);
        jTextAreaQuickInfo.setColumns(20);
        jTextAreaQuickInfo.setForeground(java.awt.Color.white);
        jTextAreaQuickInfo.setRows(5);
        jTextAreaQuickInfo.setText("Nothing selected yet..");
        jTextAreaQuickInfo.setToolTipText("");
        jScrollPane2.setViewportView(jTextAreaQuickInfo);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator2)
                    .addComponent(jSeparator1)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(jButton3)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jButton2)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jButton1))
                                .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextFieldSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 394, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonSearch))
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 527, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 2, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 7, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(jTextFieldSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 446, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addContainerGap())
        );

        jLabel2.getAccessibleContext().setAccessibleName("<html>This shell wizard helps you create your  personalized agent to use with Exploit Pack.  <br>The created script, if needed, could include also a custom shellcode<br> and converted into an executable file.</html>");

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        ClientsideWizard2.main(exploit);
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jTextFieldSearchMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextFieldSearchMouseClicked
        // TODO add your handling code here:
        jTextFieldSearch.setText("");
    }//GEN-LAST:event_jTextFieldSearchMouseClicked

    private void jTextFieldSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldSearchActionPerformed
        // TODO add your handling code here:
        jTextFieldSearch.setText("");
    }//GEN-LAST:event_jTextFieldSearchActionPerformed

    private void jTextFieldSearchKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldSearchKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyChar() == KeyEvent.VK_ENTER) {
            jTextAreaQuickInfo.setText("");
            jButtonSearch.doClick();
            jButtonSearch.doClick();
        }
    }//GEN-LAST:event_jTextFieldSearchKeyPressed

    private void jButtonSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSearchActionPerformed
        // TODO add your handling code here:
        String path = "exploits/";
        String files;
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();
        jTextAreaQuickInfo.setText("Search executed successfully.." + "\n");

        DefaultTreeCellRenderer rendererExploits = new DefaultTreeCellRenderer();
        rendererExploits.setClosedIcon(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/exploitpack/resources/foldernormal.png"))));
        rendererExploits.setOpenIcon(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/exploitpack/resources/foldernormal2.png"))));
        rendererExploits.setLeafIcon(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/exploitpack/resources/scriptnormal.png"))));
        jTreeExploits.setCellRenderer(rendererExploits);

        DefaultTreeModel model = (DefaultTreeModel) jTreeExploits.getModel();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getChild(model.getRoot(), 0);
        root.removeAllChildren();
        boolean notfound = true;
        int searchtotal = 0;
        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                files = listOfFiles[i].getName();
                if (files.toLowerCase().contains(jTextFieldSearch.getText().toLowerCase())) {
                    // Instance of XMLTreenode
                    NewXMLNode = new XMLTreenode();
                    // Start of the class main of XMLTreenode
                    NewXMLNode.main(null, files);
                    DefaultMutableTreeNode item = new DefaultMutableTreeNode(NewXMLNode.ExploitName);
                    root.add(item);
                    model.reload(root);
                    searchtotal++;
                    // Obtain total modules
                    TotalModulesLength = i;
                    notfound = false;
                }
            }
        }
        if (notfound) {
            jTextAreaQuickInfo.append("Sorry, module not found.." + "\n");
        }
        jTreeExploits.expandRow(0);
        jTextAreaQuickInfo.append("Found: "
                + searchtotal + " exploits for this criteria."
                + "\n");
        jTextAreaQuickInfo.append("Total exploits on your pack: "
                + TotalModulesLength + ""
                + "\n");
        EPLogger.log("Search executed successfully: OK");
        EPLogger.log("Total of modules for this instance: " + TotalModulesLength);
    }//GEN-LAST:event_jButtonSearchActionPerformed

    private void jTreeExploitsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTreeExploitsMouseClicked
        // TODO add your handling code here:
        jTextAreaQuickInfo.append(NewXMLNode.ExploitName + " - Selected" + "\n");
    }//GEN-LAST:event_jTreeExploitsMouseClicked

    private void jTreeExploitsValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTreeExploitsValueChanged

        String ExploitSelection = evt.getPath().getPath()[2].toString();

        // Check if the selected item is part of these categories
        if (ExploitSelection.equals("Exploits")
                || ExploitSelection.equals("Windows")
                || ExploitSelection.equals("BSD")
                || ExploitSelection.equals("Unix")
                || ExploitSelection.equals("Linux")
                || ExploitSelection.equals("Tools")
                || ExploitSelection.equals("Search")
                || ExploitSelection.equals("History")
                || ExploitSelection.equals("Personal")
                || ExploitSelection.startsWith("RAT")
                || Character.isDigit(ExploitSelection.charAt(0))
                || ExploitSelection.equals("Data")) {
        } else {
            if (LastTreeItemSelected != null) {
                DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
                renderer.setClosedIcon(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/exploitpack/resources/foldernormal.png"))));
                renderer.setOpenIcon(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/exploitpack/resources/folderselect.png"))));
                renderer.setLeafIcon(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/exploitpack/resources/scriptselect.png"))));
                //selectedElement.setUserObject(renderer);
            }
            DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
            renderer.setClosedIcon(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/exploitpack/resources/foldernormal.png"))));
            renderer.setOpenIcon(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/exploitpack/resources/folderselect.png"))));
            renderer.setLeafIcon(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/exploitpack/resources/scriptnormal.png"))));
            //selectedElement.setUserObject(renderer);

            //jTreeExploits.setCellRenderer(renderer);
            LastTreeItemSelected = ExploitSelection;

            // QuickInformation
            ExploitSelection = ExploitSelection.concat(".xml");

            // Clean QuickInformation
            jTextAreaQuickInfo.setText("");

            // Start de la clase main de XMLTreenode
            NewXMLNode.main(null, ExploitSelection);
            jTextAreaQuickInfo.append("Exploit Name: "
                    + NewXMLNode.ExploitName + "\n");
            jTextAreaQuickInfo.append("Author of this module: "
                    + NewXMLNode.Author + "\n");
            jTextAreaQuickInfo.append("Type of exploit: "
                    + NewXMLNode.ExploitType + "\n");
            jTextAreaQuickInfo.append("CVE ( Mitre ID ): "
                    + NewXMLNode.Vulnerability + "\n");
            jTextAreaQuickInfo.append("Disclosure Date: "
                    + NewXMLNode.Date + "\n");
            jTextAreaQuickInfo.append("Platform: "
                    + NewXMLNode.Platform);
            jTextAreaQuickInfo.append(NewXMLNode.Information);
            jTextAreaQuickInfo.setCaretPosition(0);

            // Set new instance of exploit
            exploit.setModuleName(NewXMLNode.ExploitName);
            exploit.setCodeName(NewXMLNode.CodeName);
            exploit.setType(NewXMLNode.ExploitType);
            exploit.setPlatform(NewXMLNode.Platform);
            exploit.setService(NewXMLNode.Service);
            exploit.setShellcodePort(NewXMLNode.ShellPort);
            exploit.setRemotePort(NewXMLNode.RemotePort);
            exploit.setShellcodeAvail(NewXMLNode.ShellcodeAvailable);
            exploit.setInformation(NewXMLNode.Information);
            exploit.setSpecialArgs(NewXMLNode.SpecialArgs);

        }
    }//GEN-LAST:event_jTreeExploitsValueChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ClientsideWizard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ClientsideWizard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ClientsideWizard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ClientsideWizard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ClientsideWizard().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButtonSearch;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextArea jTextAreaQuickInfo;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextFieldSearch;
    private javax.swing.JTree jTreeExploits;
    // End of variables declaration//GEN-END:variables
}
