免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6284 | 回复: 2
打印 上一主题 下一主题

一个轻量型完整性检测工具-Triproot [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-30 12:35 |只看该作者 |倒序浏览
前些天为了测试个东西,写的监控某个目录下文件变化的小程序,稍微修改下就成了类似tripwire的工具。\r\n编译: javac triproot.java\r\n运行:java triproot -init [Dir you want init] [Output file name]\r\n         java triproot -check [Dir you want check] [Trip file,that must be init early]\r\n\r\n在windows2k、window2003、AS5.1上测试过,linux下编译运行要改个字符,注释里有。\r\n\r\n\r\n\r\n
   import java.io.*;\r\n   import java.util.*;\r\n   import java.lang.*;\r\n   import java.text.*;\r\n\r\n   public class triproot\r\n   {\r\n\r\n\r\n  static  ArrayList dirlist = new ArrayList();\r\n  static  HashSet filewriter=new HashSet();\r\n  static long filenum=0;\r\n  static long dirnum=0;\r\n\r\n  \r\n\r\npublic   String   getDateString(long unixtime)    //convert unix time to human time\r\n\r\n  {  \r\n Date   date   =   new   Date(unixtime);   \r\n SimpleDateFormat formatter = new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss\");\r\n String dateString = formatter.format(date);\r\n return dateString;\r\n  }\r\n\r\n   void detectwhatisit(String receivefilename)  //detect receivefilename is a dir or a file,push dir to dirlist,push file to filewriter;\r\n\r\n   {\r\n            File dir2 = new File(receivefilename);\r\n\r\n            boolean isDir = dir2.isDirectory();\r\n            if (isDir) \r\n            {    \r\n            triproot.dirnum++;\r\n            triproot.dirlist.add(receivefilename);\r\n            }\r\n            else \r\n            {\r\n            triproot.filenum++;\r\n            String lastmodified=getDateString(dir2.lastModified());\r\n            triproot.filewriter.add(\"Size : \"+dir2.length() +\"\\t Last modify : \"+lastmodified + \" \\t File name :\"+dir2.getPath() );\r\n\r\n            }//else end\r\n\r\n\r\n    }//end of detectwhatisit\r\n\r\n\r\n\r\n\r\n\r\n\r\nboolean listfile(String getDir)          //scan getDir\r\n\r\n{\r\n   File dir = new File(getDir);\r\n   String[] children = dir.list();\r\n   boolean list_if=false;\r\n\r\n\r\n    if (children == null) \r\n    {\r\n        // Either dir does not exist or is not a directory\r\n\r\n        System.out.println(\"Directory is null\");\r\n    } \r\n    else \r\n    {\r\n        for (int i=0; i<children.length; i++) \r\n        {\r\n            // Get filename of file or directory\r\n\r\n            String filename = children[i];\r\n            detectwhatisit(getDir+\"\\\\\"+filename);   //if os is linux,replace \"\\\\\" to \"/\"\r\n\r\n\r\n            if((i+1)==children.length)\r\n            {\r\n                list_if=true;\r\n                }\r\n            else{list_if=false;}                 //if scan getDir is done,set list_if=true\r\n\r\n\r\n        }//for end\r\n\r\n    }//else end\r\n\r\n    return list_if;\r\n}//end of listfile\r\n\r\n\r\n\r\nvoid init_dir(String original,String outDir)\r\n{\r\nArrayList templist=new ArrayList();\r\nif(new triproot().listfile(original))\r\n{\r\nwhile(dirlist.size()!=0)\r\n{\r\nfor(int j=0;j<dirlist.size();j++)\r\n{\r\ntemplist.add(dirlist.get(j).toString());\r\n            if((j+1)==dirlist.size())\r\n            dirlist.clear();                   //copy dirlist to templist,and clear dirlist\r\n\r\n}\r\n\r\nfor(int k=0;k<templist.size();k++)                      //scan the second round dir\r\n\r\n{\r\nnew triproot().listfile(templist.get(k).toString());\r\n        if((k+1)==templist.size()){\r\n             templist.clear(); }\r\n            else{} \r\n}\r\n\r\nif(dirlist.size()==0)\r\n{\r\ntry\r\n{\r\n  PrintWriter out2 = new PrintWriter(new FileWriter(outDir));\r\n  Iterator ir=filewriter.iterator();\r\n  while(ir.hasNext())\r\n  {\r\n   out2.println(ir.next());\r\n  }\r\n\r\nSystem.out.println(\"Total dirs is: \"+triproot.dirnum);\r\nSystem.out.println(\"Total files is: \"+triproot.filenum);\r\nout2.close();\r\n}\r\ncatch(Exception e)\r\n{\r\nSystem.out.println(e);\r\n}\r\n}\r\n  }//end of while\r\n\r\n  }\r\n}//end of  function init_dir\r\n\r\n\r\n\r\n\r\nvoid check_dir(String newList,String oldList)\r\n{\r\ntry\r\n{\r\nBufferedReader new_List =new BufferedReader(new FileReader(newList)); \r\nBufferedReader old_List =new BufferedReader(new FileReader(oldList)); \r\nHashSet new_hash=new HashSet();\r\nHashSet old_hash=new HashSet();\r\nString s=new String();\r\n    \r\n    while((s = new_List.readLine())!= null)\r\n            {\r\n                new_hash.add(s);   \r\n            }\r\n    \r\n            \r\n            new_List.close();\r\n    while((s = old_List.readLine())!= null)\r\n            {\r\n                old_hash.add(s);   \r\n            }\r\n        \r\n            old_List.close();\r\n\r\n  File delete_temp = new File(newList);\r\n  delete_temp.delete();\r\n\r\n\r\n  Iterator irq=new_hash.iterator();\r\n  int countChange=0;\r\n  System.out.println(\"-----------Start Check-----------------------------------------------------\");\r\n  while(irq.hasNext())\r\n  {\r\n   s =(String)irq.next();\r\n   if(old_hash.contains(s))\r\n   {\r\n   \r\n   }\r\n   else\r\n   {\r\n   System.out.println(s);\r\n   countChange++;\r\n   }\r\n  }\r\n  System.out.println(\"-----------Check Done!-----------------------------------------------------\");\r\n  System.out.println(\"Total \"+countChange+ \" Files have been Modified!!!\");\r\n\r\n}\r\n\r\ncatch(Exception e)\r\n{\r\nSystem.out.println(e);\r\n}\r\n}//end of function check_dir\r\n\r\n\r\n\r\npublic static void main(String args[])   \r\n{\r\ntry{\r\nif(args.length!=3)\r\n{\r\n\r\nif(args[0].equals(\"-help\"))\r\n{\r\nSystem.out.println(\"User Guide :\");\r\nSystem.out.println(\"java triproot -init [Dir you want init] [Output file name]\");\r\nSystem.out.println(\"java triproot -check [Dir you want check] [Trip file,that must be init early]\");\r\nSystem.out.println(\"IF Dir include space,don\'t forget \\\"\\\" !\");\r\nSystem.exit(1);\r\n\r\n}\r\n\r\nif(args[0].equals(\"-check\")||args[0].equals(\"-init\"))\r\n{\r\nSystem.out.println(\"Incompleted :\");\r\nSystem.out.println(\"Please type \\\"java triproot -help\\\" for more infomation!\");\r\nSystem.exit(1);\r\n\r\n}\r\n\r\n\r\nSystem.out.println(\"Unkonw command \"+ args[0]);\r\nSystem.out.println(\"Please type \\\"java triproot -help\\\" for more infomation!\");\r\nSystem.exit(1);\r\n}\r\n}\r\ncatch(Exception e)\r\n{\r\nSystem.out.println(e);\r\nSystem.out.println(\"catched input error\");\r\n}\r\n\r\n\r\n\r\n\r\nif(args[0].equals(\"-init\"))\r\n{\r\nnew triproot().init_dir(args[1],args[2]);\r\nSystem.exit(0);\r\n}\r\n\r\n\r\n\r\nif(args[0].equals(\"-check\"))\r\n{\r\nString temp_out=\"temp.out\";\r\nnew triproot().init_dir(args[1],temp_out);\r\nnew triproot().check_dir(temp_out,args[2]);\r\nSystem.exit(0);\r\n}\r\n\r\nelse\r\n{\r\nSystem.out.println(\"Unkonw command \"+ args[0]);\r\nSystem.out.println(\"Please type \\\"java triproot -help\\\" for more infomation!\");\r\nSystem.exit(1);\r\n\r\n}\r\n\r\n}//end of main\r\n\r\n    }//end of class\r\n\r\n
\n\n[ 本帖最后由 t920 于 2008-5-30 12:40 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-06-04 13:10 |只看该作者
C:\\Program Files\\Java\\jdk1.5.0_12\\bin>javac triproot.java\r\n注意:triproot.java 使用了未经检查或不安全的操作。\r\n注意:要了解详细信息,请使用 -Xlint:unchecked 重新编译。\r\n\r\n平台p-sp3\r\n\r\n编译不成功:(

论坛徽章:
0
3 [报告]
发表于 2008-06-05 15:37 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP