Search for the function that locates the virus pattern in the LIB/OBJ file of the static library. C/C ++ source code

Source: Internet
Author: User

Author: Zhuang Xiaoli (liigo), 2010/7/12

This article first address: aspx "> http://blog.csdn.net/liigo/archive/2010/07/12/5727859.aspx

Reprinted please indicate the source: http://blog.csdn.net/liigo

 

Purpose of this article: in a specified LIB or OBJ file, search for a piece of executable code (X86 Instruction Set) and determine the function to which it belongs.

Reason: assume that the software we write is falsely reported as a virus by the antivirus software, and that we have learned some code (X86 Instruction Set) that is regarded as a virus pattern in the software (EXE) through some means) (For details, see the previous blog of liigo.) Let's assume that we have learned through some means that this code comes from a LIB/OBJ file during the compilation link process (see my previous blog ), next, we need to search for the location signature in this LIB/OBJ file to determine which function it comes from. This is what we will discuss in this article.

Basic Idea: parse the binary format of the LIB file (for the basic structure of LIB/OBJ, refer to a blog post I (liigo) and traverse all OBJ In the LIB file, traverse all sections in each OBJ and search for the pattern in RawData of the Section. If it is found, print the Section) according to the function Symbol and the offset, you can determine the function of the signature. Parsing the binary format of LIB files is not covered in this article. The main code framework is as follows:

// Search in the LIB File
CLibInfo libinfo;
If (! Libinfo. LoadCLibFile (szLibFile ))
{
Printf ("Can not load the lib file: % s", szLibFile );
Return 0;
}
CObjInfo * pObj = NULL;
For (unsigned int objIndex = 0; objIndex <libinfo. m_NumberOfMembers; objIndex ++)
{
PObj = libinfo. m_pObjs [objIndex];

COFF_SectionHeader * pseheader header = pObj-> m_SectionHeaders;
For (int sectionNumber = 1; sectionNumber <= pObj-> m_pCoffHeader-> NumberOfSections; sectionNumber ++, pseheader header ++)
{
Void * pserawrawdata = pObj-> m_pCoffData + pseheader header-> PointerToRawData;
Int lenSectionRawData = pseheader header-> SizeOfRawData;
// Search in section raw data
Int matchedRate = 0, matchOffset = 0;
MatchOffset = searchdata (unsigned char *) pserawdata, lenSectionRawData, (unsigned char *) signatureMem. GetData (), signatureMem. GetDataSize (), ignoreByte, minMatchRate, matchedRate );
If (matchOffset> = 0)
{
Int libFileOffset = pObj-> m_pCoffData + pseheader header-> PointerToRawData-libinfo. m_pCLibData + matchOffset;
Printf ("in obj # % d (% s), match % d % (at section offset % d, lib offset % d) in section # % d (% s ), which contains symbol (s ):",
ObjIndex + 1, pObj-> m_szFileName, matchedRate, matchOffset, libFileOffset, sectionNumber, pObj-> GetSectionName (pseheader header ));
PrintSymbolNameInSection (pObj, sectionNumber );
Printf ("matched data :");
PrintDataBytes (unsigned char *) pserawrawdata + matchOffset, signatureMem. GetDataSize ());
Printf ("");
}
}
}

In the code above, once a Section matches a signature, the sequence number and name of the Section, and the sequence number and name of the obj to which the Section belongs are output, the offset of the matched data in the Section and the offset in the file. Note that you must be familiar with the internal LIB/OBJ format before calculating the "libFileOffset" in the file.

The following code outputs the symbolic information in a specified Section. Note that you must filter out the auxiliary Symbols (Aux Symbols). In fact, you can also filter out the Symbols of the Section and other irrelevant Symbols. The output information includes the symbol name, whether it is a function, and the offset (suspect) of the symbol data in the section. It is sufficient for us to determine the function to which the signature belongs. The LIB/OBJ file generated by MicroSoft Visual C ++ compilers. Generally, each section contains only one function definition, making it easier to make judgments.

Void printSymbolNameInSection (CObjInfo * pObj, int sectionNumber)
{
COFF_Symbol * pSymbol = pObj-> m_Symbols;
For (unsigned int symIndex = 0; symIndex <pObj-> m_pCoffHeader-> NumberOfSymbols; symIndex ++, pSymbol ++)
{
Const char * szSymbolName = pObj-> GetSymbolName (pSymbol );

If (pSymbol-> SectionNumber = sectionNumber)
{
Printf ("% s, section offset % d", szSymbolName, (pSymbol-> Type = 0x20? "()": ""), PSymbol-> Value );
}

SymIndex + = pSymbol-> NumberOfAuxSymbols;
PSymbol + = pSymbol-> NumberOfAuxSymbols;
}
}

When retrieving the location signature, liigo introduced the minMatchRate and ignoreByte values to be ignored when calculating the matching rate, some X86 commands (such as the E8 command and call xxx) have the opposite address or the address to be relocated, which may not be exactly the same in EXE and LIB/OBJ. The Code is as follows:

Bool matchdata (unsigned char * returns archfrom, unsigned char * returns archwhat, int lenSearchWhat,
Unsigned char ignoreByte, int minMatchRate, int & matchedRate)
{
Int matchtimes = 0, matchtimesAll = lenSearchWhat;
For (int I = 0; I <lenSearchWhat; I ++)
{
If (distinct archwhat [I] = ignoreByte)
{
MatchtimesAll --;
}
Else
{
If (returns archwhat [I] = returns archfrom [I])
Matchtimes ++;
}
}

Int rate = (matchtimes * 100/matchtimesAll );
If (rate> minMatchRate)
{
MatchedRate = rate;
Return true;
}
Else
Return false;
}

// If searched, return the offset; if not searched, return-1
Int searchdata (unsigned char * returns archfrom, int lenSearchFrom, unsigned char * returns archwhat, int lenSearchWhat,
Unsigned char ignoreByte, int minMatchRate, int & matchedRate)
{
For (int I = 0; I <lenSearchFrom-lenSearchWhat + 1; I ++)
{
If (matchdata (pSearchFrom + I, pSearchWhat, lenSearchWhat, ignoreByte, minMatchRate, matchedRate ))
{
Return I;
}
}
Return-1;
}

In addition, we allow users to enter hexadecimal text data, such as "FF7424 10 E8 00 00 00 00 C2 1000 ", the program needs to convert the data to binary data in the memory, convert each two letters into a byte value, and process the characters such as spaces:

Bool HexText2Mem (char * szSignature, BufferedMem & mem)
{
Int len = strlen (szSignature );
Char firstchar =;
For (int I = 0; I <len; I ++)
{
Char c = szSignature [I];

If (c = | c = ,)
{
If (firstchar)
Mem. AppendByte (hexchar2decimal (firstchar ));
Firstchar =;
Continue;
}

Bool isLetterChar = (c> = A & c <= F) | (c> = a & c <= f ));
Bool isNumChar = (c >=0 & c <= 9 );
If (! IsLetterChar &&! IsNumChar)
{
SzSignature [I + 1] =;
Printf ("error in hexadecimal text of signature data, the printed last char is invalid: % s", szSignature );
Return false;
}

If (firstchar =)
{
Firstchar = c;
}
Else
{
Mem. AppendByte (hexchar2decimal (firstchar) * 16 + hexchar2decimal (c ));
Firstchar =;
}
}

Return true;
}

Int hexchar2decimal (char c)
{
If (c> = 0 & c <= 9)
Return (c-0 );
Else if (c> = A & c <= F)
Return (c-A + 10 );
Else if (c> = a & c <= f)
Return (c-a + 10 );
Else
Return 0;
}

The final running result of the program is as follows. The search result is consistent with the search result (figure) obtained in the previous article in easy language (compare the searched signature file offset and matching rate ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.