📄 secmodule.cpp
字号:
#include "windows.h" // DWORD
#include "secmodule.h"
#include "cvar.h"
#include "client.h" // getogcdirfile
#include <fstream>
using namespace std;
#undef SHA
////================================================================================
//void secExitHL()
//{
// gEngfuncs.pfnClientCmd("speak \"message from system _comma _comma invallid security located\"");
// Sleep(3000);
// gEngfuncs.pfnClientCmd("quit");
//// MessageBox(0,"OGC Hook will now shut down HL for security reasons\n"// "This check can be overridden in startup.cfg, however\n"// "this is NOT RECOMMENDED, as it makes you detectable.",// "Security module changed.", MB_ICONEXCLAMATION);// ExitProcess(-2);
//}
//================================================================================
bool validate_sec_module(char* base, int len)
{
if(len>450000) len=100000;
if(cvar.sec_check && len!=cvar.sec_size && !cvar.sec_dump) return false;
SHA blub;
blub.reset();
blub.feed((uint8_t*)base,len);
blub.finish();
if(cvar.sec_dump)
{
ofstream binfile (getOgcDirFile("secmodule.bin").c_str(),ios::binary);
ofstream hashfile (getOgcDirFile("secmodule.txt").c_str(),ios::app);
binfile.write(base,len);
hashfile<<"module: len="<<len<<" hash1="<<blub.hashes[0]<<" hash2="<<blub.hashes[3]<<endl;
}
if(cvar.sec_check && (blub.hashes[0]!=cvar.sec_hash1 || blub.hashes[3]!=cvar.sec_hash2))
{
return false;
}
return true;
}
//================================================================================
//================================================================================
//================================================================================
//======================= S H A =============================================
//================================================================================
//================================================================================
//================================================================================
/*
* Define the SHA1 circular left shift macro
*/
#define SHA1CircularShift(bits,word) \
(((word) << (bits)) | ((word) >> (32-(bits))))
/*
* SHA1Reset
*
* Description:
* This function will initialize the hashes in preparation
* for computing new SHA1 hashes.
*
*/
void SHA::reset()
{
Length_Low = 0;
Length_High = 0;
Message_Block_Index = 0;
hashes[0] = 0x67452301;
hashes[1] = 0xEFCDAB89;
hashes[2] = 0x98BADCFE;
hashes[3] = 0x10325476;
hashes[4] = 0xC3D2E1F0;
Computed = 0;
Corrupted = 0;
}
/*
* SHA::finish()
*
* Description:
* This function will return the 160-bit message digest into the
* Message_Digest array provided by the caller.
* NOTE: The first octet of hash is stored in the 0th element,
* the last octet of hash in the 19th element.
*
*
*/
#define flipbytes(a) a = ((a<<24)+(a>>24)+((a>>8)&0xFF00)+((a<<8)&0xFF0000));
void SHA::finish()
{
assert(!Corrupted);
if (!Computed)
{
padMessage();
Length_Low = 0; /* and clear length */
Length_High = 0;
Computed = 1;
}
flipbytes(hashes[0]);
flipbytes(hashes[1]);
flipbytes(hashes[2]);
flipbytes(hashes[3]);
flipbytes(hashes[4]);
}
/*
* SHA::feed
*
* Description:
* This function accepts an array of octets as the next portion
* of the message.
*
* Parameters:
* message_array: [in]
* An array of characters representing the next portion of
* the message.
* length: [in]
* The length of the message in message_array
*
* Returns:
* sha Error Code.
*
*/
void SHA::feed( const uint8_t *message_array, unsigned int length)
{
if (!length) return;
assert(!Computed && !Corrupted);
while(length-- && !Corrupted)
{
Message_Block[Message_Block_Index++] =
(*message_array & 0xFF);
Length_Low += 8;
if (Length_Low == 0)
{
Length_High++;
if (Length_High == 0)
{
/* Message is too long */
Corrupted = 1;
}
}
if (Message_Block_Index == 64)
{
processMessageBlock();
}
message_array++;
}
}
/*
* SHA::processMessageBlock
*
* Description:
* This function will process the next 512 bits of the message
* stored in the Message_Block array.
*
* Parameters:
* None.
*
* Returns:
* Nothing.
*
* Comments:
* Many of the variable names in this code, especially the
* single character names, were used because those were the
* names used in the publication.
*
*
*/
void SHA::processMessageBlock()
{
const uint32_t K[] = { /* Constants defined in SHA-1 */
0x5A827999,
0x6ED9EBA1,
0x8F1BBCDC,
0xCA62C1D6
};
int t; /* Loop counter */
uint32_t temp; /* Temporary word value */
uint32_t W[80]; /* Word sequence */
uint32_t A, B, C, D, E; /* Word buffers */
/*
* Initialize the first 16 words in the array W
*/
for(t = 0; t < 16; t++)
{
W[t] = Message_Block[t * 4] << 24;
W[t] |= Message_Block[t * 4 + 1] << 16;
W[t] |= Message_Block[t * 4 + 2] << 8;
W[t] |= Message_Block[t * 4 + 3];
}
for(t = 16; t < 80; t++)
{
W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
}
A = hashes[0];
B = hashes[1];
C = hashes[2];
D = hashes[3];
E = hashes[4];
for(t = 0; t < 20; t++)
{
temp = SHA1CircularShift(5,A) +
((B & C) | ((~B) & D)) + E + W[t] + K[0];
E = D;
D = C;
C = SHA1CircularShift(30,B);
B = A;
A = temp;
}
for(t = 20; t < 40; t++)
{
temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
E = D;
D = C;
C = SHA1CircularShift(30,B);
B = A;
A = temp;
}
for(t = 40; t < 60; t++)
{
temp = SHA1CircularShift(5,A) +
((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
E = D;
D = C;
C = SHA1CircularShift(30,B);
B = A;
A = temp;
}
for(t = 60; t < 80; t++)
{
temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
E = D;
D = C;
C = SHA1CircularShift(30,B);
B = A;
A = temp;
}
hashes[0] += A;
hashes[1] += B;
hashes[2] += C;
hashes[3] += D;
hashes[4] += E;
Message_Block_Index = 0;
}
/*
* SHA::padMessage
*
* Description:
* According to the standard, the message must be padded to an even
* 512 bits. The first padding bit must be a '1'. The last 64
* bits represent the length of the original message. All bits in
* between should be 0. This function will pad the message
* according to those rules by filling the Message_Block array
* accordingly. It will also call the ProcessMessageBlock function
* provided appropriately. When it returns, it can be assumed that
* the message digest has been computed.
*
*
*/
void SHA::padMessage()
{
/*
* Check to see if the current message block is too small to hold
* the initial padding bits and length. If so, we will pad the
* block, process it, and then continue padding into a second
* block.
*/
if (Message_Block_Index > 55)
{
Message_Block[Message_Block_Index++] = 0x80;
while(Message_Block_Index < 64)
{
Message_Block[Message_Block_Index++] = 0;
}
processMessageBlock();
while(Message_Block_Index < 56)
{
Message_Block[Message_Block_Index++] = 0;
}
}
else
{
Message_Block[Message_Block_Index++] = 0x80;
while(Message_Block_Index < 56)
{
Message_Block[Message_Block_Index++] = 0;
}
}
/*
* Store the message length as the last 8 octets
*/
#pragma warning (disable:4244)
Message_Block[56] = Length_High >> 24;
Message_Block[57] = Length_High >> 16;
Message_Block[58] = Length_High >> 8;
Message_Block[59] = Length_High;
Message_Block[60] = Length_Low >> 24;
Message_Block[61] = Length_Low >> 16;
Message_Block[62] = Length_Low >> 8;
Message_Block[63] = Length_Low;
processMessageBlock();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -