subprocess + some useful stuff
Project description
subprocess + some useful stuff
Tested against Windows / Python 3.11 / Anaconda
pip install subprocplus
# Some nice things to do with subprocess
# Includes stuff from:
# https://github.com/zeroSteiner/mayhem
# https://stackoverflow.com/a/29737399/15096247
# https://github.com/hansalemaos/procciao
from subprocplus.subproc import (
create_new_user,
disable_internet_for_user,
remove_user,
CREATIONINFO,
Popen,
PIPE,
time,
CREATION_TYPE_LOGON,
subprocess,
remove_firewall_rules,
)
import sys
username = "NO11"
password = "NOINTERNET"
# removing a user
remove_user(username)
time.sleep(5)
# creating a new user
usern, passw = create_new_user(new_username=username, new_password=password, admin=True)
time.sleep(5)
# disable internet usage for apps for created new user, returns a list of created rules
rules = disable_internet_for_user(
username=username,
password=password,
apps=[r"C:\Windows\System32\curl.exe", r"C:\msys64\usr\bin\wget.exe"],
new_display_name1=None,
new_display_name2=None,
)
time.sleep(5)
ci = CREATIONINFO(
CREATION_TYPE_LOGON, # CREATIONINFO for logged on user (from https://stackoverflow.com/a/29737399/15096247 )
lpUsername=username,
lpPassword=password,
dwCreationFlags=subprocess.CREATE_NO_WINDOW, # invisible
dwLogonFlags=1,
)
cmd = "powershell.exe"
p1 = Popen(
cmd,
suspended=False,
creationinfo=ci,
stdout=PIPE,
stderr=PIPE,
stdin=PIPE,
print_stdout=True,
print_stderr=True,
) # starting the subprocess as a logged on user
print("Process Id: %d" % p1.pid)
print("Thread Id: %d" % p1._processinfo.dwThreadId)
p1.sendcommand(
"ls",
clean_old=True, # all results are saved in p1.out_dict and p2.err_dict
restart_on_fail=True, # restarts if there is a broken pipe or something else
max_restarts=3,
sleep_after_restart=10,
)
time.sleep(5)
print(
p1.get_last_stdout(clean=True)
) # converts p1.out_dict to a list and clears p1.out_dict
print(
p1.get_last_stderr(clean=True)
) # converts p1.err_dict to a list and clears p1.err_dict
p1.sendcommand(
r"C:\Windows\System32\curl.exe google.com"
) # connection error, because it is blocked
time.sleep(3)
p1.sendcommand(
r"C:\msys64\usr\bin\wget.exe google.com"
) # connection error, because it is blocked
time.sleep(3)
remove_firewall_rules(rules) # deleting the created firewall rules
time.sleep(5)
p1.sendcommand(r"C:\Windows\System32\curl.exe google.com") # no more connection error
time.sleep(3)
p1.sendcommand(r"C:\msys64\usr\bin\wget.exe google.com") # no more connection error
time.sleep(3)
print(p1.get_last_stdout(clean=True))
print(p1.get_last_stderr(clean=True))
p1.stdin.close() # provoking an error, because it is closed
time.sleep(5)
p1.sendcommand(
"dir",
clean_old=True,
restart_on_fail=True,
max_restarts=3,
sleep_after_restart=10,
) # reconnects after error
time.sleep(5)
print(p1.get_last_stdout(clean=True))
ci1 = CREATIONINFO(
CREATION_TYPE_LOGON,
lpUsername=username,
lpPassword=password,
dwCreationFlags=subprocess.CREATE_NO_WINDOW,
dwLogonFlags=1,
)
p2 = Popen(
r"C:\Windows\System32\cmd.exe",
suspended=True, # doesn't start
creationinfo=ci1,
stdout=PIPE,
stderr=PIPE,
stdin=PIPE,
)
print("Process Id: %d" % p2.pid)
print("Thread Id: %d" % p2._processinfo.dwThreadId)
assert not p2._child_started
input("Press enter to start")
p2.start() # there we go
assert p2._child_started
time.sleep(5)
# https://github.com/zeroSteiner/mayhem
for key, item in p2.maps.items():
try:
# reads the memory. If NumPy is installed, it will be much faster,
# because it uses NumPy's buffer protocol.
# Be careful when using it without NumPy, it might print forever.
mymem = p2.read_memory(key, item.size).view("V1").view("S1")
print(mymem)
except Exception as e: # some protected areas can't be read
sys.stderr.write(f"{e}")
sys.stderr.flush()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
subprocplus-0.10.tar.gz
(569.4 kB
view details)
Built Distribution
subprocplus-0.10-py3-none-any.whl
(592.3 kB
view details)
File details
Details for the file subprocplus-0.10.tar.gz
.
File metadata
- Download URL: subprocplus-0.10.tar.gz
- Upload date:
- Size: 569.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a9615bea26e28f23d67d68cb99d129d95910e45b78b3329b69d6a162bdbb0c67 |
|
MD5 | 14730632d2305c0860dd06b0f89266eb |
|
BLAKE2b-256 | 6e6e64f5b61a9cbdf81d3cb6cfd3a8de12b3dda3692d462ec58cd1c90801a4eb |
File details
Details for the file subprocplus-0.10-py3-none-any.whl
.
File metadata
- Download URL: subprocplus-0.10-py3-none-any.whl
- Upload date:
- Size: 592.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0def1f15bf0df966889c9716af9e7566831c077d02fb76c3733e5dbc81ce7fc3 |
|
MD5 | 23132457a49a4875947b1646f37d2299 |
|
BLAKE2b-256 | ad10ad0aec7bd3d9b38559dd34ae6337bd9153982b3b89a27eeeb7bba225d930 |