CreateProcess, CreateProcessWithLogonW & CreateProcessAsUser 一些注意事项

Written by

in

前一阵子写Small Sandbox和SXKDZ Compare的时候遇到了一些问题。问题都是出在CreateProcess, CreateProcessWithLogonW和CreateProcessAsUser这三个函数上。
于是写一个备注,以防忘记,如有错误,请留言指正。

第一个问题:

[cpp]
CreateProcessW(NULL, _TEXT(“C:\\init.exe”), ……);
[/cpp]

这是 SXKDZ Compare里面的一段代码,可能明眼人马上就看得出来。我没写过Unicode的程序(搞OI的都是写Ansi的程序),于是以为以下的代码可以直接改成Unicode版本的

[cpp]
CreateProcessA(NULL, “C:\\init.exe”, ……);
[/cpp]

后来我发现改为Unicode版本以后会报错1812或者直接崩溃,于是检查StartupInfo和ProcessInformation结构体的初始化……但是没有发现错误。(咨询FlowerCode也无果)
后来咨询iceboy才知道MSDN中有这么一句(Link):

The Unicode version of this function, CreateProcessW, can modify the contents of this string. Therefore, this parameter cannot be a pointer to read-only memory (such as aconst variable or a literal string). If this parameter is a constant string, the function may cause an access violation.

也就是说我得给第二个参数一个缓冲区。
这是我犯的第一个错误 😥

第二个问题:我写Small Sandbox的时候想把被限制程序以一个0权用户运行再加上若干JobObject的限制来防止被限制程序乱搞,将程序以一个用户的权限运行有2还是可以用:CreateProcessWithLogonW、CreateProcessAsUser,咨询熊熊得知CreateProcessWithLogonW方便些(当时没有告诉他我还要加JobObject),于是有了如下代码:

[cpp]
CreateProcessWithLogonW(_TEXT(“Username”), _TEXT(“.”), _TEXT(“Password”), LOGON_WITH_PROFILE, NULL, _TEXT(“C:\\init.exe”), ……);
[/cpp]

然后成功以后加入JobObject,提示拒绝访问-5的错误,然后找到一解决方案(Link),按照解决方案添加了Breakaway form job的标记还是错了。最后在StackOverFlow里找到一提问(Link),里面有这样一句:

Edit: It seems that svchost.exe, which actually creates the process when CreateProcessWithLogonW is used, already assigns the process to an anonymous job. The CREATE_CREAKAWAY_FROM_JOB flag is ignored by this function. So the real question is: is there a way to prevent svnhost from assigning the process to a job ?

于是咨询ib,得出不能同时使用JobObject和CreateProcessWithLogonW这俩玩意儿,于是尝试写CreateProcessAsUser。
开始的时候莫名其妙什么错误吧(源代码已经丢了 😡 ) ,然后改了改弹出了应用程序初始化失败,然后ib告诉我是没有某权限(我的聊天记录丢了),于是参照VijosNT Mini的代码添加了给窗口站和一个我又忘记名字的东西加上了权限,最后成功。
代码存档:

ib友情提示:这个函数很难用……

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *