program RunButAs;
{$APPTYPE CONSOLE}
{$R *.res}
uses
Windows,
SysUtils;
var
User, Password, Params : WideString;
sUser, sPassword : string;
function CreateProcessWithLogonW(
lpUsername : LPCWSTR;
lpDomain : LPCWSTR;
lpPassword : LPCWSTR;
dwLogonFlags : DWORD;
lpApplicationName : LPCWSTR;
lpCommandLine : LPWSTR;
dwCreationFlags : DWORD;
lpEnvironment : pointer;
lpCurrentDirectory : LPCWSTR;
var lpStartupInfo : TStartupInfoW;
var lpProcessInfo : TProcessInformation
) : boolean; stdcall; external 'Advapi32.dll';
procedure ShowHelp;
begin
writeln('Run an application under a specific user account');
writeln('Copyright (c) 2009, Serhiy Perevoznyk');
writeln;
writeln('Usage : ' + ExtractFileName(ParamStr(0)) + ' <user name=""> [<password>] <command line="">');
halt(1);
end;
const LOGON_WITH_PROFILE = 1;
var
si : TStartupInfoW;
pi : TProcessInformation;
hUserToken: THandle;
begin
try
if ParamCount < 2 then
ShowHelp;
if ParamCount = 2 then
begin
user := ParamStr(1);
suser := ParamStr(1);
Password := '';
sPassword := '';
Params := ParamStr(2);
end
else
if ParamCount = 3 then
begin
user := ParamStr(1);
Password := ParamStr(2);
suser := ParamStr(1);
sPassword := ParamStr(2);
Params := ParamStr(3);
end
else
ShowHelp;
FillChar(si, sizeof(si), 0);
FillChar(pi, sizeof(pi), 0);
if not LogonUser(PChar(suser), nil, PChar(spassword),
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, hUserToken) then
begin
writeln('Wrong user name or password');
halt;
end;
CreateProcessWithLogonW(PWideChar(user), nil, PWideChar(password), LOGON_WITH_PROFILE,
nil, PWideChar(Params), CREATE_UNICODE_ENVIRONMENT, nil, nil, si, pi);
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
end.
Comments
Post a Comment