Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Restrict how a console app can be called

We have a product that is a C# console app. Is it possible to restrict it to run from the command line only? In other words, users wouldn't be able to call it from a script or another app.

If it is, some example code would be much appreciated. Thanks.

like image 940
FunLovinCoder Avatar asked Nov 24 '10 11:11

FunLovinCoder


2 Answers

You can check the process that created your application using the code given here: http://msdn.microsoft.com/en-us/netframework/aa569609.aspx#Question3 . To be started at the DOS command line the parent process should be cmd.exe. Note however as pointed out by Colin this can be bypassed easily using a batch script. You can disable that as well by making sure that the command prompt arguments to cmd.exe are null. For this you will need to use WMI :
http://skysanders.net/subtext/archive/2010/04/11/using-wmi-to-fetch-the-command-line-that-started-all.aspx
You should also check the cmd.exe image is from system32 folder.

like image 55
basarat Avatar answered Sep 30 '22 00:09

basarat


I don't think it is possible to tell the difference.

Certainly the parent process is not a useful indicator. This is what you get in the parent process:

1. type app name into Command Prompt:     cmd.exe
2. call app from batch script:            cmd.exe
3. Double click on app or shortcut:       explorer.exe
4. type app name into Run dialog box:     explorer.exe   

If you intend for 1. to be a valid way to start your program, then I don't think you can stop 2. which means your app can be called from any script or any program (since it's simple for another program to create a 1 line batch script and execute it)

(BTW, does anyone know a way to get a table on StackOverflow?)

like image 22
Colin Pickard Avatar answered Sep 30 '22 02:09

Colin Pickard