Wednesday, July 16, 2008

Run .NET Code from a shared drive/directory

Security is an issue whenever running code from an untrusted source like a shared or network drive. It would be nice to be able to put an application on a share drive to run whether it be in C# or VB .NET.

Normally, when you try to run .NET code on a shared network drive you will get an exception like System.Security.Permissions.SecurityPermission .


There is a way to relax the security via the caspol.exe command(For .Net Framework 2.0 "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CasPol.exe -s off").

An application may be launched with one simple batch file as follows:

AllInOne.BAT:
@ECHO OFF

REM Relax security constraints for .NET
START C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CasPol.exe -s off

REM Pause until first command has executed
Set Count=1
:LOOP
Set /A Count=%Count%+1
If %Count% lss 100 GoTo :LOOP

REM Launch .NET application
"""SomeExecutable.exe"""

REM Remove relaxation of security constraints
TASKKILL /im CasPol.exe

@ECHO ON

*****************************

A pause is required to allow caspol to start up before launching the .NET application.

But of course this implementation does meet the requirements of launching the .NET application from the shared location but there are those DOS windows which are open and probably not that desirable.

These can be avoided with 2 BAT files and 2 VBS files as follows. This is just one way of doing it but there are probably better ways out there.

launch.bat (Main file)
****************
@ECHO OFF
REM Main Launch for .NET application on share drive
REM VBS script used so there is not a DOS console opened up

START launchtasks.vbs

@ECHO ON

****************

launchtasks.vbs
****************
On Error Resume Next

CmdLine = """tasks.bat"""
Params = ""
CmdLine = CmdLine & " " & Params

'Launch without window
wscript.echo fShellRun(CmdLine)

Function fShellRun(sCommandStringToExecute)
' This function will accept a string as a DOS command to execute.
' It will then execute the command in a shell, and capture the output into a file.
' That file is then read in and its contents are returned as the value the function returns.
Dim oShellObject, oFileSystemObject, sShellRndTmpFile
Dim oShellOutputFileToRead, iErr
Set oShellObject = CreateObject("Wscript.Shell")
Set oFileSystemObject = CreateObject("Scripting.FileSystemObject")
sShellRndTmpFile = oShellObject.ExpandEnvironmentStrings("%temp%") & oFileSystemObject.GetTempName
On Error Resume Next
oShellObject.Run sCommandStringToExecute & " > " & sShellRndTmpFile, 0, True
iErr = Err.Number
On Error GoTo 0
If iErr <> 0 Then
fShellRun = ""
Exit Function
End If
fShellRun = oFileSystemObject.OpenTextFile(sShellRndTmpFile,1).ReadAll
oFileSystemObject.DeleteFile sShellRndTmpFile, True
End Function

****************

tasks.bat
****************
@ECHO OFF

REM Relax security constraints for .NET
REM Run this in VBS to avoid a DOS WINDOW
START caspol.vbs

REM Pause until first command has executed
Set Count=1
:LOOP
Set /A Count=%Count%+1
If %Count% lss 70 GoTo :LOOP

REM Launch .NET application
"""SomeExecutable.exe"""

REM Remove relaxation of security constraints (The >NUL is to avoid showing the info on the processes terminated)
TASKKILL /im CasPol.exe >NUL

@ECHO OFF

****************


caspol.vbs
****************
On Error Resume Next

'The command below will also depend on the .NET version of the application you are running

CmdLine = """C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CasPol.exe"""
Params = "-s off"
CmdLine = CmdLine & " " & Params

'New way without window
wscript.echo fShellRun(CmdLine)

Function fShellRun(sCommandStringToExecute)
' This function will accept a string as a DOS command to execute.
' It will then execute the command in a shell, and capture the output into a file.
' That file is then read in and its contents are returned as the value the function returns.
Dim oShellObject, oFileSystemObject, sShellRndTmpFile
Dim oShellOutputFileToRead, iErr
Set oShellObject = CreateObject("Wscript.Shell")
Set oFileSystemObject = CreateObject("Scripting.FileSystemObject")
sShellRndTmpFile = oShellObject.ExpandEnvironmentStrings("%temp%") & oFileSystemObject.GetTempName
On Error Resume Next
oShellObject.Run sCommandStringToExecute & " > " & sShellRndTmpFile, 0, True
iErr = Err.Number
On Error GoTo 0
If iErr <> 0 Then
fShellRun = ""
Exit Function
End If
fShellRun = oFileSystemObject.OpenTextFile(sShellRndTmpFile,1).ReadAll
oFileSystemObject.DeleteFile sShellRndTmpFile, True
End Function

****************

11 comments:

Anonymous said...

Have you checked out .NET Framework 3.5 SP1? This feature is addressed in that release.
You can actually run .NET code from a network share without needing the fancy script file magic.
http://blogs.msdn.com/brada/archive/2008/05/05/visual-studio-2008-and-net-framework-3-5-sp1-beta.aspx

Anonymous said...

I am thinking about performing a reverse cell look up by going online. I am having these phone calls by someone that I don't know and am curious who believe that they are contacting. Lots of different crazy texts and messages are being left on my personal voice mail and its starting to drive me insane. So, where should i locate these reverse phone search pages?

Anonymous said...

Greetings! I know this is somewhat off topic but I was wondering
if you knew where I could find a captcha plugin for my comment form?
I'm using the same blog platform as yours and I'm
having difficulty finding one? Thanks a lot!

Feel free to surf to my blog post :: Trans Vaginal Settlement Funding

Anonymous said...

I comment when I especially enjoy a article on a site or if
I have something to valuable to contribute to the conversation.
Usually it is triggered by the sincerness displayed in the article I read.
And on this post "Run .NET Code from a shared drive/directory".

I was actually excited enough to leave a comment :
-P I actually do have a couple of questions
for you if you don't mind. Could it be just me or do a few of these comments look like coming from brain dead
individuals? :-P And, if you are posting on other online social
sites, I'd like to follow anything new you have to post. Would
you list all of all your community pages like your
Facebook page, twitter feed, or linkedin profile?|

Look into my web site; pity 2014 program

Anonymous said...

It is a great time to be spontaneous and maybe
even a little silly. If you are still unsure of the photographer,
you can always seek referrals by asking friends or family members about their experiences with photographers used in their weddings.
If you are developing your client's pictures yourself, Photoshop is an excellent
photo editing program.

My webpage ... wedding photography ky ()

Anonymous said...

Awesome site you have here but I was curious if you knew of any forums that cover the same topics discussed here?
I'd really love to be a part of online community where I can get responses from other
experienced people that share the same interest. If you have
any suggestions, please let me know. Thanks a
lot!

Also visit my page cenaless

Anonymous said...

Hey there! I know this is kind of off topic but I was wondering iff you knew where I could
locate a captcha plugin for my comment form? I'm using the same
blog pkatform as yours and I'm having trouble findiing
one? Thanks a lot!

My website; SEO company prices

Anonymous said...

Cons: Lowest potency, couldn't discover a business website, need
to take 3 capsules each day to get even 1500mg
Nature's Symbiotics Garcinia Cambogia HP - Natural Appetite Suppressant
and Premium Belly Fat Burner - Pure 1600mg Extract, 60% HCA, 30 Day Supply.

Bitter orange extract chemicals may increase heart
rate, blood pressure, migraines and cause fainting,
strokes and heart attacks. First, transdermal diet patches never have been approved by these US Food and
Treatment Administration for weight diminishment or weight control.


my web page reviews of pure garcinia cambogia

Anonymous said...

Good day! I know this is somewhat off topic but I was wondering if you
knew where I could get a captcha plugin for my comment form?
I'm using the same blog platform as yours and I'm having
trouble finding one? Thanks a lot!

my web site - bmw repair st george ut

Anonymous said...

Hi colleagues, how is the whole thing, and what you want to
say regarding this article, in my view its genuinely
remarkable in support of me.

Also visit my web site ... home inspector in staten island

Unknown said...

this article replica designer bags wholesale you can try these out Ysl replica handbags visit the website bags replica ysl