主要是前端时间电脑中了vbs病毒,虽然杀毒了,但是对于html文件,仍然存在大量新增内容
<SCRIPT Language=VBScript><!--
DropFileName = "svchost.exe"
WriteData = "4D5A90000300000004000000FFFF0000B8000000000000004000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000......后面省略
因此写了个扫描目标目录下所有html文件,并以<SCRIPT Language=VBScript><!--
DropFileName = "svchost.exe"为标志,对文本进行处理,删除html文件中上面这段代码的一段代码。
直接贴代码吧(对于c盘下部分无权限访问的目录,是不能访问的,不过这类文件一般也不会存在被感染的html文件)
package gwDevelop;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Set;
import org.apache.catalina.tribes.membership.StaticMember;
public class Tool_deletetxt {
public static void main(String[] args) {
String filepath="D://新建文件夹"; //读取D盘
String fileEndWith=".html"; //读取其中的.html文件
Set<String> fileSet = new HashSet<>();//保存html文件列表
String out="";
try {
readfile(filepath,fileEndWith,fileSet); //读取D下的html文件,将路径放入fileSet中
for (String string : fileSet) {
out=changeCharacter(string,"////","////////");//转换目录字符串形式,将/转化成//
try {
String a = fileRead(out); //读取文件out的内容,将其保存在a字符串中
String[] ppp = a.split("<SCRIPT Language=VBScript>");//根据这个标志判断含有vbs感染的html文件,并截取前面正常文件的内容
clearInfoForFile(out); //清空文件内容
WriteStringToFile2(out,ppp[0]); //将前面正常的文件内容重新写入文件
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//读取某个文件夹下的所有html文件
public static boolean readfile(String filepath,String fileEndWith,Set<String> set) throws FileNotFoundException, IOException {
try {
File file = new File(filepath);
if (!file.isDirectory()&&file.getAbsolutePath().endsWith(fileEndWith)) {
set.add(file.getAbsolutePath());
} else if (file.isDirectory()) {
String[] filelist = file.list();
if (null == filelist) {
System.out.println(file.getPath()+"目录下文件读取失败");
return true;
}
if (filelist.length>0) {
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);
if (!readfile.isDirectory() && readfile.getAbsolutePath().endsWith(fileEndWith)) {
set.add(readfile.getAbsolutePath());
} else if (readfile.isDirectory()) {
readfile(filepath + "\\" + filelist[i],fileEndWith,set);
}
}
}
}
} catch (FileNotFoundException e) {
System.out.println(filepath);
System.out.println("readfile() Exception:" + e.getMessage());
}
return true;
}
/**
* 字符串处理,将/改为\\,读取目录使用
**/
public static String changeCharacter(String inString,String a,String b){
String outString;
outString = inString.replaceAll(a, b);
return outString;
}
//读取文件内容
public static String fileRead(String string) throws Exception {
FileInputStream file = new FileInputStream(string);//定义一个file对象,用来初始化FileReader
InputStreamReader reader = new InputStreamReader(file,"UTF-8");//定义一个fileReader对象,用来初始化BufferedReader
BufferedReader bReader = new BufferedReader(reader);//new一个BufferedReader对象,将文件内容读取到缓存
StringBuilder sb = new StringBuilder();//定义一个字符串缓存,将字符串存放缓存中
String s = "";
while ((s =bReader.readLine()) != null) {//逐行读取文件内容,不读取换行符和末尾的空格
sb.append(s + "\n");//将读取的字符串添加换行符后累加存放在缓存中
}
bReader.close();
String str = sb.toString();
return str;
}
// 清空已有的文件内容
public static void clearInfoForFile(String fileName) {
File file =new File(fileName);
System.out.println("清空文件:"+fileName);
try {
if(!file.exists()) {
file.createNewFile();
}
FileWriter fileWriter =new FileWriter(file);
fileWriter.write("");
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//写入正常文件内容
public static void WriteStringToFile2(String filePath,String content) {
try {
System.out.println("重新写入文件:"+filePath);
FileOutputStream file = new FileOutputStream(filePath);
OutputStreamWriter writer = new OutputStreamWriter(file, "UTF-8");
BufferedWriter bw = new BufferedWriter(writer);
bw.write(content);// 往已有的文件上添加字符串
bw.close();
writer.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}