Skip to main content

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()

Metadata

Release files for subprocplus 0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for subprocplus 0.10
File Size Uploaded Provenance Actions
subprocplus-0.10.tar.gz 569.4 kB Without provenance Details for subprocplus-0.10.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for subprocplus 0.10
File Interpreter ABI Platform Actions
subprocplus-0.10-py3-none-any.whl Python 3 none any Details for subprocplus-0.10-py3-none-any.whl

Total release size: 1.2 MB

Release files / subprocplus-0.10.tar.gz

Download URL subprocplus-0.10.tar.gz
Size 569.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a9615bea26e28f23d67d68cb99d129d95910e45b78b3329b69d6a162bdbb0c67
BLAKE2b-256 checksum
How to use checksums
6e6e64f5b61a9cbdf81d3cb6cfd3a8de12b3dda3692d462ec58cd1c90801a4eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / subprocplus-0.10-py3-none-any.whl

Download URL subprocplus-0.10-py3-none-any.whl
Size 592.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0def1f15bf0df966889c9716af9e7566831c077d02fb76c3733e5dbc81ce7fc3
BLAKE2b-256 checksum
How to use checksums
ad10ad0aec7bd3d9b38559dd34ae6337bd9153982b3b89a27eeeb7bba225d930
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Security for subprocplus 0.10

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.10 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page