import java.text.*; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.JOptionPane; public class DetectorFrame extends Frame{ public static Frame frame; public static TextArea text; public static Panel panel; public static Button start_button; public static Button stop_button; //public static FuzzyController fc; //public static double res; public static boolean loop = true; public static DataFetcher df; public static IfTrafficData itd; public static int n = 0; public static void main(String args[]) throws Exception{ initFrame(); df = new DataFetcher(1000L); df.fetch(); df.getSNMPData(); FuzzyController fc = new FuzzyController(); showFrame(); while(true) { while(loop) { start_button.setEnabled(false); itd = df.fetch(); if(itd!=null) { double res = fc.getOutput(itd.getRxpkt(), (itd.getRxoct()/itd.getRxpkt())); text.append(res+"\r\n"); recognizeAttack(res); } try { Thread.sleep(1000); } catch (Exception e) { } } } } public static void initFrame() { String title = "DDoS Detector Demo"; frame = new Frame(title); // Create a component to add to the frame text = new TextArea(); text.setFont(new Font(null, Font.BOLD, 12)); //text.setEditable(false); panel = new Panel(); start_button = new Button("Start"); start_button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { df.clearTempBuffer(); loop = true; //startProcess(); } }); stop_button = new Button("Pause"); stop_button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { loop = false; start_button.setEnabled(true); } }); panel.add(start_button, BorderLayout.WEST); panel.add(stop_button, BorderLayout.EAST); frame.add(text, BorderLayout.CENTER); frame.add(panel, BorderLayout.SOUTH); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { // Exit the application df.close(); System.exit(0); } }); } public static void showFrame() { frame.setSize(300, 300); frame.setVisible(true); } public static void getConfirm(String type) { int result = JOptionPane.showConfirmDialog( frame, "You are probably under" + type + "attack!\r\nDo you want to discount?", "Warning", JOptionPane.OK_CANCEL_OPTION); if(result == JOptionPane.OK_OPTION) { loop = false; start_button.setEnabled(true); discount(); text.append("Process Ended...\r\n"); } else { df.clearTempBuffer(); return; } } public static void recognizeAttack(double z) { SnmpData sd; if(z<=2.0) { n=(n==0)? 0 : n-1; return; } if(z>8.0) { getConfirm(" "); } else { n++; } if(n==2) { df.getSNMPData(); } else if(n==3) { sd = df.getSNMPData(); double com = itd.getRxpkt()/1.2; if((sd.getUdpInDatagrams() / com) > 0.8) getConfirm(" UDP flooding "); else if(sd.getUdpNoPorts() / com > 0.8) getConfirm(" Random Port UDP flooding "); else if(sd.getIcmpInEchos() / com > 0.8) getConfirm(" Ping-of-Death "); else if(sd.getIcmpInEchoReps() / com > 0.8) getConfirm(" Smurf "); else if(sd.getIpReasmFails() / com > 0.8) getConfirm(" Targa3 "); n = 0; } } public static void discount() { text.append("Discounting...\r\n"); } }