📄 allmycode.cpp
字号:
delete[] backupCode;
delete[] jumpCode;
return 0xff;
}
//需要用到的一些自定义函数
inline BOOL IsemptySrt(LPTSTR Str)//empty return true测试字符串是否为空
{
CString StrTmp;
StrTmp.Insert(0, Str );
if( !StrTmp.IsEmpty() )
return false;
return true;
}
//改变指定窗口标题
inline void ChangeWindowTitle(HWND hwnd, LPTSTR DEF_Title,LPTSTR CG_Str )//改变成功 返回 TRUE 否则返回错误代码
{
if( DEF_Title == NULL )
{
::SetWindowText( hwnd, CG_Str);
}
LPTSTR Title_Tmp = new TCHAR[(_tcslen(DEF_Title)+_tcslen(CG_Str)+_tcslen(_T(" - ")) +1 )];
_stprintf_s( Title_Tmp, -1, _T("%s - %s"), DEF_Title, CG_Str);
SetWindowText( hwnd, Title_Tmp);
delete [] Title_Tmp;
}
//-------字 转换函数-----------------转换成功返回 TRUE 失败返回 FALSE
inline BOOL WcharToChar( LPTSTR lpWchar, LPSTR lpStr )
{
if(lpStr == NULL)
return ::WideCharToMultiByte(CP_ACP, 0, lpWchar, -1, NULL, 0, NULL, NULL);
int Str_len = ::WideCharToMultiByte(CP_ACP, 0, lpWchar, -1, NULL, 0, NULL, NULL);
LPSTR lpStr_tmp = new char[(Str_len+1)];
if( !::WideCharToMultiByte(CP_ACP, 0, lpWchar, -1, lpStr_tmp, Str_len+1, NULL, NULL) )
{
delete [] lpStr_tmp;
return false;
}
strcpy_s(lpStr, -1, lpStr_tmp);
delete [] lpStr_tmp;
return true;
}
inline BOOL CharToWchar(LPTSTR lpWchar, LPSTR lpStr)
{
if(lpWchar == NULL)
return ::MultiByteToWideChar( CP_ACP, 0, lpStr, -1, NULL, 0);
int Wchar_len = ::MultiByteToWideChar( CP_ACP, 0, lpStr, -1, NULL, 0);
LPTSTR lpWchar_tmp = new TCHAR[(Wchar_len + 1 )];
if( !::MultiByteToWideChar( CP_ACP, 0, lpStr, -1, lpWchar_tmp, Wchar_len+1) )
{
delete [] lpWchar_tmp;
return false;
}
wcscpy_s( lpWchar , -1 , lpWchar_tmp );
delete [] lpWchar_tmp;
return true;
}
///数字 字符串 转换成 DWORD
inline DWORD StringToDword( LPTSTR num_Str )
{
#ifdef _UNICODE
LPSTR num_Strtmp = NULL;
num_Strtmp = new char[( WcharToChar(num_Str, num_Strtmp) +1 )];
WcharToChar(num_Str, num_Strtmp);
int number = atoi(num_Strtmp);
delete [] num_Strtmp;
return number;
#else
return atoi(num_Str);
#endif
}
//获得本地IP
BOOL LocalHostToIP(CString* HostIp)
{
WORD wVersionRequested;
WSADATA wsaData;
CHAR bHostName[255];
TCHAR tHostIp[255];
HOSTENT* HostEnt;
wVersionRequested = MAKEWORD(2 , 2);
int err = WSAStartup(wVersionRequested , &wsaData );
if( err != 0 )
{
return false;
}
try
{
::ZeroMemory(bHostName,255);
if(::gethostname( bHostName, 255) == SOCKET_ERROR)
return false;
HostEnt = gethostbyname ( bHostName );
if( HostEnt != NULL)
{
for(int Num = 0 ;;Num++ )
{
char* bHostIp = inet_ntoa( *( struct in_addr * )HostEnt->h_addr_list[Num] );
::ZeroMemory(tHostIp,255);
CharToWchar( tHostIp, bHostIp);
HostIp->Format(_T("%s"), tHostIp );
if( (HostEnt->h_addr_list[Num] + HostEnt->h_length) >= HostEnt->h_name )
break;
}
return true;
}
}
catch(_com_error& e)
{
CString ErrorMessage;
ErrorMessage.Format( _T("操作异常!\r\n错误信息%s"), e.ErrorMessage());
MessageBox(NULL, ErrorMessage,_T("错误"),NULL );
}
WSACleanup();
return false;
}
//解析域名到IP
BOOL HostToIP(CString Name,CString* HostIp)
{
WORD wVersionRequested;
WSADATA wsaData;
CHAR bHostName[255];
TCHAR tHostIp[255];
HOSTENT* HostEnt;
wVersionRequested = MAKEWORD(2 , 2);
int err = WSAStartup(wVersionRequested , &wsaData );
if( err != 0 )
{
return false;
}
try
{
::ZeroMemory(bHostName,255);
WcharToChar( Name.AllocSysString(), bHostName);
HostEnt = gethostbyname ( bHostName );
if( HostEnt != NULL)
{
for(int Num = 0 ;;Num++ )
{
char* bHostIp = inet_ntoa( *( struct in_addr * )HostEnt->h_addr_list[Num] );
::ZeroMemory(tHostIp,255);
CharToWchar( tHostIp, bHostIp);
HostIp->Format(_T("%s"), tHostIp );
if( (HostEnt->h_addr_list[Num] + HostEnt->h_length) >= HostEnt->h_name )
break;
}
return true;
}
}
catch(_com_error& e)
{
CString ErrorMessage;
ErrorMessage.Format( _T("操作异常!\r\n错误信息%s"), e.ErrorMessage());
MessageBox(NULL, ErrorMessage,_T("错误"),NULL );
}
WSACleanup();
return false;
}
//插入代码并加入自己的代码获得指定值存入EBX
//--------------------插入地址------------------覆盖代码长度---------------自定义函数地址/名------代码指针------代码长度-----
DWORD DetourMyCode( DWORD targetCodeAddress, DWORD targetCodeLength, DWORD ourFunctionAddress ,BYTE* lpCode , DWORD CodeLength)
{
// 目标处代码长度小于 5 则太短不足以填充跳转代码
if ( targetCodeLength < 5 )
return 0x01;//代码太小
//
// 修改权限, 分配相关资源
//
DWORD oldAttr = 0, tmpAttr = 0, tmpIdx = 0;
// 修改目标代码区权限为可执行可读写
if ( !VirtualProtect( (void *)(DWORD_PTR)targetCodeAddress, targetCodeLength, PAGE_EXECUTE_READWRITE, &oldAttr ) )
{
MessageBox(NULL, _T("内存权限设置失败!"),_T("提示"),NULL);
return 0x02;
}
BYTE *backupCode;
// 在堆上分配内存用以备份原目标代码
backupCode = new BYTE[ targetCodeLength ];
if ( backupCode == NULL )
return 0x03;
// 在堆上分配内存用以存放构造的跳转代码
BYTE *jumpCode = new BYTE[ targetCodeLength ];
if ( jumpCode == NULL )
{
delete[] backupCode;
return 0x04;
}
// 在堆上分配内存用以存放构造的"门"代码, 并修改其权限为可执行可读写
DWORD GateWayCode_Len = targetCodeLength+ CodeLength + 12;
BYTE *gatewayCode = new BYTE[ GateWayCode_Len ];
if ( gatewayCode == NULL )
{
delete[] backupCode;
delete[] jumpCode;
return 0x05;
}
if ( !VirtualProtect( (void *)gatewayCode, GateWayCode_Len , PAGE_EXECUTE_READWRITE, &tmpAttr ) )
{
delete[] backupCode;
delete[] jumpCode;
delete[] gatewayCode;
return 0x06;
}
//
// 备份原目标代码
//
memcpy( (void *)backupCode, (const void *)(DWORD_PTR)targetCodeAddress, targetCodeLength );
//
// 构造跳转代码, 覆盖到目标处代码处, 使其转到我们构造的"门"代码处
//
jumpCode[0] = 0xE9;//E8 CALL E9 JMP,原始E8
*(PDWORD)( jumpCode + 1 ) = \
(DWORD)(DWORD_PTR)(&(*gatewayCode)) - targetCodeAddress - 5;
// 将多余字节填充为 nop
DWORD tmp = 5;
while ( tmp < targetCodeLength )
jumpCode[ tmp++ ] = 0x90;
memcpy( (void *)(DWORD_PTR)targetCodeAddress, (const void *)jumpCode, targetCodeLength );
//
// 构造"门"代码, 维护宿主程序现场并调用 ourFuncitonAddr
//
DWORD CodePosition = 0;
gatewayCode[CodePosition] = 0x60;
for( CodePosition++ ;CodePosition <= CodeLength ;CodePosition++)
{
gatewayCode[CodePosition] = lpCode[ CodePosition-1 ];
}
gatewayCode[CodePosition] = 0xE8;
*(PDWORD)(gatewayCode+CodePosition+1) = ourFunctionAddress - (DWORD)(DWORD_PTR)(&(*gatewayCode)) - 1 - 1 - 4 - CodeLength;
gatewayCode[CodePosition+5] = 0x61;
memcpy( (void *)(gatewayCode+CodePosition+6), backupCode, targetCodeLength );
gatewayCode[ CodePosition + 6 + targetCodeLength ] = 0xE9;
*(PDWORD)( gatewayCode + CodePosition + 6 + targetCodeLength + 1 )
= targetCodeAddress + targetCodeLength - (DWORD)(DWORD_PTR)(&(*gatewayCode)) - 7 - targetCodeLength - 1 - 4 - CodeLength;
//
// 恢复权限, 释放相关资源
//
// 注意, 这里释放 backupCode, jumpCode, 不释放 gatewayCode
// gatewayCode 在我们的目的上生命期相当于静态变量, 所以不用删除, 程
// 序退出时操作系统会将其释放
//
VirtualProtect( (void *)(DWORD_PTR)targetCodeAddress, targetCodeLength, oldAttr, &tmpAttr );
delete[] backupCode;
delete[] jumpCode;
return 0xff;
}
/*
在IP地址的主要三种类型里,各保留了三个区域作为私有地址,其地址范围如下:
A类地址:10.0.0.0~10.255.255.255
B类地址:172.16.0.0~172.31.255.255
C类地址:192.168.0.0~192.168.255.255
*/
inline BOOL IsPrivateIP( CString InComeIP)
{
int PrivateIP_First[] = { 10,172,192 };
int PrivateIP_Second[] = { 16,31,168};
char strInComeIP[16] = {0}, strInComeIP_First[4]={0},strInComeIP_Second[4]={0};
InComeIP.Trim();
if( InComeIP.Compare( _T("127.0.0.1") ) == NULL )//相同
return true;
else if( WcharToChar( InComeIP.AllocSysString() , strInComeIP))
{
size_t First_Len = strstr( strInComeIP, ".") - strInComeIP;
::ZeroMemory( strInComeIP_First, 4);
strncpy_s( strInComeIP_First, -1, strInComeIP,First_Len);
size_t Second_Len = strstr( strstr( strInComeIP, ".")+1, ".") - (strstr( strInComeIP, ".")+1);
::ZeroMemory( strInComeIP_Second, 4);
strncpy_s( strInComeIP_Second, -1, strstr( strInComeIP, ".")+1 ,Second_Len);
int FirstNum = atoi( strInComeIP_First );
int SecindNum = atoi( strInComeIP_Second );
for(int i=0;i < 3 ; i++)
{
if( PrivateIP_First[i] == FirstNum)
{
if( i == 0 )
return true;
else if( i == 1 )
{
if( PrivateIP_Second[ i-1 ] <= SecindNum && PrivateIP_Second[ i ] >= SecindNum)
return true;
}
else
{
if( PrivateIP_Second[ i ] == SecindNum )
return true;
}
}
}
}
return false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -