import idc import idaapi import sys def main(): # 1. Get the effective address (EA) of the current cursor position. ea = idaapi.get_screen_ea() if ea == idaapi.BADADDR: print("[-] Error: Could not get current effective address.") return # 2. Read the value at the effective address. # We will try to read a 4-byte (DWORD) value as a common default. # You might need to adjust the size (e.g., idc.get_qword for 8 bytes, idc.get_byte for 1 byte) # depending on what you are highlighting. try: # Use get_wide_dword to read 4 bytes (DWORD). # Note: If you highlight an immediate value, you might need more complex logic # (like analyzing the instruction's operand) to get the value directly. # This approach assumes you are highlighting the address of the variable's value in memory. value = idc.get_wide_dword(ea) except Exception as e: print(f"[-] Error reading memory at 0x{ea:X}: {e}") return # 3. Format the value as a hexadecimal string. # The 'X' format specifier produces a hexadecimal string (e.g., 'DEADBEEF'). # The '0' fills with leading zeros up to the required width (8 for DWORD). hex_string = f"{value:08X}" # 4. Print the output in the format you requested: # "Passing value as hex string: DEADBEEF" print(f"[+] Effective Address: 0x{ea:X}") print(f"[+] Extracted Value (4-bytes): 0x{value:X}") print(f"[+] Passing value as hex string: {hex_string}") if __name__ == "__main__": # Ensure the environment is set up for IDAPython execution print("--- Running Script to Get Highlighted Value ---") main() print("--- Script Finished ---")