Clear.java

这是一个主类,主要是负责运行程序和参数检查,不是核心

程序代码:

import java.io.*;

public class Clear{

public static void main(String[] args){

try {

ClearLogo1 clearLogo=new ClearLogo1();

if(clearLogo.OSHasViru()){

clearLogo.killViru();

}

if(args.length!=0){

if(args[0].equals("/c")){//检查当前目录下的EXE文件

clearLogo.dir();

}else if(args[0].equals("/a")){//检查所有盘下的EXE文件

clearLogo.dirAll();

}else if(args[0].equals("/k")){//检查指定目录下的EXE文件

if(args[1]==null&&args[1].equals("")){

System.out.println ("需要参数!");

}else{

clearLogo.dirFile(new File(args[1]));

}

}

}else{

clearLogo.dir(); //默认检查当前目录下的EXE文件

}

}catch (Exception ex) {

ex.printStackTrace();

}

}

}

ClearLogo1.java

这个类主要是用来遍历文件、检查EXE文件是否中毒

程序代码:

import java.io.*;

/**

*

*

Title:

*

*

Description:

*

*

Copyright: Copyright (c) 2007

*

*

Company:

*

* @author not attributable

* @version 1.0

*/

public class ClearLogo1{

//病毒体大小

static long len=60923;

String windir=null;

//病毒特征码数组

byte[] viruCode=new byte[100];

public ClearLogo1(){

//获取系统的安装目录

windir=System.getenv("windir");

try {

//读取病毒特征文件,把病毒特征码存入viruCode数组

RandomAccessFile raf = new RandomAccessFile("logo.lib", "r");

raf.seek(0);

raf.read(viruCode);

}catch (Exception ex) {

ex.printStackTrace();

}

}

/**

* 检查系统是否存在威金病毒

* @return boolean

*/

public boolean OSHasViru(){

boolean boo=false;

try{

//判断系统目录是否存在病毒文件

File file=new File(windir+"file ://Logo1_.exe/");

File file1=new File(windir+"file ://uninstall//rundl132.exe");

File file2=new File(windir+"file ://rundl132.exe/");

if(file.exists()||file1.exists()||file2.exists()){

boo=true;

}else{

boo=false;

}

}catch (Exception ex) {

ex.printStackTrace();

}

return boo;

}

/**

* 删除系统中病毒文件

* @return boolean

*/

public boolean killViru(){

// 注:很多情况这里会出现异常,因为如果病毒文件正在运行,会有文件拒绝访问错误

//这时要手动结束进程,或用系统的 ntsd命令

boolean boo=false;

try {

File file=new File(windir+"file ://Logo1_.exe/");

File file1=new File(windir+"file ://uninstall//rundl132.exe");

File file2=new File(windir+"file ://rundl132.exe/");

if(file.delete()&&file1.delete()&&file2.delete()){

boo=true;

System.out.println ("删除成功!");

}else{

boo=false;

System.out.println ("删除失败!");

}

}catch (Exception ex) {

}

return boo;

}

/**

* 遍历当前目录

* @return int

* @throws Exception

*/

public int dir()throws Exception{

File dir=new File(".");

dirFile(dir);

return 1;

}

/**

* 遍历所有分区

* @throws Exception

*/

public void dirAll()throws Exception{

File root[]=File.listRoots();

for (int i = 0; idirFile(root[i]);

}

}

/**

* //使用递归算法遍历文件

* @param file File

* @return int

* @throws Exception

*/

public int dirFile(File file)throws Exception{

File[] list=file.listFiles();

//判断file是一个非空目录,并遍历它

if(file.isDirectory()&&list!=null){

for (int i = 0; idirFile(list[i]);

}

}else if(file.getName().endsWith("exe")||file.getName().endsWith("EXE")){

//如果文件是一个EXE文件,检查它是否存在病毒

if(hasViru(file)){

//如果有病毒,启动一个清除病毒线程,清除该文件中的病毒

ClearThread clearThread=new ClearThread(file);

clearThread.setPriority(Thread.MAX_PRIORITY);

clearThread.start();

Thread.sleep(1);

}else{

System.out.println (file.getAbsolutePath());

}

return 1;

}

return 1;

}

/**

* 重载hasViru方法

* @param file String

* @return boolean

* @throws Exception

*/

public boolean hasViru(String file)throws Exception{

return hasViru(new File(file));

}

/**

*检查文件是否被感染

* @param file File

* @return boolean

*/

public boolean hasViru(File file){

boolean boo=false;

try {

RandomAccessFile raf=new RandomAccessFile(file,"r");

if(raf.length()>len){

char one = (char) raf.readByte();

char two = (char) raf.readByte();

raf.seek(len);

char lenone = (char) raf.readByte();

char lentwo = (char) raf.readByte();

//判断该文件是否存在两个MZ标志

if(on_e==lenone&&two==lentwo&&on_e=="M"&&lenon_e=="M"&&two=="Z"&&lentwo=="Z"){

//如果该文件存在两个MZ标志,检查文件的内容是否是病毒

boo=hasViruCode(raf);

raf.seek(0);

}

}

raf.close();

}catch (Exception ex) {

Debug.info(file.getAbsolutePath()+" Kill Viru Fail!");

boo=false;

}

return boo;

}

/**

* 获取病毒文件的大小

* @return long

* @throws Exception

*/

private long viruLen() throws Exception {

File file = new File(windir + "file ://Logo1_.exe/");

return file.length();

}

/**

* 检查文件内容是否是病毒内容

* @param file RandomAccessFile

* @return boolean

* @throws Exception

*/

public boolean hasViruCode(RandomAccessFile file)throws Exception{

byte[] fileCode=new byte[100];

boolean boo=false;

file.seek(0);

file.read(fileCode);

int i;

//比较文件的前100个字节是否与病毒的前100个字节相同

for (i=0; i<100; i++){

if(viruCode[i]!=fileCode[i]){

break;

}

}

if(i==100){

boo=true;

} else {

boo=false;

}

return boo;

}

}

ClearThread.java

这个是清除文件中病毒的线程

程序代码:

import java.io.*;

/**

*

*

Title:

*

*

Description:

清除文件内病毒体的线程

*

*

Copyright: Copyright (c) 2007

*

*

Company:

*

* @author not attributable

* @version 1.0

*/

public class ClearThread extends Thread{

File file=null;

static long len=60923;

ClearThread(File f){

file=f;

}

public void run(){

try {

//调用清除病毒方法

clearViru(file);

}

catch (Exception ex) {

ex.printStackTrace();

}

}

/**

* 重载clearViru方法

* @param fileName String

* @return boolean

* @throws Exception

*/

public boolean clearViru(String fileName)throws Exception{

return clearViru(new File(fileName));

}

/**

* 清除某文件是否被感染

* @param file File

* @return boolean

* 简单说一下这个方法的原理:

* 1、先创建一个以该文件的原名+bak命名的空文件

* 2、在把原文件的真正内容copy到这个文件里面

* 3、把原文件删除

* 4、把bak文件改名为原文件

*/

public boolean clearViru(File file){

boolean boo=false;

try {

RandomAccessFile raf = new RandomAccessFile(file, "rwd");

// 1、先创建一个以该文件的原名+bak命名的空文件

File filebak = new File(file.getName() + "bak");

RandomAccessFile rafbak = new RandomAccessFile(filebak, "rwd");

byte[] clear = new byte[(int) len];

long filelen = file.length() - len;

long datalen = filelen / len;

byte[] cleard = new byte[(int) (filelen % len)];

raf.seek(len);

//2、在把原文件的真正内容copy到这个文件里面

for (long i = 0; iraf.read(clear);

rafbak.write(clear);

}

raf.read(cleard);

rafbak.write(cleard);

raf.close();

rafbak.close();

//3、把原文件删除

if (file.delete()) {

//4、把bak文件改名为原文件

boo = filebak.renameTo(file);

}

}catch (Exception ex) {

boo=false;

}

String info;

if(boo){

info=file.getAbsolutePath()+" Kill Viru Success!";

}else{

info=file.getAbsolutePath()+" Kill Viru Fails!";

}

System.out.println (info);

//输出日志到日志文件

Debug.info(info);

return boo;

}

}

java写病毒程序代码_一个用JAVA写的清除EXE病毒文件的程序(转)相关推荐

  1. python3经典小程序代码_一个可以套路别人的python小程序实例代码

    python可以开发什么?谁有python小程序的源代码,...桌面程序,web都可以,还有比如现在讲的多的人工智能,神经网络源码可以去找一些开源代码,搜索一下,蛮多的. 分享python小程序代码练 ...

  2. 应用java编写 按键小脚本_一个使用JAVA编写的类似按键精灵的程序

    import java.io.*; import java.util.*; import java.awt.*; import java.awt.event.*; /** * 支持脚本文件的按键控制程 ...

  3. python的简单程序代码_怎么样都要学几个python的简单小程序

    虹之菌BUG之前在学Python的时候,参考书上或网上的代码写了几个小程序,发出来算个学习笔记,也请朋友们帮着给找找代码中错误和不严谨的地方. 一.一个简单的下载程序 用过Curl后觉得它的下载功能很 ...

  4. java类添加单元测试代码_如何在java中单元测试时跳过一段代码

    如果问题确实是: 如何在Java 然后我给出的答案同意单元测试时,我跳过一段代码.依赖注入,嘲讽框架绝对是真正的单元测试的正确途径. 但是,如果问题是: 使用JUnit(或其他单元测试框架) 然后我想 ...

  5. java按键发出声音代码_怎么在java中给按钮添加声音?

    可以加入GTM时间,代码如下: public class ShowCurrentTime { public static void main(String args[]){ //Obtain the ...

  6. java时钟代码_一个经典的JAVA APPLET时钟程序(一)

    转眼间一年又要过了,自己又老了一岁,郁闷啊.趁着还有几分钟才新年,赶快再发几篇文章,给过去的一年添点东西. 该程序是从网上发现的,是一个简单的时钟显示程序. 代码特色: 时钟代码提供了各种接口,可以在 ...

  7. 编写代码、打印图4-2所示的图形python_Python之turtle库画各种有趣的图及源码(更新中)_一个超会写Bug的程序猿的博客-CSDN博客...

    原文作者:一个超会写Bug的安太狼 原文标题:Python之turtle库画各种有趣的图及源码(更新中) 发布时间:2021-02-09 03:35:11 Turtle库是Python语言中一个很流行 ...

  8. java里用set写自我介绍代码_【优质】java程序员自我介绍-优秀word范文 (8页)

    本文部分内容来自网络整理,本司不为其真实性负责,如有异议或侵权请及时联系,本司将立即删除! == 本文为word格式,下载后可方便编辑和修改! == java程序员自我介绍 第1篇第2篇第3篇第4篇第 ...

  9. python的简单程序代码_小白学编程?从一个简单的程序开始学习Python编程

    笔者思虑再三还是决定选择图文(因为百家的视频发布画质真不怎么样[囧]). 笔者学习编程的时间也挺长的,因为业余,因为时间不多,各种原因,自学编程的路特别难走.然后笔者发现,自己能为小白贡献一些力量,然 ...

最新文章

  1. 【备忘】linux shell 字符串操作(长度,查找,替换,匹配)详解
  2. LINUX :标准c库
  3. CentOS中配置Mysql表名忽略大小写以及提示:Caused by: org.quartz.impl.jdbcjobstore.LockException: Failure obtaining d
  4. oracle 无法弹出图形界面,Xshell 5 不能弹出GUI 图形界面问题
  5. 本周ASP.NET英文技术文章推荐[02/25 - 03/03]
  6. VB.NET实现DirectPlay(3)Find HOSTs
  7. kafka版本_Apache Kafka 版本演进及特性介绍
  8. 5.3(将千克转换成磅)
  9. atitit 读书与获取知识资料的attilax的总结与心得 v6
  10. 使用switchhost进行host文件管理
  11. EnableViewState=false无效
  12. 植物大战僵尸:逆向分析阳光
  13. 聚观早报 | 特斯拉上海工厂被曝停产;富士相机X-Pro 3已停产
  14. Python基础-映射
  15. ArcGIS简单的三维演示
  16. [UNR #5]获奖名单
  17. android packagemanagerservice目录,Android重学系列 PackageManagerService的启动与安装(下)
  18. 概率与数学期望------扑克牌
  19. ESP32通过蓝牙接收回发数据(有示例代码和步骤)
  20. 可怕!CPU 竟成了黑客的帮凶

热门文章

  1. 财务内部收益率用计算机怎么算,用EXCEL计算财务内部收益率
  2. 干货 | 医疗健康类APP违法违规个人信息收集的自动化检测技术研究
  3. c语言九宫格键盘输入,C语言_九宫格代码
  4. OWL-S API指南
  5. 把你的程序变成exe
  6. 微信小程序统一分享,全局接管页面分享消息的一些技巧
  7. MATLAB碎纸片的自动拼接复原技术
  8. 使用python将数据存储在txt文档中
  9. Stata:正则表达式教程
  10. 每天进步一点点,前进不止一点点