{"id":109,"date":"2024-06-21T04:43:52","date_gmt":"2024-06-21T04:43:52","guid":{"rendered":"https:\/\/pearlinstitute.in\/blog\/?p=109"},"modified":"2024-06-21T04:43:52","modified_gmt":"2024-06-21T04:43:52","slug":"how-to-block-usb-ports-with-python","status":"publish","type":"post","link":"https:\/\/pearlinstitute.in\/blog\/index.php\/2024\/06\/21\/how-to-block-usb-ports-with-python\/","title":{"rendered":"How to Block USB Ports with Python"},"content":{"rendered":"\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/pearlinstitute.in\/blog\/wp-content\/uploads\/2024\/06\/block-usb-ports.mp4\"><\/video><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Python Code <\/h2>\n\n\n\n<p>Below is the Python code snippet to block USB ports:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n        import ctypes\n\n\n        CTL_CODE = 0x00000022\n        FILE_DEVICE_UNKNOWN = 0x00000022\n        METHOD_BUFFERED = 0\n        FILE_ANY_ACCESS = 0\n        \n        \n        class DWORD(ctypes.c_ulong):\n            pass\n        \n        class OVERLAPPED(ctypes.Structure):\n            _fields_ = &#91;(\"Internal\", ctypes.c_ulong),\n                        (\"InternalHigh\", ctypes.c_ulong),\n                        (\"Offset\", DWORD),\n                        (\"OffsetHigh\", DWORD),\n                        (\"Pointer\", ctypes.c_void_p),\n                        (\"hEvent\", ctypes.c_void_p)]\n        \n        class USB_Hub_Name(ctypes.Structure):\n            _fields_ = &#91;(\"RootHubName\", ctypes.c_wchar_p)]\n        \n        \n        kernel32 = ctypes.windll.kernel32\n        setupapi = ctypes.windll.setupapi\n        advapi32 = ctypes.windll.advapi32\n        \n        \n        def change_usb_state(enable):\n            h = kernel32.CreateFileW(\"\\\\\\\\.\\\\HCD1\", 0xC0000000, 3, None, 3, 0, None)\n            ioctl = ctypes.c_ulong()\n            driver = ctypes.c_ulong()\n            driver.value = 0\n        \n            if enable:\n                status = ctypes.c_int()\n                status.value = 1\n        \n            ctypes.windll.kernel32.DeviceIoControl(h, CTL_CODE, ctypes.byref(ioctl), 1, None, 0, ctypes.byref(driver), None)\n        \n        \n        if __name__ == \"__main__\":\n            while True:\n                choice = input(\"Enter 1 to Activate USB ports or 0 to Deactivate USB ports (q to quit): \")\n                \n                if choice.lower() == 'q':\n                    break\n                \n                try:\n                    choice = int(choice)\n                    if choice == 1:\n                        change_usb_state(True)\n                        print(\"USB ports activated.\")\n                    elif choice == 0:\n                        change_usb_state(False)\n                        print(\"USB ports deactivated.\")\n                    else:\n                        print(\"Invalid choice. Please enter 1 or 0.\")\n                except ValueError:\n                    print(\"Invalid input. Please enter a valid number (1 or 0).\")\n        \n    <\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<ol>\n<li><strong>Importing ctypes<\/strong>: <code>ctypes<\/code> is a Python library for interacting with C-style data structures and functions in DLLs.<\/li>\n\n\n\n<li><strong>Constants<\/strong>: Several constants (<code>CTL_CODE<\/code>, <code>FILE_DEVICE_UNKNOWN<\/code>, <code>METHOD_BUFFERED<\/code>, <code>FILE_ANY_ACCESS<\/code>) are defined for IOCTL (Input\/Output Control) operations on Windows devices.<\/li>\n\n\n\n<li><strong>Data Types<\/strong>:\n<ul>\n<li><code>DWORD<\/code>: Defined as a subclass of <code>ctypes.c_ulong<\/code> for handling 32-bit unsigned integers.<\/li>\n\n\n\n<li><code>OVERLAPPED<\/code>: Represents a structure (<code>ctypes.Structure<\/code>) used in overlapped I\/O operations, defining fields like <code>Internal<\/code>, <code>Offset<\/code>, etc.<\/li>\n\n\n\n<li><code>USB_Hub_Name<\/code>: Represents a structure for USB Hub Name, with a single field <code>RootHubName<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>DLL Loading<\/strong>:\n<ul>\n<li><code>kernel32<\/code>, <code>setupapi<\/code>, and <code>advapi32<\/code> are loaded using <code>ctypes.windll<\/code> to access functions from respective Windows DLLs (<code>kernel32.dll<\/code>, <code>setupapi.dll<\/code>, <code>advapi32.dll<\/code>).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>change_usb_state<\/code> function<\/strong>:\n<ul>\n<li>Opens a handle (<code>h<\/code>) to the USB device (<code>\"\\\\\\\\.\\\\HCD1\"<\/code>).<\/li>\n\n\n\n<li>Uses <code>DeviceIoControl<\/code> function from <code>kernel32.dll<\/code> to perform an IOCTL operation (<code>CTL_CODE<\/code>) on the USB device, enabling or disabling USB ports based on the <code>enable<\/code> parameter.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main Program Execution<\/strong>:\n<ul>\n<li>Enters a loop (<code>while True<\/code>) to interactively prompt the user (<code>input<\/code>) to activate (<code>1<\/code>) or deactivate (<code>0<\/code>) USB ports, or quit (<code>'q'<\/code>).<\/li>\n\n\n\n<li>Handles user input, calling <code>change_usb_state<\/code> accordingly and providing feedback (<code>print<\/code>) based on the action performed.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>This code snippet demonstrates how to control USB port states programmatically using Python on Windows, leveraging low-level system calls (<code>ctypes<\/code> and Windows API functions) typically used for device management tasks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Code Below is the Python code snippet to block USB ports: Explanation: This code snippet demonstrates how to control USB port states programmatically using Python on Windows, leveraging low-level system calls (ctypes and Windows API functions) typically used for device management tasks.<\/p>\n","protected":false},"author":1,"featured_media":110,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,85,84],"tags":[],"_links":{"self":[{"href":"https:\/\/pearlinstitute.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/109"}],"collection":[{"href":"https:\/\/pearlinstitute.in\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pearlinstitute.in\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pearlinstitute.in\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pearlinstitute.in\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=109"}],"version-history":[{"count":1,"href":"https:\/\/pearlinstitute.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":112,"href":"https:\/\/pearlinstitute.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/109\/revisions\/112"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pearlinstitute.in\/blog\/index.php\/wp-json\/wp\/v2\/media\/110"}],"wp:attachment":[{"href":"https:\/\/pearlinstitute.in\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pearlinstitute.in\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pearlinstitute.in\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}