Java tutorial
/** * WS-Attacker - A Modular Web Services Penetration Testing Framework Copyright * (C) 2013 Christian Altmeier * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation; either version 2 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 51 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ package wsattacker.plugin.intelligentdos.ui.dialog; import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import java.text.DecimalFormat; import java.util.HashSet; import java.util.List; import java.util.Set; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeModel; import javax.swing.tree.TreeSelectionModel; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.apache.log4j.Logger; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import wsattacker.library.intelligentdos.common.SuccessfulAttack; import wsattacker.library.intelligentdos.position.Position; import wsattacker.plugin.intelligentdos.model.ResultModel; import wsattacker.plugin.intelligentdos.ui.controller.ResultButtonController; import wsattacker.plugin.intelligentdos.ui.helper.ChartHelper; import wsattacker.plugin.intelligentdos.ui.helper.FormatHelper; import wsattacker.plugin.intelligentdos.ui.renderer.EfficiencyRenderer; /** * @author Christian Altmeier */ public class Result_NB extends JFrame { private static final String FILE_EXT_ZIP = "zip"; private final Logger logger = Logger.getLogger(getClass()); private static final DecimalFormat df = new DecimalFormat(".00"); private final JFreeChart chart; private transient final ResultButtonController btnController; private transient final List<SuccessfulAttack> attacks; /** * Creates new form Result_NB * * @param title * @param attacks */ public Result_NB(String title, ResultModel resultModel) { super(title); this.attacks = resultModel.getAttacks(); btnController = new ResultButtonController(resultModel); chart = ChartHelper.createDumyChart(); initComponents(); resultTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); } /** * 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" // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); resultTree = new javax.swing.JTree(); attackMetadataPanel = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); attackSuccessMetricPanel = new javax.swing.JPanel(); successMetric = new javax.swing.JLabel(); chartPanel1 = new org.jfree.chart.ChartPanel(chart); chartPanel2 = new org.jfree.chart.ChartPanel(chart); saveBtn = new javax.swing.JButton(); jPanel1 = new javax.swing.JPanel(); jScrollPane2 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); resultTree.setBorder(javax.swing.BorderFactory.createTitledBorder("Successful attacks")); resultTree.setModel(createModel()); resultTree.setCellRenderer(new EfficiencyRenderer()); resultTree.setRootVisible(false); resultTree.setShowsRootHandles(true); resultTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() { @Override public void valueChanged(javax.swing.event.TreeSelectionEvent evt) { resultTreeValueChanged(evt); } }); jScrollPane1.setViewportView(resultTree); attackMetadataPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Attack metadata")); jLabel1.setVerticalAlignment(javax.swing.SwingConstants.TOP); javax.swing.GroupLayout attackMetadataPanelLayout = new javax.swing.GroupLayout(attackMetadataPanel); attackMetadataPanel.setLayout(attackMetadataPanelLayout); attackMetadataPanelLayout.setHorizontalGroup( attackMetadataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)); attackMetadataPanelLayout.setVerticalGroup( attackMetadataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)); attackSuccessMetricPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Attack success metric")); successMetric.setVerticalAlignment(javax.swing.SwingConstants.TOP); javax.swing.GroupLayout attackSuccessMetricPanelLayout = new javax.swing.GroupLayout( attackSuccessMetricPanel); attackSuccessMetricPanel.setLayout(attackSuccessMetricPanelLayout); attackSuccessMetricPanelLayout.setHorizontalGroup( attackSuccessMetricPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(successMetric, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)); attackSuccessMetricPanelLayout.setVerticalGroup( attackSuccessMetricPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(successMetric, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)); javax.swing.GroupLayout chartPanel1Layout = new javax.swing.GroupLayout(chartPanel1); chartPanel1.setLayout(chartPanel1Layout); chartPanel1Layout.setHorizontalGroup(chartPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 408, Short.MAX_VALUE)); chartPanel1Layout.setVerticalGroup(chartPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 206, Short.MAX_VALUE)); chartPanel1.setPreferredSize(new java.awt.Dimension(412, 200)); javax.swing.GroupLayout chartPanel2Layout = new javax.swing.GroupLayout(chartPanel2); chartPanel2.setLayout(chartPanel2Layout); chartPanel2Layout.setHorizontalGroup(chartPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE)); chartPanel2Layout.setVerticalGroup(chartPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 164, Short.MAX_VALUE)); chartPanel2.setPreferredSize(new java.awt.Dimension(412, 200)); saveBtn.setText("Save Results"); saveBtn.addActionListener(new java.awt.event.ActionListener() { @Override public void actionPerformed(java.awt.event.ActionEvent evt) { saveBtnActionPerformed(evt); } }); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("XML with placeholder")); jTextArea1.setColumns(20); jTextArea1.setRows(5); jScrollPane2.setViewportView(jTextArea1); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane2)); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE).addComponent( jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))); 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(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup().addGap(8, 8, 8).addComponent(saveBtn))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(attackMetadataPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(attackSuccessMetricPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(chartPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(chartPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(saveBtn)) .addGroup(layout.createSequentialGroup() .addComponent(chartPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent( chartPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(attackMetadataPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(3, 3, 3) .addComponent(attackSuccessMetricPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_saveBtnActionPerformed try { JFileChooser fileChooser = new JFileChooser(); int checker = fileChooser.showOpenDialog(null); if (checker == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); String message = "The selected file already exists.\nOverwrite?"; if (!selectedFile.exists() || JOptionPane.showConfirmDialog(this, message, "Save result", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) { if (!FilenameUtils.getExtension(selectedFile.getName()).equalsIgnoreCase(FILE_EXT_ZIP)) { selectedFile = new File(selectedFile.getAbsoluteFile() + "." + FILE_EXT_ZIP); } btnController.save(selectedFile); // default title and icon JOptionPane.showMessageDialog(this, "Result has been saved to filesystem."); } } } catch (IOException e) { JOptionPane.showMessageDialog(this, e.getMessage(), "Fehler beim Schreiben", JOptionPane.ERROR_MESSAGE); logger.warn(e, e); } catch (Exception e) { e.printStackTrace(); } }// GEN-LAST:event_saveBtnActionPerformed private void resultTreeValueChanged(javax.swing.event.TreeSelectionEvent evt) {// GEN-FIRST:event_jTree1ValueChanged JTree tree = (JTree) evt.getSource(); DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if (selectedNode != null && selectedNode.isLeaf()) { SuccessfulAttack sa = (SuccessfulAttack) selectedNode.getUserObject(); jLabel1.setText(FormatHelper.toHTML(sa)); StringBuilder builder = new StringBuilder(); builder.append("<html>"); String format = "-"; if (!Double.isNaN(sa.getRatio())) { format = df.format(sa.getRatio()); } builder.append("Attack roundtrip time ratio: ").append(format); builder.append("<br />"); builder.append("the payload is ").append(sa.getEfficiency().name()); builder.append("<br />"); builder.append("<br />"); double reuqestsPerSecond = ((int) (sa.getParamItem().getReuqestsPerSecond() * 100.0)) / 100.0; builder.append(reuqestsPerSecond).append(" requests per second were sent"); builder.append("<br />"); if (sa.getTamperedContent() != null) { long l = (long) (sa.getTamperedContent().getBytes(Charset.forName("UTF-8")).length * sa.getParamItem().getReuqestsPerSecond()); builder.append("the traffic was (approx) ").append(FileUtils.byteCountToDisplaySize(l)) .append(" per second"); } builder.append("</html>"); successMetric.setText(builder.toString()); jTextArea1.setText(sa.getXmlWithPlaceholder()); JFreeChart createOverlaidChart = ChartHelper.createOverlaidChart(sa); JFreeChart createWhiskerChart = ChartHelper.createWhiskerChart(sa); ((ChartPanel) chartPanel1).setChart(createOverlaidChart); ((ChartPanel) chartPanel2).setChart(createWhiskerChart); } else { jLabel1.setText(""); successMetric.setText(""); } }// GEN-LAST:event_jTree1ValueChanged // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel attackMetadataPanel; private javax.swing.JPanel attackSuccessMetricPanel; private javax.swing.JPanel chartPanel1; private javax.swing.JPanel chartPanel2; private javax.swing.JLabel jLabel1; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JTextArea jTextArea1; private javax.swing.JTree resultTree; private javax.swing.JButton saveBtn; private javax.swing.JLabel successMetric; // End of variables declaration//GEN-END:variables private TreeModel createModel() { DefaultMutableTreeNode root = new DefaultMutableTreeNode("root"); Set<String> dosAttackNameSet = new HashSet<String>(); Set<Position> elementSet = new HashSet<Position>(); for (SuccessfulAttack sa : attacks) { dosAttackNameSet.add(sa.getDoSAttack().getName()); elementSet.add(sa.getPosition()); } for (String dosAttackName : dosAttackNameSet) { DefaultMutableTreeNode dn = new DefaultMutableTreeNode(dosAttackName); root.add(dn); // for (Position position : elementSet) { DefaultMutableTreeNode el = new DefaultMutableTreeNode(position); boolean some = false; for (SuccessfulAttack sa : attacks) { if (sa.getDoSAttack().getName().equals(dosAttackName) && sa.getPosition().equals(position)) { DefaultMutableTreeNode attackNode = new DefaultMutableTreeNode(sa); el.add(attackNode); some = true; } } if (some) { dn.add(el); } } } return new DefaultTreeModel(root); } private void writeObject(java.io.ObjectOutputStream stream) throws java.io.IOException { throw new java.io.NotSerializableException(getClass().getName()); } private void readObject(java.io.ObjectInputStream stream) throws java.io.IOException, ClassNotFoundException { throw new java.io.NotSerializableException(getClass().getName()); } }