程序流程如下:
- 记录下正在运行的IIS相关服务(这样可以避免在步骤3启动原本服务器上并未开启的服务)
- 停止上述服务
- 开启上述服务
可以在步骤2和步骤3之间做一些事情。
调用GetTempFileName获取临时文件名的时候,GetTempFileName函数内部会自动创建这个文件。如果是调用MoveFile将文件移动到这个位置就要注意了,因为MoveFile的目的路径已经存在文件了,移动会失败。解决方法可是先DeleteFile删除文件再MoveFile移动文件,或者使用直接MoveFileEx并指定MOVEFILE_REPLACE_EXISTING参数。
#include <Windows.h>
#include <iostream>
#include "detours/include/detours.h"
#ifdef _WIN64
#pragma comment(lib,"detours/lib.X64/detours.lib")
#else
#pragma comment(lib,"detours/lib.X86/detours.lib")
#endif
INT
WINAPI
MsgBox(
_In_opt_ HWND hWnd,
_In_opt_ LPCSTR lpText,
_In_opt_ LPCSTR lpCaption,
_In_ UINT uType)
{
return MessageBoxW(NULL, L"被HOOK了", L"", 0);
}
PVOID fpRegisterModule;
bool Hook(bool restore)
{
if (restore == false)
DetourRestoreAfterWith();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
LONG result;
if (restore)
result = DetourDetach(reinterpret_cast<void**>(&fpRegisterModule), MsgBox);
else
result = DetourAttach(reinterpret_cast<void**>(&fpRegisterModule), MsgBox);
DetourTransactionCommit();
return true;
}
int main(int argc, char* argv[])
{
HMODULE hMod = LoadLibraryA("user32.dll");
if (hMod == NULL)
{
return false;
}
fpRegisterModule = GetProcAddress(hMod, "MessageBoxA");
if (fpRegisterModule == nullptr)
return false;
Hook(false);
MessageBoxA(NULL, "没被HOOK", "没被HOOK", 0);
Hook(true);
MessageBoxA(NULL, "没被HOOK", "没被HOOK", 0);
return 1;
}
我在实际情况中发现使用关闭Windows服务的方式重启IIS是有一定概率失败的,很可能是由超时时间导致的。使用COM组件的方式重启IIS就没有这个问题,iisreset.exe使用的就是这个COM接口。
#include <windows.h>
#include <iisrsta.h>
const IID IID_IIisServiceControl =
{ 0x0E8FB8620, 0x588F, 0x11D2,
{ 0x9D, 0x61, 0x00, 0xC0, 0x4F, 0x79, 0xC5, 0xFE } };
const GUID CLSID_IisServiceControl =
{ 0x0E8FB8621, 0x588F, 0x11D2,
{ 0x9D, 0x61, 0x00, 0xC0, 0x4F, 0x79, 0xC5, 0xFE } };
int main(int argc, char* argv[])
{
HRESULT result;
int rc = -1;
IIisServiceControl* IisSrvCtrl = nullptr;
result = CoInitializeEx(0, 0);
if (result != S_OK)
return 1;
do
{
result = CoCreateInstance(CLSID_IisServiceControl,
nullptr,
CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER | CLSCTX_INPROC_SERVER,
IID_IIisServiceControl,
(LPVOID*)&IisSrvCtrl
);
if (result != S_OK)
{
rc = 2;
break;
}
//如果IIS服务本身就是关闭状态,调用Stop函数返回的也是S_OK
result = IisSrvCtrl->Stop(60 * 1000, TRUE);
if (result != S_OK)
{
MessageBoxA(NULL, "关闭服务失败", "", 0);
rc = 3;
break;
}
//
MessageBoxA(NULL, "成功关闭IIS服务", "", 0);
result = IisSrvCtrl->Start(60 * 1000);
if (S_OK != result)
{
MessageBoxA(NULL, "启动服务失败", "", 0);
rc = 4;
break;
}
rc = 0;
} while (0);
CoUninitialize();
return rc;
}
#include <Windows.h>
#pragma comment(lib,"Version.lib")
BOOLEAN CopyVersionInfo(LPCSTR SrcFile, LPCSTR DstFile)
{
BOOLEAN Rv = FALSE;
PVOID VersionInfo;
DWORD VersionInfoSize;
HANDLE hUpdate = NULL;
VersionInfoSize = GetFileVersionInfoSizeA(SrcFile, NULL);
if (VersionInfoSize == 0)
return FALSE;
VersionInfo = LocalAlloc(LPTR, VersionInfoSize);
if (VersionInfo == NULL)
return FALSE;
do
{
if (GetFileVersionInfoA(SrcFile, 0, VersionInfoSize, VersionInfo) == FALSE)
break;
hUpdate = BeginUpdateResourceA(DstFile, FALSE);
if (hUpdate == NULL)
break;
if (UpdateResourceA(hUpdate, RT_VERSION, (LPCSTR)1, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
VersionInfo, VersionInfoSize) == FALSE)
break;
if (EndUpdateResourceA(hUpdate, FALSE) == FALSE)
break;
hUpdate = NULL;
Rv = TRUE;
} while (0);
if (hUpdate)
EndUpdateResourceA(hUpdate, TRUE);
LocalFree(VersionInfo);
return Rv;
}
int main(int argc, char* argv[])
{
CopyVersionInfo("c:\\windows\\system32\\ntdll.dll", "D:\\muma.dll");
return 1;
}
方案一
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/transform_width.hpp>
bool Base64Encode(const std::string& input, std::string& output) {
typedef boost::archive::iterators::base64_from_binary<boost::archive::iterators::transform_width<std::string::const_iterator, 6, 8> > Base64EncodeIterator;
std::stringstream result;
std::copy(Base64EncodeIterator(input.begin()), Base64EncodeIterator(input.end()), std::ostream_iterator<char>(result));
size_t equal_count = (3 - input.length() % 3) % 3;
for (size_t i = 0; i < equal_count; i++) {
result.put('=');
}
output = result.str();
return output.empty() == false;
}
bool Base64Decode(const std::string& input, std::string& output) {
typedef boost::archive::iterators::transform_width<boost::archive::iterators::binary_from_base64<std::string::const_iterator>, 8, 6> Base64DecodeIterator;
std::stringstream result;
try {
std::copy(Base64DecodeIterator(input.begin()), Base64DecodeIterator(input.end()), std::ostream_iterator<char>(result));
}
catch (...) {
return false;
}
output = result.str();
return output.empty() == false;
}
方案一有问题,在使用Base64Decode解码的时候,如果输入源的末尾有“=”补位符,那么output的字符串的长度是不对的,不是实际字符串的长度,比实际的字符串长度要长(就是包含补位符的input的字符串应算出来的解码长度)。
方案二
#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/algorithm/string.hpp>
std::string decode64(const std::string &val) {
using namespace boost::archive::iterators;
using It = transform_width<binary_from_base64<std::string::const_iterator>, 8, 6>;
return boost::algorithm::trim_right_copy_if(std::string(It(std::begin(val)), It(std::end(val))), [](char c) {
return c == '\0';
});
}
std::string encode64(const std::string &val) {
using namespace boost::archive::iterators;
using It = base64_from_binary<transform_width<std::string::const_iterator, 6, 8>>;
auto tmp = std::string(It(std::begin(val)), It(std::end(val)));
return tmp.append((3 - val.size() % 3) % 3, '=');
}
方案二没有方案一的那个问题。
#include <windows.h>
BOOL EasyStartService(LPCSTR ServiceName)
{
SC_HANDLE hSCManager = NULL;
SC_HANDLE hService = NULL;
SERVICE_STATUS ServiceStatus;
BOOL Ret = FALSE;
do
{
hSCManager = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
if (hSCManager == NULL)
break;
hService = OpenServiceA(hSCManager, ServiceName, SERVICE_START | SERVICE_QUERY_STATUS);
if (hService == NULL)
break;
if (StartServiceA(hService, 0, NULL) == FALSE)
break;
do
{
if (QueryServiceStatus(hService, &ServiceStatus) == FALSE)
break;
if (ServiceStatus.dwCurrentState == SERVICE_RUNNING)
Ret = TRUE;
} while (ServiceStatus.dwCurrentState == SERVICE_START_PENDING);
} while (0);
if (hService)
CloseServiceHandle(hService);
if (hSCManager)
CloseServiceHandle(hSCManager);
return Ret;
}
BOOL EasyStopService(LPCSTR ServiceName)
{
SC_HANDLE hSCManager = NULL;
SC_HANDLE hService = NULL;
SERVICE_STATUS ServiceStatus;
BOOL Ret = FALSE;
do
{
hSCManager = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
if (hSCManager == NULL)
break;
hService = OpenServiceA(hSCManager, ServiceName, SERVICE_STOP | SERVICE_QUERY_STATUS);
if (hService == NULL)
break;
if (ControlService(hService, SERVICE_CONTROL_STOP, &ServiceStatus) == FALSE)
break;
if (ServiceStatus.dwCurrentState == SERVICE_STOPPED)
Ret = TRUE;
while (ServiceStatus.dwCurrentState == SERVICE_STOP_PENDING)
{
if (QueryServiceStatus(hService, &ServiceStatus) == FALSE)
break;
if (ServiceStatus.dwCurrentState == SERVICE_STOPPED)
Ret = TRUE;
}
} while (0);
if (hService)
CloseServiceHandle(hService);
if (hSCManager)
CloseServiceHandle(hSCManager);
return Ret;
}
VOID StopIIS()
{
EasyStopService("AppHostSvc");
EasyStopService("w3logsvc");
EasyStopService("W3SVC");
//必须最后结束下面这个服务,否则该服务结束不了
//WAS服务的显示名称是Windows Process Activation Service
EasyStopService("WAS");
}
VOID StartIIS()
{
EasyStartService("AppHostSvc");
EasyStartService("w3logsvc");
EasyStartService("W3SVC");
EasyStartService("WAS");
}
#include <Windows.h>
#include <tchar.h>
#include <stdio.h>
void ShowError(const char* pszText);
BOOL AesEncrypt(BYTE* pPassword, DWORD dwPasswordLength, BYTE* pData, LPDWORD dwDataLength, DWORD dwBufferLength);
BOOL AesDecrypt(BYTE* pPassword, DWORD dwPasswordLength, BYTE* pData, LPDWORD dwDataLength, DWORD dwBufferLength);
int main(int argc, _TCHAR* argv[])
{
BYTE pData[MAX_PATH] = { 0 };
DWORD dwDataLength = 0, dwBufferLength = MAX_PATH;
DWORD i = 0;
RtlZeroMemory(pData, dwBufferLength);
lstrcpyA((char*)pData, "Hello World !11111111111111111122222222222222222222");
dwDataLength = lstrlenA((char*)pData);
printf("Text[%d]\n", dwDataLength);
for (i = 0; i < dwDataLength; i++)
{
printf("%x ", pData[i]);
}
printf("\n\n");
//AES加密
AesEncrypt((BYTE*)"yxx", 3, pData, &dwDataLength, dwBufferLength);
printf("AES Encrypt[%d]\n", dwDataLength);
for (i = 0; i < dwDataLength; i++)
{
printf("%x ", pData[i]);
}
printf("\n\n");
//AES解密
AesDecrypt((BYTE*)"yxx", 3, pData, &dwDataLength, dwBufferLength);
printf("AES Decrypt[%d]\n", dwDataLength);
for (i = 0; i < dwDataLength; i++)
{
printf("%x ", pData[i]);
}
printf("\n\n");
system("pause");
return 0;
}
void ShowError(const char* pszText)
{
char szErr[MAX_PATH] = { 0 };
wsprintfA(szErr, "%s Error [%d]", pszText, GetLastError());
MessageBoxA(NULL, szErr, "ERROR", MB_OK | MB_ICONERROR);
}
//AES加密
BOOL AesEncrypt(BYTE* pPassword, DWORD dwPasswordLength, BYTE* pData, LPDWORD dwDataLength, DWORD dwBufferLength)
{
BOOL bRet = TRUE;
HCRYPTPROV hCryptprov = 0;
HCRYPTHASH hCryptHash = 0;
HCRYPTKEY hCryptKey = 0;
do
{
//获取CSP句柄
bRet = CryptAcquireContextA(&hCryptprov, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT);
if (FALSE == bRet)
{
ShowError("CryptAcquireContext");
break;
}
//创建HASH对象
bRet = CryptCreateHash(hCryptprov, CALG_MD5, 0, 0, &hCryptHash);
if (FALSE == bRet)
{
ShowError("CryptCreateHash");
break;
}
//对密钥进行HASH计算
bRet = CryptHashData(hCryptHash, pPassword, dwPasswordLength, 0);
if (FALSE == bRet)
{
ShowError("CryptHashData");
break;
}
//使用HASH来生成密钥
bRet = CryptDeriveKey(hCryptprov, CALG_AES_128, hCryptHash, CRYPT_EXPORTABLE, &hCryptKey);
if (FALSE == bRet)
{
ShowError("CryptDeriveKey");
break;
}
bRet = CryptEncrypt(hCryptKey, 0, TRUE, 0, pData, dwDataLength, dwBufferLength);
if (FALSE == bRet)
{
ShowError("CryptEncrypt");
break;
}
} while (FALSE);
if (hCryptKey)
{
CryptDestroyKey(hCryptKey);
}
if (hCryptHash)
{
CryptDestroyHash(hCryptHash);
}
if (hCryptprov)
{
CryptReleaseContext(hCryptprov, 0);
}
return bRet;
}
//AES解密
BOOL AesDecrypt(BYTE* pPassword, DWORD dwPasswordLength, BYTE* pData, LPDWORD dwDataLength, DWORD dwBufferLength)
{
BOOL bRet = TRUE;
HCRYPTPROV hCryptprov = 0;
HCRYPTHASH hCryptHash = 0;
HCRYPTKEY hCryptKey = 0;
do
{
//获取CSP句柄
bRet = CryptAcquireContext(&hCryptprov, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT);
if (FALSE == bRet)
{
ShowError("CryptAcquireContext");
break;
}
//创建HASH对象
bRet = CryptCreateHash(hCryptprov, CALG_MD5, 0, 0, &hCryptHash);
if (FALSE == bRet)
{
ShowError("CryptCreateHash");
break;
}
//对密钥进行HASH计算
bRet = CryptHashData(hCryptHash, pPassword, dwPasswordLength, 0);
if (FALSE == bRet)
{
ShowError("CryptHashData");
break;
}
//使用HASH来生成密钥
bRet = CryptDeriveKey(hCryptprov, CALG_AES_128, hCryptHash, CRYPT_EXPORTABLE, &hCryptKey);
if (FALSE == bRet)
{
ShowError("CryptDeriveKey");
break;
}
bRet = CryptDecrypt(hCryptKey, 0, TRUE, 0, pData, dwDataLength);
if (FALSE == bRet)
{
ShowError("CryptDecrypt");
break;
}
} while (FALSE);
if (hCryptKey)
{
CryptDestroyKey(hCryptKey);
}
if (hCryptHash)
{
CryptDestroyHash(hCryptHash);
}
if (hCryptprov)
{
CryptReleaseContext(hCryptprov, 0);
}
return bRet;
}