修复WebCompiler被某病毒后无法打开电子书的问题

本文介绍了一款使用C++编写的工具,该工具能够扫描并修复被特定病毒感染的电子书文件,使其恢复正常。通过检查文件末尾的特定字节序列来判断文件是否被感染,并移除病毒代码。
编程语言:C++
类    别:(其他)
主要功能:修复WebCompiler被某病毒后,无法打开电子书的问题
说明:有种病毒在电子书最后几个字节加上了一些代码.

    先看一下文件最后4个字节 
    20070707111303312.JPG
    红线上的部分,是多出来的几个字节,就是它造成了电子书不能打开!!
由于我有好多电子书,要是手动改,也太麻烦了.所以写了一个工具,希望有遇到我和一样烦恼的人,可以重新修复电子书.

    修复前要备份电子书呀,我不敢保证所有病毒都这样破坏我们的电子书

    下面我们来看看代码实现
None.gif //  FixBOOK.cpp : Defines the entry point for the console application.
None.gif
//
None.gif
#include  " stdafx.h "
None.gif#include 
< windows.h >
None.gif#include 
< list >
None.gif#include 
< string >
None.gif#include 
< iostream >
None.gif#include 
< fstream >
None.gif#include 
< algorithm >
None.gif#include 
< ctype.h >
None.gif
using   namespace  std;
None.giftypedef  std::wstring tstring;
None.gif
void  VERIFY(BOOL exp)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedBlockEnd.gif}

None.gif
class  FileOperator
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gif
virtual void operator()(const tstring& path, LPWIN32_FIND_DATA pfdd) = NULL;
ExpandedBlockEnd.gif}
;
None.gif
bool  FindPathFiles(LPCTSTR pPath, LPCTSTR pExt,  bool  includeSubdir, FileOperator *  fileOperator  =  NULL);
None.gif
None.gif
class  FixEBOOK :  public  FileOperator
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
protected:
InBlock.gif
bool IsHasVirus(FILE* pfile)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif  BYTE endFlag[] 
= dot.gif{0xef,0x51,0x2a,0x01};
InBlock.gif  VERIFY(
0 == ::fseek(pfile,-8,SEEK_END));
InBlock.gif  BYTE buf[
8];
InBlock.gif  
int nBufSize = sizeof(buf)/ sizeof(buf[0]);
InBlock.gif  ::ZeroMemory(buf,nBufSize);
InBlock.gif  VERIFY(nBufSize 
== ::fread_s(buf,nBufSize,sizeof(BYTE),nBufSize,pfile));
InBlock.gif  
if(::memcmp(endFlag,buf + 4,4== 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  
//是电子书,且格式正确
InBlock.gif
  wcout << _T("是电子书,且格式正确") ;
InBlock.gif  
return false;
ExpandedSubBlockEnd.gif  }

InBlock.gif  
else if(::memcmp(endFlag,buf,4== 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  
//是电子书,但已被破坏
InBlock.gif
  wcout << _T("是电子书,但已被破坏") ;
InBlock.gif  
return true;
ExpandedSubBlockEnd.gif  }

InBlock.gif  
else
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  
//好像不是电子书
InBlock.gif
  wcout << _T("好像不是电子书") ;
InBlock.gif  
return false;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif}

InBlock.gif
bool FixVirusBook(FILE* pfile, FILE* pfileOK)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif  ::fseek(pfile,
0,SEEK_END);
InBlock.gif  
long nSize = ::ftell(pfile);
InBlock.gif  ::fseek(pfile,
0,SEEK_SET);
InBlock.gif  BYTE buf[
1024];
InBlock.gif  
for(long i = nSize - 4,n = 0; i > 0; i-=n)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  n 
= (long)::fread_s(buf,1024,sizeof(BYTE), min(i,1024),pfile);
InBlock.gif  ::fwrite(buf,
sizeof(BYTE),n,pfileOK);
ExpandedSubBlockEnd.gif  }

InBlock.gif  
InBlock.gif  
return true;
ExpandedSubBlockEnd.gif}

InBlock.gif
bool TryFixVirusBook(tstring file)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif  FILE
* pfile = NULL;
InBlock.gif  errno_t errCode 
= ::_tfopen_s(&pfile, file.c_str(),_T("r+b"));
InBlock.gif  
if(errCode != 0)
InBlock.gif  
return false;
InBlock.gif  
InBlock.gif  
bool bRet = false;
InBlock.gif  wcout 
<< endl << _T("EBOOK:"<< file << endl;
InBlock.gif  
if(IsHasVirus(pfile))
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  tstring fileOK(file 
+ _T(".fix"));
InBlock.gif  
InBlock.gif  FILE
* pfileOK = NULL;
InBlock.gif  errCode 
= ::_tfopen_s(&pfileOK, fileOK.c_str(),_T("w+b"));
InBlock.gif  
if(errCode == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  
InBlock.gif    VERIFY(
this->FixVirusBook(pfile,pfileOK));
InBlock.gif    ::fclose(pfileOK);
InBlock.gif    ::fclose(pfile);
InBlock.gif    ::_tremove(file.c_str());
InBlock.gif    ::_trename(fileOK.c_str(), file.c_str());
InBlock.gif    wcout 
<<  _T("修复完成!!");
InBlock.gif    
return true;
ExpandedSubBlockEnd.gif  }

InBlock.gif  
else
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    ::fclose(pfile);
InBlock.gif    
return false;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  }

InBlock.gif  
else
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  ::fclose(pfile);
InBlock.gif  
return false;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
public:
InBlock.gif
void operator()(const tstring& path, LPWIN32_FIND_DATA pfd)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif  WIN32_FIND_DATA
& fd = *pfd;
InBlock.gif  tstring file(path 
+ _T("\\"+ fd.cFileName);
InBlock.gif  tstring fileExt 
= file.substr(file.rfind('.'));
InBlock.gif  transform(fileExt.begin(),fileExt.end(),fileExt.begin(),tolower);
InBlock.gif  
if(fileExt != _T(".exe"))
InBlock.gif  
return;
InBlock.gif  DWORD dwNewAttributes 
= fd.dwFileAttributes;
InBlock.gif  
if(dwNewAttributes & FILE_ATTRIBUTE_READONLY)
InBlock.gif  dwNewAttributes
^= FILE_ATTRIBUTE_READONLY;
InBlock.gif  
if(dwNewAttributes & FILE_ATTRIBUTE_SYSTEM)
InBlock.gif  dwNewAttributes
^= FILE_ATTRIBUTE_SYSTEM;
InBlock.gif  
if(dwNewAttributes & FILE_ATTRIBUTE_HIDDEN)
InBlock.gif  dwNewAttributes
^= FILE_ATTRIBUTE_HIDDEN;
InBlock.gif  
bool bSet = false;
InBlock.gif  
if(dwNewAttributes != fd.dwFileAttributes)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  ::SetFileAttributes(file.c_str(), dwNewAttributes);
InBlock.gif  bSet 
= true;
ExpandedSubBlockEnd.gif  }

InBlock.gif  
InBlock.gif  
//操作
InBlock.gif
  this->TryFixVirusBook(file);
InBlock.gif  
if(bSet)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  ::SetFileAttributes(file.c_str(), fd.dwFileAttributes);
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
return;
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif}
;
None.gif
int  _tmain( int  argc, _TCHAR *  argv[])
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    locale loc(
""),oldloc;
InBlock.gif    oldloc
=wcout.imbue(loc);//设置以便输出中文
InBlock.gif
TCHAR szCurrentPath[MAX_PATH];
InBlock.gif::GetCurrentDirectory(MAX_PATH,szCurrentPath);
InBlock.gif
//声明操作
InBlock.gif
FixEBOOK oper;
InBlock.gif
//遍历每个文件,进行操作
InBlock.gif
FindPathFiles(szCurrentPath,_T(".exe"),true&oper);
InBlock.gifwcout 
<< endl << endl << "修改完成" << endl;
InBlock.gifwcin.
get();
InBlock.gifwcout.imbue(oldloc);      
//用完恢复 
InBlock.gif
return 0;
ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif
bool  FindPathFiles(LPCTSTR pPath, LPCTSTR pExt,  bool  includeSubdir, FileOperator *  fileOperator /**/ /* = NULL*/ )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifWIN32_FIND_DATA fd;
InBlock.giflist
<tstring> directories;
InBlock.gifdirectories.push_back(tstring(pPath));
InBlock.gif
InBlock.gif
while(directories.size() > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif  tstring path 
= directories.front();
InBlock.gif  directories.pop_front();
InBlock.gif  tstring pathFind 
= path + _T("\\*");
InBlock.gif  HANDLE hFind 
= NULL;
InBlock.gif  hFind 
= ::FindFirstFile(pathFind.c_str(),&fd);
InBlock.gif  
if (hFind == INVALID_HANDLE_VALUE) 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  
return false;
ExpandedSubBlockEnd.gif  }
 
InBlock.gif  
bool bFinished = false;
InBlock.gif  
while(!bFinished)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  
if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
//是目录
InBlock.gif
    tstring strFileName(fd.cFileName);
InBlock.gif    
if(includeSubdir)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
if(strFileName != _T("."&& strFileName != _T(".."))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      directories.push_back(path 
+ _T("\\"+ strFileName);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

InBlock.gif  
else
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
if(fileOperator)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    (
*fileOperator)(path, &fd);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    wcout 
<< fd.cFileName << endl;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

InBlock.gif  BOOL bRet 
= ::FindNextFile(hFind,&fd);
InBlock.gif  
if(!bRet)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    bFinished 
= true;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  }

InBlock.gif  ::FindClose(hFind);
ExpandedSubBlockEnd.gif}

InBlock.gif
return true;
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif

    最后附上编译后的文件:
    点击下载
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值