Last active
May 15, 2022 02:32
-
-
Save mmun/688d979e8b1543b490fda236abfc0df9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import base64 | |
import subprocess | |
def encode_powershell_command(command: str) -> str: | |
return base64.b64encode(command.encode('utf-16-le')).decode('utf-8') | |
def run_powershell_command(command: str, as_admin: bool = False): | |
encoded_command = encode_powershell_command(command) | |
if as_admin: | |
encoded_wrapper_command = encode_powershell_command(f"Start-Process -Verb RunAs powershell -ArgumentList '-EncodedCommand {encoded_command}'") | |
subprocess.run(f"powershell -EncodedCommand {encoded_wrapper_command}") | |
else: | |
subprocess.run(f"powershell -EncodedCommand {encoded_command}") | |
# Example Usage | |
run_powershell_command('Get-NetIPInterface -InterfaceIndex 1') | |
run_powershell_command('Set-NetIPInterface -InterfaceIndex 1', as_admin=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment