A few days ago, the computer was infected with the Weijin virus, specially infected with the EXE file, and even anti-virus software.
So I decided to write a program to clear these viruses.
The following are the programming ideas and source code.
Clear. Java
This is a main class, mainly responsible for running programs and parameter checks, not the core
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") {// check the EXE file in the current directory
Clearlogo. dir ();
} Else if (ARGs [0]. Equals ("/a") {// check the EXE files on all disks
Clearlogo. dirall ();
} Else if (ARGs [0]. Equals ("/K") {// check the EXE file in the specified directory
If (ARGs [1] = NULL & ARGs [1]. Equals ("")){
System. Out. println ("required parameter! ");
} Else {
Clearlogo. dirfile (new file (ARGs [1]);
}
}
} Else {
Clearlogo. dir (); // check the EXE file in the current directory by default
}
} Catch (exception ex ){
Ex. printstacktrace ();
}
}
}
Clearlogo1.java
This class is mainly used to traverse files and check whether EXE files are poisoned.
Import java. Io .*;
/**
*
* <P> title: </P>
*
* <P> Description: </P>
*
* <P> copyright: Copyright (c) 2007 </P>
*
* <P> company: </P>
*
* @ Author not attributable
* @ Version 1.0
*/
Public class clearlogo1 {
// Virus size
Static long Len = 60923;
String WINDIR = NULL;
// Virus pattern Array
Byte [] virucode = new byte [1, 100];
Public clearlogo1 (){
// Obtain the installation directory of the system
WINDIR = system. getenv ("WINDIR ");
Try {
// Read the virus feature file and save the virus signature to the virucode Array
Randomaccessfile RAF = new randomaccessfile ("logo. lib", "R ");
Raf. Seek (0 );
Raf. Read (virucode );
} Catch (exception ex ){
Ex. printstacktrace ();
}
}
/**
* Check whether the system has the threat inspector.
* @ Return Boolean
*/
Public Boolean oshasviru (){
Boolean boo = false;
Try {
// Determine whether a virus file exists in the system directory
File file = new file (WINDIR + "// logow..exe ");
File file1 = new file (WINDIR + "// uninstall // rundl132.exe ");
File file2 = new file (WINDIR + "// rundl132.exe ");
If (file. exists () | file1.exists () | file2.exists ()){
Boo = true;
} Else {
Boo = false;
}
} Catch (exception ex ){
Ex. printstacktrace ();
}
Return boo;
}
/**
* Delete Virus files in the system
* @ Return Boolean
*/
Public Boolean killviru (){
// Note: in many cases, an exception occurs, because if the virus file is running, access to the file is denied.
// Manually terminate the process or use the system's ntsd command
Boolean boo = false;
Try {
File file = new file (WINDIR + "// logow..exe ");
File file1 = new file (WINDIR + "// uninstall // rundl132.exe ");
File file2 = new file (WINDIR + "// rundl132.exe ");
If (file. Delete () & file1.delete () & file2.delete ()){
Boo = true;
System. Out. println ("deleted successfully! ");
} Else {
Boo = false;
System. Out. println ("deletion failed! ");
}
} Catch (exception ex ){
}
Return boo;
}
/**
* Traverse the current directory
* @ Return int
* @ Throws exception
*/
Public int Dir () throws exception {
File dir = new file (".");
Dirfile (DIR );
Return 1;
}
/**
* Traverse all partitions
* @ Throws exception
*/
Public void dirall () throws exception {
File root [] = file. listroots ();
For (INT I = 0; I <root. length; I ++ ){
Dirfile (root [I]);
}
}
/**
* // Use recursive algorithms to traverse objects
* @ Param file File
* @ Return int
* @ Throws exception
*/
Public int dirfile (File file) throws exception {
File [] list = file. listfiles ();
// Determine that file is a non-empty directory and traverse it
If (file. isdirectory () & list! = NULL ){
For (INT I = 0; I <list. length; I ++ ){
Dirfile (list [I]);
}
} Else if (file. getname (). endswith ("EXE") | file. getname (). endswith ("EXE ")){
// If the file is an EXE file, check whether it has a virus
If (hasviru (File )){
// If a virus exists, start a virus cleanup thread to clear the virus in the file.
Clearthur read clearthur READ = new clearthur read (File );
Clearthur read. setpriority (thread. max_priority );
Clearthur read. Start ();
Thread. Sleep (1 );
} Else {
System. Out. println (file. getabsolutepath ());
}
Return 1;
}
Return 1;
}
/**
* Overload the hasviru Method
* @ Param file string
* @ Return Boolean
* @ Throws exception
*/
Public Boolean hasviru (string file) throws exception {
Return hasviru (new file (File ));
}
/**
* Check whether the file is infected.
* @ 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 ();
// Determine whether the file has two MZ marks
If (one = lenone & Two = lentwo & one = 'M' & lenone = 'M' & Two = 'Z' & lentwo = = 'Z ') {
// If the file has two MZ marks, check whether the file content is virus
Boo = hasvirucode (RAF );
Raf. Seek (0 );
}
}
Raf. Close ();
} Catch (exception ex ){
Debug.info (file. getabsolutepath () + "Kill Viru fail! ");
Boo = false;
}
Return boo;
}
/**
* Obtain the virus file size.
* @ Return long
* @ Throws exception
*/
Private long virulen () throws exception {
File file = new file (WINDIR + "// logow..exe ");
Return file. Length ();
}
/**
* Check whether the file content is virus content
* @ Param file randomaccessfile
* @ Return Boolean
* @ Throws exception
*/
Public Boolean hasvirucode (randomaccessfile file) throws exception {
Byte [] filecode = new byte [1, 100];
Boolean boo = false;
File. Seek (0 );
File. Read (filecode );
Int I;
// Compare whether the first 100 bytes of the file are the same as the first 100 bytes of the virus.
For (I = 0; I <100; I ++ ){
If (virucode [I]! = Filecode [I]) {
Break;
}
}
If (I = 100 ){
Boo = true;
} Else {
Boo = false;
}
Return boo;
}
}
Clearthur read. Java
This is the thread that clears the virus from the file.
Import java. Io .*;
/**
*
* <P> title: </P>
*
* <P> Description: clears the virus body thread in the file. </P>
*
* <P> copyright: Copyright (c) 2007 </P>
*
* <P> company: </P>
*
* @ Author not attributable
* @ Version 1.0
*/
Public class clearthur read extends thread {
File file = NULL;
Static long Len = 60923;
Clearthur read (file F ){
File = F;
}
Public void run (){
Try {
// Call the virus clearing method
Clearviru (File );
}
Catch (exception ex ){
Ex. printstacktrace ();
}
}
/**
* Reload the clearviru Method
* @ Param filename string
* @ Return Boolean
* @ Throws exception
*/
Public Boolean clearviru (string filename) throws exception {
Return clearviru (new file (filename ));
}
/**
* Clear whether a file is infected
* @ Param file File
* @ Return Boolean
* Briefly describe the principle of this method:
* 1. Create an empty file named after the file's original name + Bak
* 2. Copy the real content of the original file to this file.
* 3. Delete the original file
* 4. Rename the Bak file as the original file.
*/
Public Boolean clearviru (File file ){
Boolean boo = false;
Try {
Randomaccessfile RAF = new randomaccessfile (file, "RWD ");
// 1. Create an empty file named after the file name + 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 the real content of the original file to this file.
For (long I = 0; I <datalen; I ++ ){
Raf. Read (clear );
Rafbak. Write (clear );
}
Raf. Read (cleard );
Rafbak. Write (cleard );
Raf. Close ();
Rafbak. Close ();
// 3. Delete the original file
If (file. Delete ()){
// 4. Rename the Bak file as the original file
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 );
// Output logs to log files
Debug.info (Info );
Return boo;
}
}
There is also a debug. Java class used to write anti-virus information into log files, which is very simple. I will not post it here.
The logo. Lib virus library file cannot be pasted here.
Usage: Run cmd
// Check the EXE file in the current directory
Logo.exe/C
// Check the EXE file in all partitions
Logo.exe/
// Check the EXE file in the specified directory
Logo.exe/k c:/Program Files
I feel that the method to clear viruses is a little far-fetched. Which expert has a better method and posted a study.
If you add the virus process and registry cleanup function to this program, it can be regarded as a brute-force killer tool.
If you are interested, you can improve it.