IDAPython script to copy cursor location to clipboard

Below is a small IDAPython script that will copy the location of the current address you are looking at to your windows clipboard.
I use it to copy paste from IDA to WinDBG which is why it currently uses ‘module + offset’, for example ‘ntdll + 0x1234′. This way it doesn’t matter if the module in WinDBG is loaded at a different offset then your module in IDA.

import idaapi
import ctypes

COPYHOTKEY = 'z'

strcpy = ctypes.cdll.msvcrt.strcpy
ocb = ctypes.windll.user32.OpenClipboard    #Basic Clipboard functions
ecb = ctypes.windll.user32.EmptyClipboard
gcd = ctypes.windll.user32.GetClipboardData
scd = ctypes.windll.user32.SetClipboardData
ccb = ctypes.windll.user32.CloseClipboard
ga = ctypes.windll.kernel32.GlobalAlloc    # Global Memory allocation
gl = ctypes.windll.kernel32.GlobalLock     # Global Memory Locking
gul = ctypes.windll.kernel32.GlobalUnlock
GMEM_DDESHARE = 0x2000 

def Paste( data ):
  ocb(None) # Open Clip, Default task
  ecb()
  hCd = ga( GMEM_DDESHARE, len(data)+1 )
  pchData = gl(hCd)
  strcpy(ctypes.c_char_p(pchData),data)
  gul(hCd)
  scd(1,hCd)
  ccb()

def CopyEA():
  myModuleName = GetInputFile()
  MyModuleShortName = re.sub(r'\.[^.]*$','',GetInputFile())
  myModuleBase = idaapi.get_imagebase()
  myOffset = ScreenEA() - myModuleBase
  Paste(MyModuleShortName + " + " + hex(myOffset))


print "Press '%s' to copy location of effective address to clipboard()"%COPYHOTKEY
idaapi.CompileLine('static _copy_ea() { RunPythonStatement("CopyEA()"); }')
idc.AddHotkey(COPYHOTKEY,"_copy_ea")

Its maybe a little bit ugly, but the other option to use the clipboard was to install Python for Win32.
Feel free to use and change it into what ever you need.

Comments (1)

newklearOctober 20th, 2010 at 5:02

intersting..

Leave a comment

Your comment

Spam protection by WP Captcha-Free