Zum Hauptinhalt springen
Echtzeit-Radar & Feeds
Alle RSS Feeds ➔
👥 Community & Social
•
Sicherheitslücken (CVE)IT Security News Hourly Summary 2026-09-29 11h : 7 posts(29.09.2026 um 11:00 Uhr)
•
IT Security NachrichtenSeptember 2026 Cyber Attacks Timeline(29.09.2026 um 11:01 Uhr)
•••
IT Security NachrichtenCloudflare’s EmDash 1.0 makes sandboxed plugins ask for access first(29.09.2026 um 11:03 Uhr)
•
IT Security NachrichtenEngineering velocity is a competitive advantage in modern cybersecurity(29.09.2026 um 11:00 Uhr)
•
Sicherheitslücken (CVE)Kiteworks patches critical flaw, brings customer systems online(29.09.2026 um 11:04 Uhr)
•
IT Security NachrichtenGroßrazzia gegen Mogel-Handy-Ring: 300 Millionen Euro Schaden(29.09.2026 um 11:14 Uhr)
•
Malware / Trojaner / VirenMalware-Schutz auf QNAP-NAS richtig steuern(29.09.2026 um 11:00 Uhr)
••
Sicherheitslücken (CVE)IT Security News Hourly Summary 2026-09-29 11h : 7 posts(29.09.2026 um 11:00 Uhr)
•
IT Security NachrichtenSeptember 2026 Cyber Attacks Timeline(29.09.2026 um 11:01 Uhr)
•••
IT Security NachrichtenCloudflare’s EmDash 1.0 makes sandboxed plugins ask for access first(29.09.2026 um 11:03 Uhr)
•
IT Security NachrichtenEngineering velocity is a competitive advantage in modern cybersecurity(29.09.2026 um 11:00 Uhr)
•
Sicherheitslücken (CVE)Kiteworks patches critical flaw, brings customer systems online(29.09.2026 um 11:04 Uhr)
•
IT Security NachrichtenGroßrazzia gegen Mogel-Handy-Ring: 300 Millionen Euro Schaden(29.09.2026 um 11:14 Uhr)
•
Malware / Trojaner / VirenMalware-Schutz auf QNAP-NAS richtig steuern(29.09.2026 um 11:00 Uhr)
•
Intelligence View
⚡ tsecurity.de Intelligence

How to run Python code with admin privileges.

Sometimes, your Python script needs to perform actions that require administrator privileges, such as managing services, modifying system settings, or running tools like net session. On Windows, this typically involves triggering a UAC…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

Sometimes, your Python script needs to perform actions that require administrator privileges, such as managing services, modifying system settings, or running tools like net session. On Windows, this typically involves triggering a UAC (User Account Control) prompt.



In this post, we'll walk through how to automatically elevate your Python script to admin, run privileged commands, and gracefully fall back when permissions are denied.






🧩 The Python Script






import ctypes
import sys
import subprocess

def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False

def request_admin_privileges():
try:
result = ctypes.windll.shell32.ShellExecuteW(
None, "runas", sys.executable, " ".join(sys.argv), None, 1
)
return result > 32 # Success if result > 32
except Exception as e:
print("Error:", str(e))
return False

# === Main Logic ===
if not is_admin():
success = request_admin_privileges()
if success:
print("Requested admin privileges. Relaunching...")
else:
print("Admin privilege request was denied.")
sys.exit()
else:
# Relaunched with admin rights
print("Running with admin privileges!")

# Example privileged command
subprocess.run("net session", shell=True)

input("Press Enter to exit...")









🔍 How It Works




  • ctypes.windll.shell32.IsUserAnAdmin() checks whether the script is running as administrator.

  • If not, ShellExecuteW(..., "runas", ...) relaunches the same script using UAC.

  • Once elevated, the script re-enters from the top but now is_admin() will return True.

  • ✅ Note: net session is a classic example of a command that only works if the script is elevated.






Thanks For Reading....

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten How to run Python code with admin privileges.

Thematisch verwandte Begriffe: Python, code, with, admin · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

💬 Kommentare werden geladen…
Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-102240 | A vulnerability was found in Netcore NAP930 0.1.241010.141410. This aff…
Advisory →
tsecurity.de Icon
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag