Created
April 14, 2024 00:06
-
-
Save ascpixi/70d98e132322b70c977b3e9eb74c5904 to your computer and use it in GitHub Desktop.
Script for displaying individual PT indices of a x86-64 virtual address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from colorama import Fore | |
from colorama import Style | |
va = int(sys.argv[1], base=0) | |
sign_extend_mask = 0b1111111111111111_000000000_000000000_000000000_000000000_000000000000 | |
pml4_mask = 0b0000000000000000_111111111_000000000_000000000_000000000_000000000000 | |
pdp_mask = 0b0000000000000000_000000000_111111111_000000000_000000000_000000000000 | |
pd_mask = 0b0000000000000000_000000000_000000000_111111111_000000000_000000000000 | |
pt_mask = 0b0000000000000000_000000000_000000000_000000000_111111111_000000000000 | |
page_mask = 0b0000000000000000_000000000_000000000_000000000_000000000_111111111111 | |
c = (va & sign_extend_mask) >> 48 | |
sign_valid = False | |
last_bit = (va & pml4_mask) >> 47 | |
if (last_bit == 0 and c == 0) or (last_bit == 1 and c == 0b1111111111111111): | |
sign_valid = True | |
strva = f"{va:064b}" | |
colored_va = f"{Fore.LIGHTBLUE_EX}{strva[16:25]}{Fore.LIGHTCYAN_EX}{strva[25:34]}{Fore.LIGHTBLUE_EX}{strva[34:43]}{Fore.LIGHTCYAN_EX}{strva[43:52]}{Fore.LIGHTBLUE_EX}{strva[52:64]}" | |
print(f"Stats for virtual address {Fore.LIGHTBLACK_EX if sign_valid else Fore.LIGHTRED_EX}{strva[:16]}{Fore.LIGHTWHITE_EX}{colored_va}{Style.RESET_ALL}:") | |
print(f" └ Sign extend ──┴ PML4 ──┴ PDP ───┴ PD ────┴ PT ────┴ Offset ───") | |
print(f"Sign extend: {c} (0x{c:02x}), {'valid' if sign_valid else f'{Fore.RED}invalid{Style.RESET_ALL}'}") | |
c = (va & pml4_mask) >> 39 | |
print(f"PML4: {c} (0x{c:02x})") | |
c = (va & pdp_mask) >> 30 | |
print(f"PDP: {c} (0x{c:02x})") | |
c = (va & pd_mask) >> 21 | |
print(f"PD: {c} (0x{c:02x})") | |
c = (va & pt_mask) >> 12 | |
print(f"PT: {c} (0x{c:02x})") | |
c = (va & page_mask) | |
print(f"Page Offset (assuming 4KiB): {c} (0x{c:02x})") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment