pythonpluginsida

IDA Pro change color of variables in pseudocode


Very basic Ida Pro plugin that changes color of MyVar123 inside pseudocode window.

Problem is this approach is limited to using ida_lines.SCOLOR_... constants for color. How to define my own text color e.g. #00FF00 or any other way?

I read trough the SDK and i find no function that responsible for custom color codes.

import idaapi, ida_kernwin, ida_lines

class ColorizeVariable(ida_kernwin.action_handler_t):

    def __init__(self):
        ida_kernwin.action_handler_t.__init__(self)

    def activate(self, ctx):

        if ida_kernwin.get_widget_type(ctx.widget) == ida_kernwin.BWN_PSEUDOCODE:
            vu = idaapi.get_widget_vdui(ctx.widget)
            pc = vu.cfunc.get_pseudocode()


            find = "MyVar"

            vu.refresh_view(False)


            for sl in pc:
                sl.line = sl.line.replace(find, ida_lines.COLSTR(find, ida_lines.SCOLOR_DNAME))
                #sl.line = sl.line.replace(find, f'{ida_lines.COLOR_ON}{ida_lines.COLOR_CHAR}{find}{ida_lines.COLOR_OFF}{ida_lines.COLOR_CHAR}')  # broken
                #sl.line = sl.line.replace(find, f'{ida_lines.COLOR_ON}\x0A{find}{ida_lines.COLOR_OFF}\x0A')  #working
                #sl.line = sl.line.replace(find, f'\1\x0A{find}\2\x0A')  #short version

        return 0

    def update(self, ctx):
        return ida_kernwin.AST_ENABLE_ALWAYS




class ida_plugin_container(idaapi.plugin_t):

    flags         = idaapi.PLUGIN_UNL
    comment       = 'plugin comment'
    help          = 'help message'
    wanted_name   = "myPlugin"
    wanted_hotkey = 'Shift-Q'

    def init(self):

        action_desc = idaapi.action_desc_t(
            'myAction',
            'Colorize Variable',
            ColorizeVariable(),
            'Ctrl+R',
            'Colorize Variable in Pseudocode',
            10)

        ida_kernwin.unregister_action('myAction')
        ida_kernwin.register_action(action_desc)

        ida_kernwin.attach_action_to_toolbar("SearchToolBar", 'myAction')
        print('loaded')
        return idaapi.PLUGIN_OK


    def run(self, arg):
        pass


    def term(self):
        pass

def PLUGIN_ENTRY():
    return ida_plugin_container()

Solution

  • Seems there's no option for using RGB color codes or any other means of specifying custom color?

    Reading this doc's

    A typical color sequence looks like this:

    COLOR_ON COLOR_xxx text COLOR_OFF COLOR_xxx

    The first 2 items turn color 'xxx' on, then the text follows, and the color is turned off by two last items.

    i discovered an additional way to use predefined color codes trough COLOR_xxx constants (that happen to not work on IDA 8.3 or i don't know how to use them correctly).

    Example this line (also in above code example) does not work.

    sl.line.replace(find, f'{ida_lines.COLOR_ON}{ida_lines.COLOR_CHAR}{find}{ida_lines.COLOR_OFF}{ida_lines.COLOR_CHAR}')
    

    Working way to use predefined color codes is:

    sl.line.replace(find, f'{ida_lines.COLOR_ON}\x0A{find}{ida_lines.COLOR_OFF}\x0A')
    

    where ida_lines.COLOR_ON is color open tag, \x0A is color code (predefined colors are between hex values \x01 and \x27), ida_lines.COLOR_OFF color close tag, \x0A color code tag repeated (required by ida).

    Short version:

    sl.line.replace(find, f'\1\x0A{find}\2\x0A')
    

    Good thing though colors can be redefined trough menu Options > Colors > Disassembly in the Text Colors drop-down.

    Settings are saved into file %APPDATA%\Hex-Rays\IDA Pro\themes\dark\user.css

    Here's the Color code to menu item mapping

    Color #  HEX     COLOR_xxx               IDA Menu Item                        CSS Class                         
    1        '\x01'  COLOR_DEFAULT           Default                              qproperty-line-fg-default         
    2        '\x02'  COLOR_REGCMT            Regular comment                      qproperty-line-fg-regular-comment 
    3        '\x03'  COLOR_RPTCMT            Repeatable commen                    qproperty-line-fg-repeatable-comment
    4        '\x04'  COLOR_AUTOCMT           Automatic comment                    qproperty-line-fg-automatic-comment
    5        '\x05'  COLOR_INSN              Instruction                          qproperty-line-fg-insn            
    6        '\x06'  COLOR_DATNAME           Dummy Data Name                      qproperty-line-fg-dummy-data-name 
    7        '\x07'  COLOR_DNAME             Regular Data Name                    qproperty-line-fg-regular-data-name
    8        '\x08'  COLOR_DEMNAME           Demangled Name                       qproperty-line-fg-demangled-name  
    9        '\x09'  COLOR_SYMBOL            Punctuation                          qproperty-line-fg-punctuation     
    10       '\x0A'  COLOR_CHAR              Char constant                        qproperty-line-fg-charlit-in-insn 
    11       '\x0B'  COLOR_STRING            String constant                      qproperty-line-fg-strlit-in-insn  
    12       '\x0C'  COLOR_NUMBER            Numeric constant                     qproperty-line-fg-numlit-in-insn  
    13       '\x0D'  COLOR_VOIDOP            Void operand                         qproperty-line-fg-void-opnd       
    14       '\x0E'  COLOR_CREF              Code reference                       qproperty-line-fg-code-xref       
    15       '\x0F'  COLOR_DREF              Data reference                       qproperty-line-fg-data-xref       
    16       '\x10'  COLOR_CREFTAIL          Code reference to tail byte          qproperty-line-fg-code-xref-to-tail
    17       '\x11'  COLOR_DREFTAIL          Data reference to tail byte          qproperty-line-fg-data-xref-to-tail
    18       '\x12'  COLOR_ERROR             Error or problem                     qproperty-line-fg-error           
    19       '\x13'  COLOR_PREFIX            Line prefix Regular function         qproperty-line-pfx-func           
    20       '\x14'  COLOR_BINPREF           Opcode bytes                         qproperty-line-fg-opcode-byte     
    21       '\x15'  COLOR_EXTRA             Extra line                           qproperty-line-fg-extra-line      
    22       '\x16'  COLOR_ALTOP             Alternative operand                  qproperty-line-fg-alt-opnd        
    23       '\x17'  COLOR_HIDNAME           Hidden name                          qproperty-line-fg-hidden          
    24       '\x18'  COLOR_LIBNAME           Library function name                qproperty-line-fg-libfunc         
    25       '\x19'  COLOR_LOCNAME           Local variable name                  qproperty-line-fg-locvar          
    26       '\x1A'  COLOR_CODNAME           Dummy code name                      qproperty-line-fg-dummy-code-name 
    27       '\x1B'  COLOR_ASMDIR            Assembler directive                  qproperty-line-fg-asm-directive   
    28       '\x1C'  COLOR_MACRO             Macro                                qproperty-line-fg-macro           
    29       '\x1D'  COLOR_DSTR              String constant in data directive    qproperty-line-fg-strlit-in-data  
    30       '\x1E'  COLOR_DCHAR             Char constant in data directive      qproperty-line-fg-charlit-in-data 
    31       '\x1F'  COLOR_DNUM              Numeric constant in data directive   qproperty-line-fg-numlit-in-data 
    32       '\x20'  COLOR_KEYWORD           Keywords (offset, byte ptr, near)    qproperty-line-fg-keyword         
    33       '\x21'  COLOR_REG               Register name                        qproperty-line-fg-register-name   
    34       '\x22'  COLOR_IMPNAME           Imported name                        qproperty-line-fg-import-name     
    35       '\x23'  COLOR_SEGNAME           Segment name                         qproperty-line-fg-segment-name    
    36       '\x24'  COLOR_UNKNAME           Dummy unknown name                   qproperty-line-fg-dummy-unknown-name
    37       '\x25'  COLOR_CNAME             Regular Code Name                    qproperty-line-fg-code-name       
    38       '\x26'  COLOR_UNAME             Regular Unknown Name                 qproperty-line-fg-unknown-name    
    39       '\x27'  COLOR_COLLAPSED         Collapsed line                       qproperty-line-fg-collapsed-line