Chinaunix

标题: 一个轻量型完整性检测工具-Triproot [打印本页]

作者: t920    时间: 2008-05-30 12:35
标题: 一个轻量型完整性检测工具-Triproot
前些天为了测试个东西,写的监控某个目录下文件变化的小程序,稍微修改下就成了类似tripwire的工具。
编译: javac triproot.java
运行:java triproot -init [Dir you want init] [Output file name]
         java triproot -check [Dir you want check] [Trip file,that must be init early]

在windows2k、window2003、AS5.1上测试过,linux下编译运行要改个字符,注释里有。



   import java.io.*;
   import java.util.*;
   import java.lang.*;
   import java.text.*;

   public class triproot
   {


  static  ArrayList dirlist = new ArrayList();
  static  HashSet filewriter=new HashSet();
  static long filenum=0;
  static long dirnum=0;

  

public   String   getDateString(long unixtime)    //convert unix time to human time

  {  
Date   date   =   new   Date(unixtime);   
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(date);
return dateString;
  }

   void detectwhatisit(String receivefilename)  //detect receivefilename is a dir or a file,push dir to dirlist,push file to filewriter;

   {
            File dir2 = new File(receivefilename);

            boolean isDir = dir2.isDirectory();
            if (isDir)
            {   
            triproot.dirnum++;
            triproot.dirlist.add(receivefilename);
            }
            else
            {
            triproot.filenum++;
            String lastmodified=getDateString(dir2.lastModified());
            triproot.filewriter.add("Size : "+dir2.length() +"\t Last modify : "+lastmodified + " \t File name :"+dir2.getPath() );

            }//else end


    }//end of detectwhatisit






boolean listfile(String getDir)          //scan getDir

{
   File dir = new File(getDir);
   String[] children = dir.list();
   boolean list_if=false;


    if (children == null)
    {
        // Either dir does not exist or is not a directory

        System.out.println("Directory is null");
    }
    else
    {
        for (int i=0; i<children.length; i++)
        {
            // Get filename of file or directory

            String filename = children[i];
            detectwhatisit(getDir+"\\"+filename);   //if os is linux,replace "\\" to "/"


            if((i+1)==children.length)
            {
                list_if=true;
                }
            else{list_if=false;}                 //if scan getDir is done,set list_if=true


        }//for end

    }//else end

    return list_if;
}//end of listfile



void init_dir(String original,String outDir)
{
ArrayList templist=new ArrayList();
if(new triproot().listfile(original))
{
while(dirlist.size()!=0)
{
for(int j=0;j<dirlist.size();j++)
{
templist.add(dirlist.get(j).toString());
            if((j+1)==dirlist.size())
            dirlist.clear();                   //copy dirlist to templist,and clear dirlist

}

for(int k=0;k<templist.size();k++)                      //scan the second round dir

{
new triproot().listfile(templist.get(k).toString());
        if((k+1)==templist.size()){
             templist.clear(); }
            else{}
}

if(dirlist.size()==0)
{
try
{
  PrintWriter out2 = new PrintWriter(new FileWriter(outDir));
  Iterator ir=filewriter.iterator();
  while(ir.hasNext())
  {
   out2.println(ir.next());
  }

System.out.println("Total dirs is: "+triproot.dirnum);
System.out.println("Total files is: "+triproot.filenum);
out2.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
  }//end of while

  }
}//end of  function init_dir




void check_dir(String newList,String oldList)
{
try
{
BufferedReader new_List =new BufferedReader(new FileReader(newList));
BufferedReader old_List =new BufferedReader(new FileReader(oldList));
HashSet new_hash=new HashSet();
HashSet old_hash=new HashSet();
String s=new String();
   
    while((s = new_List.readLine())!= null)
            {
                new_hash.add(s);   
            }
   
            
            new_List.close();
    while((s = old_List.readLine())!= null)
            {
                old_hash.add(s);   
            }
        
            old_List.close();

  File delete_temp = new File(newList);
  delete_temp.delete();


  Iterator irq=new_hash.iterator();
  int countChange=0;
  System.out.println("-----------Start Check-----------------------------------------------------");
  while(irq.hasNext())
  {
   s =(String)irq.next();
   if(old_hash.contains(s))
   {
   
   }
   else
   {
   System.out.println(s);
   countChange++;
   }
  }
  System.out.println("-----------Check Done!-----------------------------------------------------");
  System.out.println("Total "+countChange+ " Files have been Modified!!!");

}

catch(Exception e)
{
System.out.println(e);
}
}//end of function check_dir



public static void main(String args[])   
{
try{
if(args.length!=3)
{

if(args[0].equals("-help"))
{
System.out.println("User Guide :");
System.out.println("java triproot -init [Dir you want init] [Output file name]");
System.out.println("java triproot -check [Dir you want check] [Trip file,that must be init early]");
System.out.println("IF Dir include space,don't forget \"\" !");
System.exit(1);

}

if(args[0].equals("-check")||args[0].equals("-init"))
{
System.out.println("Incompleted :");
System.out.println("Please type \"java triproot -help\" for more infomation!");
System.exit(1);

}


System.out.println("Unkonw command "+ args[0]);
System.out.println("Please type \"java triproot -help\" for more infomation!");
System.exit(1);
}
}
catch(Exception e)
{
System.out.println(e);
System.out.println("catched input error");
}




if(args[0].equals("-init"))
{
new triproot().init_dir(args[1],args[2]);
System.exit(0);
}



if(args[0].equals("-check"))
{
String temp_out="temp.out";
new triproot().init_dir(args[1],temp_out);
new triproot().check_dir(temp_out,args[2]);
System.exit(0);
}

else
{
System.out.println("Unkonw command "+ args[0]);
System.out.println("Please type \"java triproot -help\" for more infomation!");
System.exit(1);

}

}//end of main

    }//end of class



[ 本帖最后由 t920 于 2008-5-30 12:40 编辑 ]
作者: ylcqen    时间: 2008-06-03 09:14
嗯,写得不错!
作者: sunki    时间: 2008-06-04 13:10
C:\Program Files\Java\jdk1.5.0_12\bin>javac triproot.java
注意:triproot.java 使用了未经检查或不安全的操作。
注意:要了解详细信息,请使用 -Xlint:unchecked 重新编译。

平台p-sp3

编译不成功:(
作者: 烟花如绣    时间: 2008-06-05 15:37
提示: 作者被禁止或删除 内容自动屏蔽
作者: friendshao    时间: 2008-06-19 18:06
有点问题
作者: yuanyuan025    时间: 2008-06-23 01:18
支持了 强
作者: flb_2001    时间: 2008-08-22 12:08
怎么使用的啊?
作者: beyondfly    时间: 2008-12-02 03:16
嗯,写得不错!
作者: shangrilawyx    时间: 2010-05-16 00:53
顶!!
作者: 地球车    时间: 2010-05-16 12:11
提示: 作者被禁止或删除 内容自动屏蔽
作者: 野狼1978    时间: 2010-05-16 13:47

作者: twsfdr    时间: 2010-05-19 17:30
提示: 作者被禁止或删除 内容自动屏蔽




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2