| 发表于:2007-10-03 23:25:13 楼主 |
下面的代码是我在网上下载的 可注入到一个简单的helloword窗口文件中 但是注入其他进程或者系统进程就不行 原来下载下来的没有提权我在网上找了一个提权函数把进程权限提升到了debug权限 不知道怎么回事还是不行 dword findtarget( lpctstr lpszprocess ) { dword dwret = 0; handle hsnapshot = createtoolhelp32snapshot( th32cs_snapprocess, 0 ); processentry32 pe32; pe32.dwsize = sizeof( processentry32 ); process32first( hsnapshot, &pe32 ); do { if ( lstrcmpi( pe32.szexefile, lpszprocess ) == 0 ) { dwret = pe32.th32processid; break; } } while ( process32next( hsnapshot, &pe32 ) ); closehandle( hsnapshot ); return dwret; } //给当前进程提权 void enabledebugpriv( void ) { handle htoken; token_privileges tkp; openprocesstoken(getcurrentprocess(),token_adjust_privileges ¦ token_query, &htoken); lookupprivilegevalue(null, se_debug_name,&tkp.privileges[0].luid); tkp.privilegecount = 1; tkp.privileges[0].attributes = se_privilege_enabled; adjusttokenprivileges(htoken, false, &tkp, 0,(ptoken_privileges)null, 0); closehandle( htoken ); } bool remoteloadlibrary( dword dwprocessid, lpcstr lpszdll ) { //得到调试权限 enabledebugpriv(); //messagebox( 0, _t("提权失败"), _t("错误"), mb_iconinformation ); // 打开目标进程 handle hprocess = openprocess( process_create_thread ¦ process_vm_operation ¦ process_vm_write, false, dwprocessid ); // 向目标进程地址空间写入dll名称 dword dwsize, dwwritten; dwsize = lstrlena( lpszdll ) + 1; lpvoid lpbuf = virtualallocex( hprocess, null, dwsize, mem_commit, page_readwrite ); if ( null == lpbuf ) { messagebox( 0, _t("在目标进程中分配空间失败"), _t("错误"), mb_iconinformation ); closehandle( hprocess ); return false; } if ( writeprocessmemory( hprocess, lpbuf, (lpvoid)lpszdll, dwsize, &dwwritten ) ) { // 要写入字节数与实际写入字节数不相等,仍属失败 if ( dwwritten != dwsize ) { virtualfreeex( hprocess, lpbuf, dwsize, mem_decommit ); messagebox( 0, _t("要写入字节数与实际写入字节数不相等"), _t("错误"), mb_iconinformation ); closehandle( hprocess ); return false; } } else { messagebox( 0, _t("向目标进程中写入数据失败"), _t("错误"), mb_iconinformation ); closehandle( hprocess ); return false; } // 使目标进程调用loadlibrary,加载dll dword dwid; pthread_start_routine pfunc = (pthread_start_routine)getprocaddress( getmodulehandle("kernel32.dll"),"loadlibrarya"); //lpvoid pfunc = loadlibrarya; handle hthread = createremotethread( hprocess, null, 0, pfunc, lpbuf, 0, &dwid ); // 等待loadlibrary加载完毕 waitforsingleobject( hthread, infinite ); // 释放目标进程中申请的空间 virtualfreeex( hprocess, lpbuf, dwsize, mem_decommit ); closehandle( hthread ); closehandle( hprocess ); return true; } bool remotefreelibrary( dword dwprocessid, lpcstr lpszdll ) { //得到调试权限 enabledebugpriv(); // 打开目标进程 handle hprocess = openprocess( process_create_thread ¦ process_vm_operation ¦ process_vm_write, false, dwprocessid ); // 向目标进程地址空间写入dll名称 dword dwsize, dwwritten; dwsize = lstrlena( lpszdll ) + 1; lpvoid lpbuf = virtualallocex( hprocess, null, dwsize, mem_commit, page_readwrite ); if ( null == lpbuf ) { closehandle( hprocess ); return false; } if ( writeprocessmemory( hprocess, lpbuf, (lpvoid)lpszdll, dwsize, &dwwritten ) ) { // 要写入字节数与实际写入字节数不相等,仍属失败 if ( dwwritten != dwsize ) { virtualfreeex( hprocess, lpbuf, dwsize, mem_decommit ); closehandle( hprocess ); return false; } } else { closehandle( hprocess ); return false; } // 使目标进程调用getmodulehandle,获得dll在目标进程中的句柄 dword dwhandle, dwid; lpvoid pfunc = getmodulehandlea; handle hthread = createremotethread( hprocess, null, 0, (lpthread_start_routine)pfunc, lpbuf, 0, &dwid ); // 等待getmodulehandle运行完毕 waitforsingleobject( hthread, infinite ); // 获得getmodulehandle的返回值 getexitcodethread( hthread, &dwhandle ); // 释放目标进程中申请的空间 virtualfreeex( hprocess, lpbuf, dwsize, mem_decommit ); closehandle( hthread ); // 使目标进程调用freelibrary,卸载dll pfunc = freelibrary; hthread = createremotethread( hprocess, null, 0, (lpthread_start_routine)pfunc, (lpvoid)dwhandle, 0, &dwid ); // 等待freelibrary卸载完毕 waitforsingleobject( hthread, infinite ); closehandle( hthread ); closehandle( hprocess ); return true; } int callback maindlgproc( hwnd hdlg, uint umsg, wparam wparam, lparam lparam ) { static dword dwprocessid; switch ( umsg ) { case wm_initdialog: { dwprocessid = 0; senddlgitemmessage( hdlg, idc_edt_target, em_limittext, max_path, 0 ); } break; case wm_command: { switch ( loword( wparam ) ) { case idc_btn_exit: { enddialog( hdlg, 0 ); } break; case idc_btn_insert: { tchar sztarget[max_path]; getdlgitemtext( hdlg, idc_edt_target, sztarget, max_path ); dwprocessid = findtarget( sztarget ); if ( 0 == dwprocessid ) { messagebox( hdlg, _t("找不到目标进程。"), _t("错误"), mb_iconinformation ); break; } if ( !remoteloadlibrary( dwprocessid, "dll.dll" ) ) { messagebox( hdlg, _t("远程dll加载失败。"), _t("错误"), mb_iconinformation ); } } break; case idc_btn_detach: { if ( 0 == dwprocessid ) { messagebox( hdlg, _t("找不到目标进程。"), _t("错误"), mb_iconinformation ); break; } if ( !remotefreelibrary( dwprocessid, "dll.dll" ) ) { messagebox( hdlg, _t("远程dll卸载失败。"), _t("错误"), mb_iconinformation ); } } break; } } break; case wm_close: { enddialog( hdlg, 0 ); } break; } return 0; } int winapi _twinmain( hinstance hinstance, hinstance hprevinstance, lptstr lpcmdline, int nshowcmd ) { return dialogbox( hinstance, makeintresource( idd_main_dlg ), null, maindlgproc ); } |
|
|
|
|