delphi 以低用户权限启动一个进程.比如Vista或者WIN7中的IE


本文整理自网络,侵删。

 { *******************************************************

以低权限启动一个进程(例如IE)

版权所有 (C) 2013 wr960204 武稀松

******************************************************* }

unit LowIntergrityLevelProcess;

interface

uses
WinApi.Windows;

const
SECURITY_MANDATORY_UNTRUSTED_RID = $00000000;
SECURITY_MANDATORY_LOW_RID = $00001000;
SECURITY_MANDATORY_MEDIUM_RID = $00002000;
SECURITY_MANDATORY_HIGH_RID = $00003000;
SECURITY_MANDATORY_SYSTEM_RID = $00004000;
SECURITY_MANDATORY_PROTECTED_PROCESS_RID = $00005000;

function CreateLowIntegrityProcess(const ExeName: string;
const Params: string = ”; TimeOut: DWORD = 0): HResult;
function GetIntegrityLevel(): DWORD;

implementation

type
PTokenMandatoryLabel = ^TTokenMandatoryLabel;

TTokenMandatoryLabel = packed record
Label_TSidAndAttributes;
end;

function GetIntegrityLevel(): DWORD;
var
hProcesshTokenTHandle;
pTILPTokenMandatoryLabel;
dwReturnLengthDWORD;
dwTokenUserLengthDWORD;
psaCountPUCHAR;
SubAuthorityDWORD;
begin
Result := 0;
dwReturnLength := 0;
dwTokenUserLength := 0;
pTIL := nil;

hProcess := GetCurrentProcess();
OpenProcessToken(hProcess, TOKEN_QUERY or TOKEN_QUERY_SOURCE, hToken);
if hToken = 0 then
Exit;
if not GetTokenInformation(hToken, WinApi.Windows.TTokenInformationClass
(TokenIntegrityLevel), pTILdwTokenUserLengthdwReturnLengththen
begin
if GetLastError = ERROR_INSUFFICIENT_BUFFER then
Begin
pTIL := Pointer(LocalAlloc(0, dwReturnLength));
if pTIL = nil then
Exit;
dwTokenUserLength := dwReturnLength;
dwReturnLength := 0;

if GetTokenInformation(hToken, WinApi.Windows.TTokenInformationClass
(TokenIntegrityLevel), pTILdwTokenUserLengthdwReturnLengthand
IsValidSid((pTIL.Label_).Sidthen
begin
psaCount := GetSidSubAuthorityCount((pTIL.Label_).Sid);
SubAuthority := psaCount^;
SubAuthority := SubAuthority ?C 1;
Result := GetSidSubAuthority((pTIL.Label_).SidSubAuthority)^;
end;
LocalFree(Cardinal(pTIL));
End;
end;

CloseHandle(hToken);
end;

const
userenvlib = ‘userenv.dll’;

function CreateEnvironmentBlock(lpEnvironment: PPointer; hToken: THandle;
bInherit: BOOL): BOOLstdcallexternal userenvlib;
function DestroyEnvironmentBlock(lpEnvironment: Pointer): BOOLstdcall;
external userenvlib;

function CreateLowIntegrityProcess(const ExeName, Params: string;
TimeOut: DWORD): HResult;
type
_TOKEN_MANDATORY_LABEL = Record
Label_SID_AND_ATTRIBUTES;
End;

TOKEN_MANDATORY_LABEL = _TOKEN_MANDATORY_LABEL;
PTOKEN_MANDATORY_LABEL = ^TOKEN_MANDATORY_LABEL;

const
SECURITY_MANDATORY_LABEL_AUTHORITYTSidIdentifierAuthority =
(Value: (0000016));
SE_GROUP_INTEGRITY = $00000020;
SE_GROUP_INTEGRITY_ENABLED = $00000040;
var
hTokenhNewTokenTHandle;
MLAuthoritySID_IDENTIFIER_AUTHORITY;
pIntegritySidPSID;
tmlTOKEN_MANDATORY_LABEL;
siTStartupInfo;
piPROCESS_INFORMATION;
pszCommandLinestring;
dwCreationFlagDWORD;
pEnvironmentLPVOID;
begin

Result := ERROR_SUCCESS;
pszCommandLine := ExeName + Params;
hToken := 0;
hNewToken := 0;
MLAuthority := SECURITY_MANDATORY_LABEL_AUTHORITY;
pIntegritySid := nil;
FillChar(tml, sizeof(tml), 0);
FillChar(si, sizeof(si), 0);
FillChar(pi, sizeof(pi), 0);

si.cb := sizeof(si);
si.lpDesktop := ‘Winsta0\Default’;
dwCreationFlag := NORMAL_PRIORITY_CLASS or CREATE_NEW_CONSOLE;
pEnvironment := nil;

try
// 从自己获取一个令牌
if (not OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE or
TOKEN_QUERY or TOKEN_ADJUST_DEFAULT or TOKEN_ASSIGN_PRIMARYhToken)) then
begin
Result := GetLastError();
Exit;
end;

// 复制令牌
if (not DuplicateTokenEx(hToken, 0, nil, SecurityImpersonation,
TokenPrimary, hNewToken)) then
begin
Result := GetLastError();
Exit;
end;

// 创建一个低权限的SID
if (not AllocateAndInitializeSid(MLAuthority, 1, SECURITY_MANDATORY_LOW_RID,
0000000, pIntegritySid)) then
begin
Result := GetLastError();
Exit;
end;

tml.Label_.Attributes := SE_GROUP_INTEGRITY;
tml.Label_.Sid := pIntegritySid;

// 设置这个低权限SID到令牌
if (not SetTokenInformation(hNewToken, TokenIntegrityLevel, @tml,
(sizeof(tml) + GetLengthSid(pIntegritySid)))) then
begin
Result := GetLastError();
Exit;
end;

// 创建一个环境变量
if (CreateEnvironmentBlock(@pEnvironment, hToken, FALSE)) then
dwCreationFlag := dwCreationFlag or CREATE_UNICODE_ENVIRONMENT
else
pEnvironment := nil;

// 创建一个低权限的进程
if (not CreateProcessAsUser(hNewToken, nil, PChar(pszCommandLine), nilnil,
FALSEdwCreationFlagpEnvironmentnilsipi)) then
begin
Result := GetLastError();
Exit;
end;

WaitForSingleObject(pi.hProcess, TimeOut);
finally
// 清理现场
if pEnvironment <> nil then
begin
DestroyEnvironmentBlock(pEnvironment);
pEnvironment := nil;
end;

if (hToken <> 0then
begin
CloseHandle(hToken);
hToken := 0;
end;
if (hNewToken <> 0then
begin
CloseHandle(hNewToken);
hNewToken := 0;
end;
if (pIntegritySid <> nilthen
begin
FreeSid(pIntegritySid);
pIntegritySid := nil;
end;
if (pi.hProcess <> 0then
begin
CloseHandle(pi.hProcess);
pi.hProcess := 0;
end;
if (pi.hThread <> 0then
begin
CloseHandle(pi.hThread);
pi.hThread := 0;
end;

if (ERROR_SUCCESS <> Resultthen
begin
SetLastError(Result);
end
else
begin
Result := ERROR_SUCCESS;
end;
end;
end;

end.

相关阅读 >>

Delphi 扫雷外挂

Delphi 数字分隔

Delphi遍历所有控件

Delphi jpeg压缩的两种方法

Delphi xe8 支持md5

delph控制台(console)程序添加图标和版权信息

Delphi通过wmi获取系统信息

Delphi extctrls.frame3d

Delphi excel表格数据导入数据库

Delphi 10.4.1 edgebrowser 模拟操作网页方法

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    正在狠努力加载,请稍候...