cbuildidmef.java

来自「windows系统下用java开发的网络入侵检测程序」· Java 代码 · 共 414 行

JAVA
414
字号
import javax.xml.parsers.*;                                                                                                                         import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;                                                   
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;                                        
import javax.xml.transform.stream.StreamResult;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 * 
 * Build XML IDMEF file.
 * 2005.9.15
 * @version 0.1.2
 * 
 * @author Yuan Wang
 *
 */
public class CbuildIDMEF extends Frame implements ActionListener{
	
	/**
	 * Button OK. If user has not opened the save file dialog and choose the path and file name
	 * ,save file dialog will be open. The the content entered in the textfield will be build 
	 * to the file.
	 */
	JButton button;
	/**
	 * Button Cancel. Close the window.
	 */
	JButton button1;
	/**
	 * Button Open.  Open the save file dialog to choose the build file will be saved to where.
	 */
	JButton button2;
	/**
	 * Button Clear.  Clear all the textfield.
	 */
	JButton button3;
	/**
	 * Show each textfield represent what content which will be build to the XML IDMEF file.
	 */
	JLabel label[];
	/**
	 * The build XML IDMEF file's contents come from these textfield.
	 */
	JTextField text[];
	/**
	 * Label.:"Attention:Each field can't be null."
	 */
	Label label1;
	/**
	 * Label.:"Please enter the Open button to set the path and name of file to be saved :"
	 */
	JLabel label2;
	/**
	 * Show the saved XML IDMEF file's path and name.
	 */
	JTextField tex;
	
	/**
	 * Save file dialog.
	 */
	FileDialog filedialog_save;
	CbuildIDMEF()
	{
		super("Build IDMEF XML File");
		
	    setBounds(20,50,780,450);
	    setVisible(false);
	    setLayout(new BorderLayout());
	    filedialog_save=new FileDialog(this,"Save File Dialog",FileDialog.SAVE);
	    filedialog_save.setVisible(false);
	    
	    button=new JButton(" OK ");
	    button1=new JButton("Cancel");
	    button2=new JButton("Open");
	    button3=new JButton("Clear");
	    
	    button.addActionListener(this);
	    button1.addActionListener(this);
	    button2.addActionListener(this);
	    button3.addActionListener(this);
	    label=new JLabel[24];
	    text=new JTextField[24];
	    label1=new Label("Attention:Each field can't be null.",Label.CENTER);
	    label1.setBackground(Color.red);
	    label2=new JLabel("Please enter the Open button to set the path and name of file to be saved :");
	    
	    for(int i=0;i<24;i++)
	    { 
	    	text[i]=new JTextField(25);
	    }
	    tex=new JTextField(40);
	    
	    label[0]= new JLabel("Alert messageid:");
	    label[1]= new JLabel("Analyzer analyzerid:");
	    label[2]= new JLabel("Analyzer Node category:");
	    label[3]= new JLabel("Analyzer Node location:");
	    label[4]= new JLabel("Analyzer Node name:");
	    label[5]= new JLabel("CreateTime ntpstamp:");
	    label[6]= new JLabel("CreateTime:");
	    label[7]= new JLabel("Source ident:");
	    label[8]= new JLabel("Source Node ident:");
	    label[9]= new JLabel("Source Node category:");
	    label[10]= new JLabel("Source Node name:");
	    label[11]= new JLabel("Source Node Address ident:");
	    label[12]= new JLabel("Source Node Address category:");
	    label[13]= new JLabel("Source Node address:");
	    label[14]= new JLabel("Source Node netmask:");
	    label[15]= new JLabel("Target ident:");
	    label[16]= new JLabel("Target Node ident:");
	    label[17]= new JLabel("Target Node category:");
	    label[18]= new JLabel("Target Node Address category:");
	    label[19]= new JLabel("Target Node address:");
	    label[20]= new JLabel("Classification text:");
	    label[21]= new JLabel("Reference origin:");
	    label[22]= new JLabel("Reference name:");
	    label[23]= new JLabel("Reference url:");
	    JPanel p=new JPanel();
	    JPanel p1=new JPanel();
	    JPanel p2=new JPanel();
	    JPanel p3=new JPanel();
	    JPanel p4=new JPanel();
	   
	   
	    p.setLayout(new GridLayout(13,4));
	    for(int i=0;i<24;i++)
	    {
	    	p.add(label[i]);
	        p.add(text[i]); 
	    }
	    
	     
	    p2.setLayout(new GridLayout(3,1));
	    
	    
	    tex.setEditable(false);
	    p3.add(label2);
	    p4.add(tex);
	    p4.add(button2);
	    p4.add(button3);
	    
  	    p2.add(label1);
  	    p2.add(p3);
  	    p2.add(p4);
	    
	    p1.add(button);
	    p1.add(button1);
	    p1.setBackground(Color.lightGray);
	    add(p,"North");
	   
	    add(p2,"Center");
	    add(p1,"South");
	    

	    addWindowListener(new WindowAdapter()
	      {public void windowClosing(WindowEvent e)
	      {setVisible(false);}
	      });
	    filedialog_save.addWindowListener(new WindowAdapter()
	      {  public void windowClosing(WindowEvent e)
	         {
	           filedialog_save.setVisible(false);}
	      });
	   
	    
	}
	/**
	 * Listen button OK,Cancel,Open,Clear's action.
	 */
	
	public void actionPerformed(ActionEvent e)
	{
		
		String messageid;
	    String analyzerid;
	    String nodecategory;
	    String nodelocation;
	    String nodename;
	    String createtimentpstamp;
	    String createtime;
	    String sident;
	    String snodeident;
	    String snodecategory;
	    String snodename;
	    String snodeaddressident;
	    String snodeaddresscategory;
	    String snodeaddress;
	    String snodenetmask;
	    String tident;
	    String tnodeident;
	    String tnodecategory;
	    String tnodeaddresscategory;
	    String tnodeaddress;
	    String ctext;
	    String creferenceorgin;
	    String creferencename;
	    String creferenceurl;
	    String str;
	    String str1;
    
	    if(e.getSource()==button)
        {
	    	if(tex.getText().equals(""))
	 	    {
	 	    	filedialog_save.setVisible(true);
	 	    	str=filedialog_save.getFile();
		 	    str1=filedialog_save.getDirectory();
		 	    tex.setText(str1+str);
		 	    if(str==null || str1==null)
		 	    {
		 	    	tex.setText("");
		 	    }
		 	    
	 	    }
	    	else
	    	{
	    		try
	            {
	            	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	                DocumentBuilder builder=factory.newDocumentBuilder();
	                Document doc=builder.parse("idm.xml");
	                doc.normalize();
	                messageid=text[0].getText();//"abc123456789";
	                analyzerid=text[1].getText();//"hq-dmz-analyzer01";
		       	    nodecategory=text[2].getText();//"dns";
		       	    nodelocation=text[3].getText();//"Headquarters DMZ Network";
		       	    nodename=text[4].getText();//"analyzer01.example.com";
		       	    createtimentpstamp=text[5].getText();//"0xbc723b45.0xef449129";
		       	    createtime=text[6].getText();//"2000-03-09T10:01:25.93464-05:00";
		       	    sident=text[7].getText();//"a1b2c3d4";
		       	    snodeident=text[8].getText();//"a1b2c3d4-001";
		       	    snodecategory=text[9].getText();//"dns";
		       	    snodename=text[10].getText();//"badguy.example.net";
		       	    snodeaddressident=text[11].getText();//"a1b2c3d4-002";
		       	    snodeaddresscategory=text[12].getText();//"ipv4-net-mask";
		       	    snodeaddress=text[13].getText();//"192.0.2.50";
		       	    snodenetmask=text[14].getText();//"255.255.255.255";
		       	    tident=text[15].getText();//"d1c2b3a4";
		       	    tnodeident=text[16].getText();//"d1c2b3a4-001";
		       	    tnodecategory=text[17].getText();//"dns";
		       	    tnodeaddresscategory=text[18].getText();//"ipv4-addr-hex";
		       	    tnodeaddress=text[19].getText();//"0xde796f70";
		       	    ctext=text[20].getText();//"Teardrop detected";
		       	    creferenceorgin=text[21].getText();//"bugtraqid";
		       	    creferencename=text[22].getText();//"124";
		       	    creferenceurl=text[23].getText();//"http://www.securityfocus.com/bid/124";
		       	    
		       	    Text textseg;
		 	        Element root=doc.getDocumentElement();
		 	        Element Alert=doc.createElement("Alert");
			 	    Alert.setAttribute("messageid",messageid);
			 	    
			 	    Element Analyzer=doc.createElement("Analyzer");
			 	    Analyzer.setAttribute("analyzerid",analyzerid);

			 	    Element node=doc.createElement("Node");
			 	    node.setAttribute("category",nodecategory);

			 	    Element nlocation=doc.createElement("location");
			 	    textseg=doc.createTextNode(nodelocation);
			 	    nlocation.appendChild(textseg);
			 	    node.appendChild(nlocation);
			 	   
			 	    Element nname=doc.createElement("name");
			 	    textseg=doc.createTextNode(nodename);
			 	    nname.appendChild(textseg);
			 	    node.appendChild(nname);
			 	    
			 	    Analyzer.appendChild(node);
			 	    
			 	    Alert.appendChild(Analyzer);

			 	    Element CreateTime=doc.createElement("CreateTime");
			 	    CreateTime.setAttribute("ntpstamp",createtimentpstamp);
			 	    textseg=doc.createTextNode(createtime);
			 	    CreateTime.appendChild(textseg);

			 	    Alert.appendChild(CreateTime);

			 	    Element Source=doc.createElement("Source");
			 	    Source.setAttribute("ident",sident);

			 	    Element snode=doc.createElement("Node");
			 	    snode.setAttribute("ident",snodeident);
			 	    snode.setAttribute("category",snodecategory);

			 	    Element snname=doc.createElement("name");
			 	    textseg=doc.createTextNode(snodename);
			 	    snname.appendChild(textseg);
			 	    snode.appendChild(snname);
			 	    
			 	    Element sAddress=doc.createElement("Address");
			 	    sAddress.setAttribute("ident",snodeaddressident);
			 	    sAddress.setAttribute("category",snodeaddresscategory);

			 	    Element saddress=doc.createElement("address");
			 	    textseg=doc.createTextNode(snodeaddress);
			 	    saddress.appendChild(textseg);
			 	    sAddress.appendChild(saddress);
			 	   
			 	    Element snetmask=doc.createElement("netmask");
			 	    textseg=doc.createTextNode(snodenetmask);
			 	    snetmask.appendChild(textseg);
			 	    sAddress.appendChild(snetmask);

			 	   
			 	    snode.appendChild(sAddress);

			 	    Source.appendChild(snode);

			 	    Alert.appendChild(Source);

			 	    Element Target=doc.createElement("Target");
			 	    Target.setAttribute("ident",tident);

			 	    Element tnode=doc.createElement("Node");
			 	    tnode.setAttribute("ident",tnodeident);
			 	    tnode.setAttribute("category",tnodecategory);

			 	    Element tAddress=doc.createElement("Address");
			 	    tAddress.setAttribute("category",tnodeaddresscategory);

			 	    Element taddress=doc.createElement("address");
			 	    textseg=doc.createTextNode(tnodeaddress);
			 	    taddress.appendChild(textseg);
			 	    tAddress.appendChild(taddress);

			 	    tnode.appendChild(tAddress);

			 	    Target.appendChild(tnode);

			 	    Alert.appendChild(Target);

			 	    Element Classification=doc.createElement("Classification");
			 	    Classification.setAttribute("text",ctext);

			 	    Element Reference=doc.createElement("Reference");
			 	    Reference.setAttribute("orgin",creferenceorgin);

			 	    Element crname=doc.createElement("name");
			 	    textseg=doc.createTextNode(creferencename);
			 	    crname.appendChild(textseg);
			 	    Reference.appendChild(crname);
			 	   
			 	    Element crurl=doc.createElement("url");
			 	    textseg=doc.createTextNode(creferenceurl);
			 	    crurl.appendChild(textseg);
			 	    Reference.appendChild(crurl);
			 	 
			 	    Classification.appendChild(Reference);

			 	    Alert.appendChild(Classification);

			 	    root.appendChild(Alert);
			 	    
			 	    TransformerFactory tFactory=TransformerFactory.newInstance();
			 	    Transformer transformer=tFactory.newTransformer();
			 	    DOMSource source=new DOMSource(doc);
			 	    
			 	    str=filedialog_save.getFile();
			 	    str1=filedialog_save.getDirectory();
			 	    StreamResult result=new StreamResult(new java.io.File(str1,str));
			 	    transformer.transform(source,result);
		 	        MsgDialog msg=new MsgDialog();
		 	        msg.show("The file has been saved to"+str1+str);
                   
		 	    
	       	     
	            }
	            catch(Exception m)
	            { 
	                m.printStackTrace();
	            }
	    	}
            
        }//end if button
	    if(e.getSource()==button2)
	    {
	    	if(tex.getText().equals(""))
	 	    {
	 	    	filedialog_save.setVisible(true);
	 	    	str=filedialog_save.getFile();
		 	    str1=filedialog_save.getDirectory();
		 	    tex.setText(str1+str);
		 	    if(str==null || str1==null)
		 	    {
		 	    	tex.setText("");
		 	    }
		 	    
	 	    }
	    }
	    if(e.getSource()==button3)
	    {
	    	for(int i=0;i<24;i++)
	    	{
	    		text[i].setText("");
	    	}
		    tex.setText("");
	    }
	    if(e.getSource()==button1)
	    {
	    	setVisible(false);
	    }
		
		
	}
	

	 


}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?