2

I use a program (on Windows), whose name I won't disclose, that can be opened from the command line without going through any authentication. I'm trying to create some security to prevent others from accessing it this way.

I plan on replacing the built-in binary for this program with a batch file with a first line that points to my own authentication system (implemented in python, compiled to .exe with py2exe), and the second line to the command that opens the program.

My original plan was to have my script (auth.py) stop the batch file from executing the second line if authentication failed with something like this:

if not authenticated: print('Authentication failed') sys.exit(1) else: print('Successfully authenticated!') 

I had counted on sys.exit(1) to do this, and I didn't bother testing it out until I was done developing the script. Now I realize that sys.exit only exits the python process.

I either need a way to stop the batch process FROM the python script, or a way for the batch to detect the exit code from auth.py (1 if failed, 0 if passed) and execute the other program only if the exit code is 0 from there.

If anyone could give me any suggestions or help of any sort, I would really appreciate it. Thanks!

2
  • 7
    if someone can access the program via a batch script, what stops them from running it directly? Commented Aug 10, 2012 at 18:25
  • Nothing. It's just a light security measure to prevent un-technologically inclined people from accessing it until it is hopefully patched in the next release. Commented Aug 10, 2012 at 20:59

3 Answers 3

2

Use subprocess to call the program on successful authentication. So your python script would launch the program, not a batch file.

if not authenticated: print('Authentication failed') sys.exit(1) else: print('Successfully authenticated!') proc = subprocess.Popen([program]) 

Please note, if the user has permission to start the program from within a python or batch script, nothing is stopping them from accessing it directly. This will prevent no one from accessing the program; save maybe the extreme un-technical.

Sign up to request clarification or add additional context in comments.

1 Comment

This is a great idea, it hadn't even crossed my mind. Now I don't even need a batch file, since I'll be compiling to .exe with py2exe anyways. And I know it won't stop anybody who really wants to access it. The command to open it is really obscure at least. It has a built in authentication system, but this is kind of a backdoor type thing I stumbled upon accidentally. I reported it to the devs already, I'm hoping they'll patch it in their next release. This is just a temporary solution :) Thanks!
1

You could do something really complicated to try and find the parent PID of the Python process and kill that or you could just check the %ERRORLEVEL% in your batch file. Something like:

python auth.py if %ERRORLEVEL% neq 0 exit /B 1 

Comments

1

I found these two methods hope these might help
http://metazin.wordpress.com/2008/08/09/how-to-kill-a-process-in-windows-using-python/
http://code.activestate.com/recipes/347462-terminating-a-subprocess-on-windows/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.