xosski

What the DOS?

Jan 1st, 2026
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. import ctypes
  2. from ctypes import Structure, windll, byref, POINTER, sizeof
  3. from ctypes.wintypes import DWORD, HANDLE, LPVOID, LPWSTR, ULONG
  4.  
  5. # --- Structure Definitions ---
  6.  
  7. class MIB_TCPROW_OWNER_PID(Structure):
  8. _fields_ = [
  9. ("dwState", DWORD),
  10. ("dwLocalAddr", DWORD),
  11. ("dwLocalPort", DWORD),
  12. ("dwRemoteAddr", DWORD),
  13. ("dwRemotePort", DWORD),
  14. ("dwOwningPid", DWORD),
  15. ]
  16.  
  17. class MIB_UDPROW_OWNER_PID(Structure):
  18. _fields_ = [
  19. ("dwLocalAddr", DWORD),
  20. ("dwLocalPort", DWORD),
  21. ("dwOwningPid", DWORD),
  22. ]
  23.  
  24. class IMAGE_DOS_HEADER(Structure):
  25. _fields_ = [
  26. ("e_magic", ctypes.c_uint16),
  27. ("e_cblp", ctypes.c_uint16),
  28. ("e_cp", ctypes.c_uint16),
  29. ("e_lfanew", DWORD),
  30. ]
  31.  
  32. class IMAGE_DATA_DIRECTORY(Structure):
  33. _fields_ = [
  34. ("VirtualAddress", ctypes.c_uint32),
  35. ("Size", ctypes.c_uint32),
  36. ]
  37.  
  38. class IMAGE_OPTIONAL_HEADER(Structure):
  39. _fields_ = [
  40. ("Magic", ctypes.c_uint16),
  41. ("AddressOfEntryPoint", ctypes.c_uint32),
  42. ("DataDirectory", IMAGE_DATA_DIRECTORY * 16),
  43. ]
  44.  
  45. class IMAGE_NT_HEADERS(Structure):
  46. _fields_ = [
  47. ("Signature", ctypes.c_uint32),
  48. ("FileHeader", ctypes.c_uint32),
  49. ("OptionalHeader", POINTER(IMAGE_OPTIONAL_HEADER)),
  50. ]
  51.  
  52. # --- Example: Open a file and read IMAGE_DOS_HEADER ---
  53.  
  54. def read_dos_header(path):
  55. with open(path, 'rb') as f:
  56. dos_header = IMAGE_DOS_HEADER()
  57. f.readinto(dos_header)
  58. print(f'DOS Magic: {hex(dos_header.e_magic)}')
  59. print(f'DOS LFA New: {hex(dos_header.e_lfanew)}')
  60.  
  61. # --- Example Usage (for a PE file, like notepad.exe) ---
  62. if __name__ == "__main__":
  63. exe_path = r"C:\Windows\System32\notepad.exe" # Adjust if needed
  64. try:
  65. read_dos_header(exe_path)
  66. except Exception as e:
  67. print("Error:", e)
Tags: DoS
Advertisement
Add Comment
Please, Sign In to add comment