In this program you will learn to create a GUI to perform Intrusion detection system (IDS) operations like, Start/Stop the IDS, View current traffic, view blocked list (IP, Domains), view current firewall rules and unblock users using Java Swing.
An intrusion detection system (IDS) inspects all inbound and outbound network activity and identifies suspicious patterns that may indicate a network or system attack from someone attempting to break into or compromise a system.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
import java.awt.Container; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class IDS1 { public static String line; public static JFrame fr; public static Container c; public static JTextArea tx; public static JScrollPane js; public static JButton ids_on,ids_off,disp_blocked_ip,disp_rules,unblock_ip; public static JTextField ip; public IDS1(){ fr=new JFrame(); c=fr.getContentPane(); c.setLayout(null); fr.setTitle("Intrusion Detection System Config"); fr.setBounds(0, 0, 920, 550); //components on the frame tx=new JTextArea(); js=new JScrollPane(tx,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); ids_on=new JButton("IDS ON"); ids_off=new JButton("IDS OFF"); disp_blocked_ip=new JButton("Blocked IPs"); disp_rules=new JButton("Firewall Rules"); ip=new JTextField("Enter Ip address"); unblock_ip=new JButton("Unblock Ip"); //setting bounds js.setBounds(5, 20, 900, 400); ids_on.setBounds(10, 470, 120, 50); ids_off.setBounds(10, 470, 120, 50); disp_blocked_ip.setBounds(140, 470, 120, 50); disp_rules.setBounds(270, 470, 120, 50); unblock_ip.setBounds(410, 470, 120, 50); ip.setBounds(410, 520, 220, 50); ip.setVisible(false); //adding components on the frame container c.add(js); c.add(ids_on); c.add(ids_off); c.add(disp_blocked_ip); c.add(unblock_ip); c.add(ip); c.add(disp_rules); //all button's action listeners ids_on.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { ids_on.setVisible(false); ids_off.setVisible(true); exec_commands("sudo service psad start"); exec_commands("sudo service psad status"); } }); ids_off.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { ids_on.setVisible(true); ids_off.setVisible(false); exec_commands("sudo service psad stop"); exec_commands("sudo service psad status"); } }); disp_blocked_ip.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { exec_commands("sudo iptables -L INPUT -v -n --line-numbers"); } }); disp_rules.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { /*exec_commands("sudo iptables -N TRAFFIC_ACCT");//own traffic chain in * order to avoid changes in firewall rules */ /*exec_commands("sudo iptables -I FORWARD -j TRAFFIC_ACCT");//forwarding all * traffic to my created chain */ /*exec_commands("iptables -A TRAFFIC_ACCT -p tcp && iptables -A TRAFFIC_ACCT * -p ip && iptables -A TRAFFIC_ACCT -p icmp"); */ exec_commands("sudo iptables -L"); } }); unblock_ip.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { String response = JOptionPane.showInputDialog(null,"Enter IP address", JOptionPane.QUESTION_MESSAGE); exec_commands("sudo iptables -D INPUT -s "+response+" -j DROP"); } }); fr.setVisible(true); fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } //Main method public static void main(String arr[]) throws Exception{ IDS1 ids=new IDS1(); } //method for execute the commands public void exec_commands(String cmd){ try { Runtime rt = Runtime.getRuntime(); //Process pr = rt.exec("cmd /c dir"); Process pr = rt.exec(cmd); BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream())); String line=null; tx.setText(""); while((line=input.readLine()) != null) { System.out.println(line); //display cmd output on textarea tx tx.append(line+"\n"); } //int exitVal = pr.waitFor(); // System.out.println("Exited with error code "+exitVal); }catch(Exception e) { System.out.println(e.toString()); e.printStackTrace(); } } } |
When you run above program, the output will be following:
How to Run this program on ECLIPSE: https://goo.gl/A2Hjqn