您的位置:程序门 -> vc/mfc -> 进程/线程/dll



dll注入系统进程不成功


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


dll注入系统进程不成功
发表于: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   );
}
发表于:2007-10-03 23:34:441楼 得分:0
远线程序插入一般对于当前用户的进程有效,但对系统进程和大多数的服务是无效的.最典型的就是可以将dll插入smss.exe,但是却不能创建窗口,对于无交互的系统服务也不能创建窗口.

我建议你在dll中通过一个管道来报告执行情况,看看dll倒底执行到了哪一步或者根本就没有执行.
发表于:2007-10-04 14:13:552楼 得分:0
谢谢,不过我听说不是把自己的进程提权为调试权限不就可以了吗?哎。。。。。。。。。
发表于:2007-10-04 16:34:313楼 得分:0
我简单地用测试了一下,管理员权限下,任务管理器显示出来的"进程"中除了"system   idle   process",   "system",   "smss.exe"三个不能注入处,其它如lsass.exe,   csrss.exe等等都可以成功注入,,   但是与创建窗口有关的操作将可能阻塞注入的线程,导致假死,   可以参照"在没有设置'交互'的服务中与用户交互"的解决方案来实现窗口的创建.
我是用可能不成功的操作后面写管道的方法来验证的.使用了汇编:
.386
.model   flat,stdcall
option   casemap:none

include windows.inc
include user32.inc
includelib                 user32.lib
include syelog.inc
includelib syelog.lib

.data

szcaption db 'hi   !',0
sztext db 'hello,   world   !',0
szlog db 'sltest',0

.code
start:
invoke   syelogopen,   addr   szlog,   syelog_facility_application   ;包装了一个写管道的函数
invoke   syelog,syelog_severity_information,   addr   sztext     ;除system   idle   process,   system   ,smss.exe   外都能成功送出内容
invoke   messagebox,   0,   addr   sztext,   addr   szcaption,   0   ;插入当前用户进程时成功,其它情况下将导致进程阻塞,   下一句无法执行  
invoke   syelog,syelog_severity_information,   addr   sztext
invoke   syelogclose,   0
ret
;> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
end start
发表于:2007-10-04 16:54:314楼 得分:0
在远线程中使用如下代码即可实现在系统用户名下与当前桌面用户交互:
c/c++ code
bool threadinteract(void) { hdesk hdeskcurrent; hdesk hdesktest; hdesk hdesk; hwinsta hwinstacurrent; hwinsta hwinsta; hwinstacurrent = getprocesswindowstation(); if (hwinstacurrent == null) return false; hdeskcurrent = getthreaddesktop(getcurrentthreadid()); if (hdeskcurrent == null) return false; hwinsta = openwindowstation("winsta0", false, winsta_accessclipboard | winsta_accessglobalatoms | winsta_createdesktop | winsta_enumdesktops | winsta_enumerate | winsta_exitwindows | winsta_readattributes | winsta_readscreen | winsta_writeattributes); if (hwinsta == null) return false; if!setprocesswindowstation(hwinsta)) return false; hdesktest = getthreaddesktop(getcurrentthreadid()); if (hdesktest == null) return false; hdesk = opendesktop("default", 0, false, desktop_createmenu | desktop_createwindow | desktop_enumerate | desktop_hookcontrol | desktop_journalplayback | desktop_journalrecord | desktop_readobjects | desktop_switchdesktop | desktop_writeobjects); if (hdesk == null) return false; if!setthreaddesktop(hdesk)) return false; messagebox(null, "mb_ok", "test_interact", mb_ok); if!setprocesswindowstation(hwinstacurrent)) return false; if!setthreaddesktop(hdeskcurrent)) return false; if!closewindowstation(hwinsta)) return false; if!closedesktop(hdesk)) return false; return true; }
发表于:2007-10-06 21:28:295楼 得分:0

lisunlin0  
我简单地用测试了一下,管理员权限下,任务管理器显示出来的"进程"中除了"system   idle   process",   "system",   "smss.exe"三个不能注入


-------------------------------------

前两个可以说不是进程,smss不加载kernel32.dll,因此你不能createremotethread   的方法~
其他系统进程因为不在   default窗口中,所以看不到。你注入到winlogon中,然后按win+l应该可以看到窗口。
发表于:2007-10-06 21:50:576楼 得分:0
怎么听不懂你们在说什么呢?能说的具体一点


快速检索

最新资讯
热门点击