Last active
December 18, 2015 05:39
-
-
Save maphew/5734699 to your computer and use it in GitHub Desktop.
An experimental Leo button which executes the currently selected node as a python script after invoking User Account Control (UAC). Not fully functional yet -- and dangerous. You could easily bork all your data or even your computer with this: it runs code with admin privileges, no sanity checking, and very limited feedback. Tracebacks etc. are …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<!-- Created by Leo (http://webpages.charter.net/edreamleo/front.html) --> | |
<?xml-stylesheet ekr_test?> | |
<leo_file xmlns:leo="http://www.leo-editor.org/2011/leo" > | |
<leo_header file_format="2"/> | |
<vnodes> | |
<v t="maphew.20130607222534.1739" a="E"><vh>@button Run Elevated</vh> | |
<v t="maphew.20130608014320.1983"><vh>docstring</vh></v> | |
<v t="maphew.20130608014320.1743"><vh>imports</vh></v> | |
<v t="maphew.20130608014320.1744"><vh>Leo preparation</vh></v> | |
<v t="maphew.20130608014320.1745"><vh>UAC Elevation</vh></v> | |
<v t="maphew.20130608014320.1742"><vh>Resources</vh></v> | |
</v> | |
</vnodes> | |
<tnodes> | |
<t tx="maphew.20130607222534.1739">@others | |
</t> | |
<t tx="maphew.20130608014320.1742"># adapted from | |
# @url http://blog.pythonaro.com/2011/09/python-wmi-services-and-uac.html | |
# @url http://stackoverflow.com/questions/130763/request-uac-elevation-from-within-a-python-script | |
# @url http://pypeelf.googlecode.com/svn/branches/third-party/winappdbg-1.3/winappdbg/win32/shell32.py | |
# Launching Applications (ShellExecute, ShellExecuteEx, SHELLEXECUTEINFO) | |
# @url http://msdn.microsoft.com/en-us/library/windows/desktop/bb776886%28v=vs.85%29.aspx</t> | |
<t tx="maphew.20130608014320.1743">import os | |
import sys | |
import ctypes | |
import tempfile</t> | |
<t tx="maphew.20130608014320.1744">g.es('Run-elevated on:', p.h) | |
params = r'-ic "{}" '.format(p.b) # pass body of current node to python as command line script | |
# g.es(params) | |
</t> | |
<t tx="maphew.20130608014320.1745">hwnd = 0 # parent window | |
lpOperation = 'runas' # force elevated UAC prompt | |
lpFile = sys.executable # path to python | |
lpParameters = params # arguments to pass to python | |
lpDirectory = tempfile.gettempdir() # working dir | |
nShowCmd = 1 # window visibility, must be 1 for Leo. | |
print(lpFile, lpParameters) | |
# g.es(lpFile, lpParameters) | |
retcode = ctypes.windll.shell32.ShellExecuteA(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd) | |
msg = 'Exit code: {0} - {1}'.format(retcode, ctypes.FormatError(retcode)) | |
print(msg) | |
g.es(msg) | |
@ | |
Full command executed when script is complete will resemble: | |
C:\Python27\python.exe -ic "import \ntestfile = 'foo'\n...\n#end of file" | |
</t> | |
<t tx="maphew.20130608014320.1983">''' An experimental leo button which executes the currently selected node as a | |
python script after invoking User Account Control (UAC). | |
You could easily bork all your data or even your computer with this: it runs | |
code with admin privileges, no sanity checking, and very limited feedback. By | |
default tracebacks etc. are silently swallowed. The only feedback you'll get is | |
what you explicitly add, and only to gui widgets or files on disk at that, as | |
Windows has probably hidden the console window. | |
1. Copy and paste the full text from below into any Leo outline pane | |
2. save and reopen the workbook to create the button | |
3. navigate to a node and push the [run-elevated] button, but for heavens | |
sake be careful! | |
Limitations: | |
- if Leo was started with pythonw.exe instead of python.exe there | |
is zero feedback for syntax errors etc. | |
- the node script must not have any doublequotes -- " | |
- there may be length limitations, likely about 8k characters | |
'''</t> | |
</tnodes> | |
</leo_file> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment