Home All Groups Group Topic Archive Search About

Winform: Call a vbscript with elevated privileges

Author
9 Nov 2006 11:19 AM
Jim Andersen
I am making a .NET 2.0 WinForms application.

It lets a "normal" user select one or more scripts to be run with Admin
(sort of) privileges. And their actions get logged in a database.

How do I run a script as another user?

It's not super secret stuff we are talking about. Embedding the name and
password of the Admin (sort of) account is enough.

I saw this piece of code for calling calc.exe. How do I modify it to use a
different user account?
ms-help://MS.VSCC.v80/MS.MSDN.vAug06.en/ws_wminet_conc/html/b40d2a05-e631-4a09-97cb-881d452dc432.htm

thx
/jim

Author
9 Nov 2006 3:31 PM
Dominick Baier
Have a look at the StartupInformation you can pass into Process.Start. Here
you can specify username/password.

---
Dominick Baier, DevelopMentor
http://www.leastprivilege.com

Show quoteHide quote
> I am making a .NET 2.0 WinForms application.
>
> It lets a "normal" user select one or more scripts to be run with
> Admin (sort of) privileges. And their actions get logged in a
> database.
>
> How do I run a script as another user?
>
> It's not super secret stuff we are talking about. Embedding the name
> and password of the Admin (sort of) account is enough.
>
> I saw this piece of code for calling calc.exe. How do I modify it to
> use a different user account?
> ms-help://MS.VSCC.v80/MS.MSDN.vAug06.en/ws_wminet_conc/html/b40d2a05-e
> 631-4a09-97cb-881d452dc432.htm
>
> thx
> /jim
Author
10 Nov 2006 11:19 AM
Jim Andersen
Dominick Baier wrote:
> Have a look at the StartupInformation you can pass into
> Process.Start. Here you can specify username/password.

Perfect ! Thx a bunch.

/jim
Author
14 Nov 2006 8:52 AM
Jim Andersen
Dominick Baier wrote:
> Have a look at the StartupInformation you can pass into
> Process.Start. Here you can specify username/password.

I am now switching to an ASP.NET  web-based solution.
I have the files for the solution on a network-share.
The following code works fine when I am in Visual Studio 2005 and hit F5.
It uses the builtin http:\\localhost:1417 to run the app.
My testscript echoes back the currentUsername. And it returns the supplied
username.
Perfect.

But when I copy the solution to c:\inetpub\wwwroot and start a browser and
goes to localhost/myapp it bombs with:
Titlebar: "cscript.exe - Program error"
Text: Failed to initialize program correctly (0xc0000142). Click ok to close
the program."

Dim myProcess As New Process
Dim mypass As New System.Security.SecureString
Dim output As String

For Each ch As Char In "MyPassword"
   mypass.AppendChar(ch)
Next

With myProcess.StartInfo
.Domain = MYDOMAIN
.FileName = "c:\windows\system32\cscript.exe"
.Arguments = "//Nologo " & PATH & "TestScript.vbs HelloWorld"
.UserName = "MyUserName"
.Password = mypass
.UseShellExecute = False
.RedirectStandardOutput = True
End With

myProcess.Start()
output = myProcess.StandardOutput.ReadToEnd()
myProcess.WaitForExit()

tia
/jim
Author
16 Nov 2006 1:04 PM
Jim Andersen
OK. Since supplying Process.Start with username/password didn't work, I
tried another approach: Impersonation via LogonUser.

So I have
1. <code to start impersonation using LogonUser, and Impersonate>
2. my code to Process.Start as before,
but this time without specifying a username/password.
3. <code to stop impersonation>

but now I get this output:
Microsoft Windows scripting version 5.6 (etc... the banner-info) followed by
"Windows Script Host"...
"Can't find script engine 'VBScript' for 'C:\test.vbs'."

If I move 2 up before 1 & 3 the code works fine. c:\test.vbs is executed.
The only "solutions" to this problem is reinstalling wscript or updating to
the newest version, og regserver /s vscript.dll

But that is on PCs where scripts doesn't work. It works ok. Just not when
impersonating.

/jim
Author
16 Nov 2006 2:50 PM
Jim Andersen
addendum.... If I go to a dosprompt and do
runas cmd.exe
and supply the username/password I get a dosprompt and is logged in as the
superuser.
I can then do
cscript.exe c:\test.vbs
and it runs ok.

So it shouldn't be a question of insufficient rights.

/jim