Windows impersonation for WMI calls via python? - python

I'm using PyWin32 to make WMI calls to the system in python from my django web application. My goal is to allow users to add printers to the system via a web interface. To do this, I'm using win32print.AddPrinterConnection.
This works well running the development server under my user account. I can add all the printers I want. However, eventually, this will need to run under apache which runs as the LocalSystem account.
This is problematic for two reasons:
The LocalSystem account has no network privileges at all, and this is a network printer. The AddPrinterConnection WMI call eventually makes a COM call that will be disallowed.
The LocalSystem account has no access to the domain these printers are on. They require a domain account to access.
Therefore, I've come to the conclusion that I need to impersonate domain user(s) to accomplish this task. I've done so using the code found here:
http://code.activestate.com/recipes/81402/
This seems to work as I'm able to verify that I've successfully impersonated the calling code. Unfortunately, after impersonation I always get this error from the win32print.AddPrinterConnection API call:
Exception Type: error
Exception Value: (2, 'AddPrinterConnection', 'The system cannot find the file specified.')
Do you have any idea why this may be?
Thanks a bunch! Pete
Update
Playing around, I noticed the the AddPrinterConnection API call completes successfully if the user that I'm impersonating is currently logged into the system. Once I log that user out and retry the command while impersonating that user, I get the error stated above.
What is going on here?

I can't help with the specific problem, but I do know that if I had to work with WMI stuff on Windows, with Python, I would definitely reach for Tim Golden's Python WMI module instead of pywin32. Perhaps in the documentation/cookbook or Google searches using that module you can find a solution.

Related

Overwriting variables in system libraries (smtplib.py)

So I'm using Flask for a website and I'm using the extension Flask Mail to send emails for me. Unfortunately, my email server doesn't support CRAM_MD5 (for a valid reason) so when smtplib.py (Python system library file) reaches the line that is:
preferred_auths = [AUTH_CRAM_MD5, AUTH_PLAIN, AUTH_LOGIN]
it uses CRAM, and fails because of an authentication error. On my development server, I just edited the file and switch the order of PLAIN and CRAM and faced no problems. My system admin doesn't want to make this change every time he does a server build if he can help it so I was wondering if there was a way I could overwrite that system library variable from my Flask application? My Flask application is a little large so if there is a way to do this, I can post a structure of my application. I mostly want to know if it is possible to overwrite variables in system libraries and if so, the standard procedure for doing so. Thanks!

Log in to Windows from a Python service?

I'm theory crafting a Python service which will manipulate domain joined machines into running tests as part of a suite. Details of requirements
We must be logged in as a domain user, and we must not have the automatic login enabled
We need to reboot machines a few times, so it's a requirement for this to be sustainable
I'm wondering if it's possible to, from a Python service, somehow convince Windows to log us in? Presumably a Python service runs in Session0, as does Microsoft's Hardware Certification Kit? If that's capable of doing it, Python should also be (so far as I can see).
Any suggestions most welcome, I've got a suspicion there's a cheeky Windows API call that does this, but can't seem to find it anywhere.
So I've found a way to do it from a Windows service (written in C++) and presumably the ctypes library will permit me to use it.
Simple as using LogonUser from Win32API so far as I can see. Yet to actually set up and test it but it does seem to be exactly what I need. The difficulty being that session0 can only be accessed once logged in or via some remote debugging, so getting something like this working is no easy feat.

How can I implement error reporting (submit log to remote server) in Python desktop application

I'm writing an installed desktop app that I would like users to try out. I'd like to launch a pre-alpha release and collect some feedback, especially to fix any uncaught exceptions that might be thrown. As the developer would like to know about in the first instant.
i.e. I would like the installed desktop app to automatically submit relevant log entries to a remote server such that I can inspect them and fix the error.
I've considered using cloud-based services (they provide a nice dashboard interface: this is ideal) like but they're not really what I need:
Airbrake.io — quite pricey, geared towards webapps and servers
Loggly — has a forever free plan, but for servers only, based on syslog monitoring. I cannot expect users to install a syslog client as well as my application
I have never done centralized logging over internet connections, but in a local network. I used the standard sockethandler: http://docs.python.org/2/library/logging.handlers.html#sockethandler and it worked for me.
Other alternatives may be:
http://code.google.com/p/python-loggingserver/
https://papertrailapp.com/
http://pyfunc.blogspot.de/2013/08/centralized-logging-for-distributed.html
Also saving to a regular local log on crash may be a solution, and on the next startup of the app check if the log contains errors and send the log to your email.

Simple Python program to connect to a secure wifi network with user input credentials

My question I guess is: Is this possible without shelling out to command line and without 3rd party Python packages? I can't seem to find any native Python commands to manipulate or configure a wireless network connection. I know there are already built-in 'netsh wlan' commands in Windows 7, but would rather this all be in python. I am also confused by the logistics of this operation. With the netsh stuff, you still are required to have a wireless profile xml file specified in the command. My current image doesn't have any wireless profiles and I do not really understand the purpose of that if you are connecting to a brand new network. Why is this not automatically generated when you connect?
A little bit about the network
Network type: Infrastructure
Authentication: WPA2-Enterprise
Encryption: CCMP
The ultimate goal is to have a script that my users can just launch, put in their credentials, and never see the multiple Windows dialogues while doing so. I'm not asking for someone to write this for me. That's what I'm suppose to do. I just need to know if anyone has successfully done something like this in strictly Python or point me in the right direction. Thanks!
No. Python standard library doesn't ship with any functionality to control platform-specific functionality like wireless adapters. You have to invoke the tools shipped with the platform, find some 3rd party libraries that control this functionality, or write your own such libraries.

Security Flaw in my Python Program

I am working on a small program that requires the user to insert a USB Flash Drive (pre configured) to login to the system. The way it works right now is you log in to the windows OS, like usual. Then the script checks for the USB key, and if found, closes. But, if the key isn't found, the a dialog appears for the user to input a password. They have a certain amount of time they specify during installation.
Now, the problem with this is, the user can just launch the task manager and kill the process, thus rendering the script useless. Is there a better way to do this? How would I just disable the task manager if the USB key isn't present?
The software is written entirely in Python.
From your comments, you're logging the user into a Windows session. You won't be able to secure the system with the scheme you've set up - you've discovered the first of what will be many easily-exploited vulnerabilities. Look into credential providers (and GINA for editions of Windows older than Vista) - they're really your only sane path forward.
It's unlikely you'll be able to develop that with Python.

Categories