как мне отобразить результат ida_hexrays.decompile
- User
как мне отобразить результат ida_hexrays.decompile
- Assistant
Чтобы отобразить результат декомпиляции с использованием
ida_hexrays.decompileв IDA Pro, вам нужно следовать нескольким шагам. Предположим, что вы уже работали с IDA Pro и имеете базовые знания работы с плагинами и Python-скриптами в IDA. Вот примерный процесс:-
Откройте файл в IDA Pro: Загрузите исполняемый файл, который вы хотите проанализировать, в IDA Pro.
-
Получите адрес функции: Найдите адрес функции, которую вы хотите декомпилировать. Адрес может быть виден в окне дизассемблера или его можно получить программно.
-
Используйте скрипт на Python: Запустите следующий Python-скрипт в IDA Pro:
import ida_hexrays import idc # Убедитесь, что у вас активна лицензия на Hex-Rays Decompiler if not ida_hexrays.init_hexrays_plugin(): print("Hex-Rays Decompiler is not available.") else: # Замените 0xADDRESS на адрес вашей функции func_address = 0xADDRESS func = ida_funcs.get_func(func_address) if func is None: print("Function not found at the specified address.") else: cfunc = ida_hexrays.decompile(func) if cfunc is None: print("Decompilation failed.") else: lines = [] sv = cfunc.get_pseudocode() for sline in sv: lines.append(sline.line) # Отобразить результат for line in lines: print(line) -
Убедитесь в правильности пути: Убедитесь, что вы правильно заменили
0xADDRESSна фактический адрес функции, которую вы хотите декомпилировать. -
Запустите скрипт: В меню IDA Pro выберите
File -> Script File...и откройте файл с вашим скриптом, либо используйте оконный интерпретатор для ввода скрипта вручную.
Этот скрипт использует API Hex-Rays для получения псевдокода декомпилированной функции и выводит его в консоль. Убедитесь, что у вас установлена лицензия на Hex-Rays, так как без нее декомпиляция работать не будет.
Если в процессе работы возникают ошибки, убедитесь, что все модули импортированы правильно, и адрес функции указан верно.
-
- User
а как открыть вкладку вместо print
- Assistant
Чтобы вместо вывода результата в консоль открыть новую вкладку в IDA Pro с результатами декомпиляции, вы можете использовать API IDA Pro для создания пользовательского окна и отображения в нём результатов. Вот как это можно сделать:
-
Используйте
idaapiдля создания окна:idaapiпредоставляет функции для создания пользовательских окон. -
Отобразите результаты декомпиляции в этом окне.
Вот пример, как это можно сделать:
import ida_hexrays import ida_funcs import ida_kernwin # Проверка доступности Hex-Rays Decompiler if not ida_hexrays.init_hexrays_plugin(): print("Hex-Rays Decompiler is not available.") else: # Замените 0xADDRESS на адрес вашей функции func_address = 0xADDRESS func = ida_funcs.get_func(func_address) if func is None: print("Function not found at the specified address.") else: cfunc = ida_hexrays.decompile(func) if cfunc is None: print("Decompilation failed.") else: # Получение результата декомпиляции как строки sv = cfunc.get_pseudocode() decompiled_code = "\n".join([sline.line for sline in sv]) # Создание новой вкладки в IDA для отображения результата ida_kernwin.msg("Opening decompiled code in a new window...\n") ida_kernwin.open_modal_caption('Decompiled Function', decompiled_code)Разъяснение кода:
ida_hexrays.init_hexrays_plugin(): Проверяет доступность Hex-Rays.ida_funcs.get_func(func_address): Получает функцию по указанному адресу.ida_hexrays.decompile(func): Декомпилирует указанную функцию.- Сохранение псевдокода в переменную
decompiled_code. ida_kernwin.open_modal_caption('Decompiled Function', decompiled_code): Открывает новое окно с результирующим псевдокодом.
Если
ida_kernwin.open_modal_caption()не работает, и вы хотите большего контроля над окном, вы можете использовать другие механизмы API IDA для создания по-настоящему кастомных окон, таких как работа сida_kernwin.simplecustviewer_tили аналогичными функциями для создания собственных UI-элементов.Убедитесь, что у вас правильно установлены все зависимости и адрес функции корректен. Проверьте документацию IDA для более продвинутых интерфейсных функций, если вам понадобятся дополнительные возможности.
-
- User
open_modal_caption не работает в 8-3
- Assistant
В версиях IDA Pro 8.3 и выше
ida_kernwin.open_modal_caption()действительно может не работать, так как это был неполный пример, неправильно использующий API. Зато есть возможность использоватьsimplecustviewer_t, чтобы создавать собственные окна для отображения текста. Вот как это можно сделать:import ida_hexrays import ida_funcs import ida_kernwin class DecompiledCodeViewer(ida_kernwin.simplecustviewer_t): def Create(self, title, content): if not ida_kernwin.simplecustviewer_t.Create(self, title): return False # Добавляем текст (декомпилированный код) for line in content.splitlines(): self.AddLine(line) self.Show() return True # Проверка, доступен ли Hex-Rays Decompiler if not ida_hexrays.init_hexrays_plugin(): print("Hex-Rays Decompiler is not available.") else: # Замените 0xADDRESS на адрес вашей функции func_address = 0xADDRESS func = ida_funcs.get_func(func_address) if func is None: print("Function not found at the specified address.") else: cfunc = ida_hexrays.decompile(func) if cfunc is None: print("Decompilation failed.") else: # Получаем результат декомпиляции как строку sv = cfunc.get_pseudocode() decompiled_code = "\n".join([sline.line for sline in sv]) # Создаем и открываем окно с декомпилированным кодом viewer = DecompiledCodeViewer() viewer.Create("Decompiled Function", decompiled_code)Объяснение кода:
- Создаем пользовательский просмотрщик:
DecompiledCodeViewerявляется подклассомida_kernwin.simplecustviewer_t, который позволяет создавать настраиваемые окна для отображения текста в IDA. - Метод
Create: Настраивает окно, добавляя каждую строку декомпилированного кода через методAddLine. - Показ окна: После добавления всех линий вызывается метод
Show, чтобы отобразить окно на экране. - Подключение к Hex-Rays: Проверяется наличие Hex-Rays и декомпилируется функция по указанному адресу.
Этот подход использует встроенные возможности IDA Pro для создания кастомных окон и может быть адаптирован для более сложных сценариев отображения. Убедитесь, что у вас есть правильный адрес функции, чтобы код выполнялся корректно.
- Создаем пользовательский просмотрщик:
- User
import idautils import ida_allins import ida_kernwin
class DecompiledCodeViewer(ida_kernwin.simplecustviewer_t): def Create(self, title, content): if not ida_kernwin.simplecustviewer_t.Create(self, title): return False
# Добавляем текст (декомпилированный код) for line in content.splitlines(): self.AddLine(line) self.Show() return Truedef find_retaddr(lvars): for var in lvars: if var.name == "retaddr": return var return 0
def get_func_stack_var_info(func_addr): try: decompilation = ida_hexrays.decompile(func_addr) except ida_hexrays.DecompilationFailure: return {} if decompilation is None: return {} rvar = find_retaddr(decompilation.lvars) print(rvar.name) decompiled_code = "\n".join([sline.line for sline in decompilation.get_pseudocode()]) ida_kernwin.msg("Opening decompiled code in a new window...\n") ida_kernwin.open_modules_window() # Создаем и открываем окно с декомпилированным кодом viewer = DecompiledCodeViewer() viewer.Create("RetAddrOptimized", decompiled_code)
return ida_offsetprint(hex(get_func_stack_var_info(here())))
func = ida_funcs.get_func(here())
а можно как-то переопределить значение для декомпиляции?
- Assistant
Да, в Hex-Rays API вы можете вносить изменения в декомпилируемый код, в том числе заменять и изменять переменные или инструкции в декомпиляции. Это делается путём манипуляции с внутренним представлением функции до её вывода в псевдокод. Вот краткий обзор того, как это можно сделать:
Шаги для изменения значения в декомпиляции
-
Получите декомпилированную версию функции: Используйте
ida_hexrays.decompile()для получения объектаcfunc_t, который представляет декомпилированную функцию. -
Итерация по элементам декомпиляции: Объект
cfunc_tсодержит различные элементы и узлы, представляющие структуру декомпилированного кода. -
Изменение значения: Найдите нужный элемент (например, локальную переменную) и измените его.
-
Сохраните изменения: Отобразите модифицированный псевдокод в окне IDA.
Пример на Python
Вот упрощённый пример, который показывает, как можно изменить имя локальной переменной. Это базовый пример, и для более сложных замен потребуется больше работы с API:
import ida_hexrays import ida_kernwin import ida_funcs class DecompiledCodeViewer(ida_kernwin.simplecustviewer_t): def Create(self, title, content): if not ida_kernwin.simplecustviewer_t.Create(self, title): return False for line in content.splitlines(): self.AddLine(line) self.Show() return True # Initialize Hex-Rays if not ida_hexrays.init_hexrays_plugin(): print("Hex-Rays Decompiler is not available.") else: ea = ida_kernwin.here() cfunc = ida_hexrays.decompile(ea) if cfunc is not None: # Найдите и измените локальную переменную for lvar in cfunc.lvars: if lvar.name == "target_var_name": # Измените 'target_var_name' на имя вашей переменной lvar.name = "new_var_name" # Задайте новое имя переменной # Покажите декомпилированный код с изменениями decompiled_code = "\n".join([line.line for line in cfunc.get_pseudocode()]) viewer = DecompiledCodeViewer() viewer.Create("Decompiled Function with Modified Var", decompiled_code) else: print("Failed to decompile the function.")Объяснение кода:
lvars: Содержит локальные переменные в декомпилированной функции.- Изменение имени переменной: В этом примере мы меняем имя локальной переменной
target_var_nameнаnew_var_name. - Отображение изменений: Результаты отображаются в кастомном окне.
Обратите внимание:
- Эти изменения производятся только в декомпиляции и не изменяют саму исполняемую программу.
- Для более сложного изменения (например, изменения типа, добавления инструкций и т.д.) вам понадобится работать с другими частями API, такими как
expr_tилиinsn_t. - При использовании подобного скрипта убедитесь, что вы тестируете его на пробной версии, чтобы предотвратить потерю данных.
-
- User
как мне переопределить микрокод для lvar_t
- Assistant
В Hex-Rays API вы можете изменять микрокод, связанный с определённой функцией, чтобы переопределить поведение переменных, таких как
lvar_t. Это может быть полезно для выполнения анализов или отладок на более низком уровне, чем просто манипулирование псевдокодом. Вот как вы можете это сделать:Шаги для изменения микрокода
-
Получите декомпилированную функцию: Используйте
ida_hexrays.decompile()для получения объектаcfunc_t. -
Работа с микрокодом: Используйте микрокод Hex-Rays, который доступен через
mblock_tиmop_t. -
Изменение микрокода: Найдите нужные инструкции или переменные и измените их.
-
Отобразите изменения: Внесите изменения в представление функции и обновите это в интерфейсе.
Точный процесс может сильно зависеть от того, какое именно изменение вы хотите внести. Вот упрощённый пример, который демонстрирует базовые операции:
Пример изменения микрокода
import ida_hexrays import ida_kernwin # Функция для изменения микрокода def modify_microcode(cfunc): mba = cfunc.get_mba() for blk in mba.blocks: for m_insn in blk: for i in range(mba.vars.size()): lv = mba.vars[i] if lv.name == "target_var_name": # Измените на имя переменной, которую хотите найти lv.name = "new_var_name" # Устанавливаем новое имя return cfunc # Проверка, доступен ли Hex-Rays Decompiler if not ida_hexrays.init_hexrays_plugin(): print("Hex-Rays Decompiler is not available.") else: ea = ida_kernwin.here() cfunc = ida_hexrays.decompile(ea) if cfunc is not None: modified_cfunc = modify_microcode(cfunc) decompiled_code = "\n".join([line.line for line in modified_cfunc.get_pseudocode()]) viewer = ida_kernwin.simplecustviewer_t() if viewer.Create("Modified Decompiled Function"): for line in decompiled_code.splitlines(): viewer.AddLine(line) viewer.Show() else: print("Failed to decompile the function.")Объяснение кода
get_mba(): Получаетmblock_t, представляющий микроблоки функции.- Итерация по блокам: Проходит по микроблокам и операциям, что позволяет находить и изменять нужные сегменты.
- Изменение переменной: Изменяет имя переменной в микрокоде.
- Отображение изменений: Использует
simplecustviewer_tдля отображения конечного результата.
Внимание
- Манипуляция микрокодом и дальнейшая работа с ним требует хорошего знания внутренней структуры Hex-Rays. Это может быть сложным и чреватым ошибками, если неправильно изменять блоки или операции.
- Обязательно делайте резервные копии данных и тестируйте в безопасной среде перед тем, как применять изменения в более критичных сценариях.
API Hex-Rays весьма мощный и гибкий, но также требует понимания того, как каждый компонент связан с другим для эффективного использования.
-
- User
мне нужно не имя а значение
- Assistant
Чтобы изменить значение локальной переменной на уровне микрокода в Hex-Rays, нужно работать с представлением микрокода, таким как
mblock_tиmop_t. Это требует более глубокого понимания структуры микрокода в Hex-Rays и того, как выражения и инструкции манипулируются в этой среде.Работа с Сообщением и Операция в Микрокоде
Когда вы работаете с микрокодом, вам необходимо изменять
mopt_t(операции в микрокоде), связанные с вашимlvar_t, чтобы изменить значение переменной.Пример изменения значения для
lvar_timport ida_hexrays import ida_kernwin # Колбек для изменения значения переменной def modify_microcode(cfunc): mba = cfunc.get_mba() # Пройти по всем блокам и инструкциям for blk in mba.blocks: for m_insn in blk: for i, op in enumerate(m_insn): if op.is_lvar(): # Проверка, является ли операция локальной переменной lvar = op.get_lvar() if lvar.name == "target_var_name": # Укажите имя переменной, значение которой вы хотите изменить # Меняем значение на уровне микрокода - используйте mop_t для этого new_value = ida_hexrays.mop_t() new_value.make_number(42, lvar.width) # Пример: устанавливаем новое числовое значение 42 m_insn.set_m(i, new_value) return cfunc # Проверка, доступен ли Hex-Rays if not ida_hexrays.init_hexrays_plugin(): print("Hex-Rays Decompiler is not available.") else: ea = ida_kernwin.here() cfunc = ida_hexrays.decompile(ea) if cfunc is not None: # Изменяем микрокод modified_cfunc = modify_microcode(cfunc) # Получаем и отображаем модифицированный псевдокод decompiled_code = "\n".join([line.line for line in modified_cfunc.get_pseudocode()]) viewer = ida_kernwin.simplecustviewer_t() if viewer.Create("Modified Decompiled Function"): for line in decompiled_code.splitlines(): viewer.AddLine(line) viewer.Show() else: print("Failed to decompile function.")Объяснение кода
- mop_t и make_number:
mop_t— это объект, представляющий микрокодовую операцию. Методmake_number()устанавливает его значение на заданное число. - is_lvar и get_lvar: Эти функции проверяют, является ли операция локальной переменной, и получают ссылку на неё.
- set_m: Изменяет операцию
mop_tна определённую.
Обратите внимание
- Этот код — это упрощённый пример того, как можно начинать манипулировать микрокодовой операцией. В зависимости от сложности исполнения, возможно, потребуется больше контроля и проверок.
- Изменения на уровне микрокода могут серьёзно повлиять на результирующий код, так что убедитесь, что вы точно знаете цель ваших изменений.
- Hex-Rays API предполагает значительный опыт работы с реверс-инженерингом и концепциями компиляции, прежде чем глубоко взаимодействовать с его внутренними структурами.
- mop_t и make_number:
- User
TypeError: 'mblock_t' object is not iterable
import idautils import ida_allins import ida_kernwin def modify_microcode(cfunc): mba = cfunc.mba # Пройти по всем блокам и инструкциям for blk in mba.blocks: for m_insn in blk: for i, op in enumerate(m_insn): if op.is_lvar(): # Проверка, является ли операция локальной переменной lvar = op.get_lvar() if lvar.name == "retaddr": # Укажите имя переменной, значение которой вы хотите изменить # Меняем значение на уровне микрокода - используйте mop_t для этого new_value = ida_hexrays.mop_t() new_value.make_number(idaapi.get_imagebase()+0x1000, lvar.width) # Пример: устанавливаем новое числовое значение 42 m_insn.set_m(i, new_value) return cfunc class DecompiledCodeViewer(ida_kernwin.simplecustviewer_t): def Create(self, title, content): if not ida_kernwin.simplecustviewer_t.Create(self, title): return False # Добавляем текст (декомпилированный код) for line in content.splitlines(): self.AddLine(line) self.Show() return True def find_retaddr(lvars): for var in lvars: if var.name == "retaddr": return var return 0 def get_func_stack_var_info(func_addr): try: decompilation = ida_hexrays.decompile(func_addr) except ida_hexrays.DecompilationFailure: return {} if decompilation is None: return {} modify_microcode(decompilation) decompiled_code = "\n".join([sline.line for sline in decompilation.get_pseudocode()]) ida_kernwin.msg("Opening decompiled code in a new window...\n") ida_kernwin.open_modules_window() # Создаем и открываем окно с декомпилированным кодом viewer = DecompiledCodeViewer() viewer.Create("RetAddrOptimized", decompiled_code) return ida_offset print(hex(get_func_stack_var_info(here()))) func = ida_funcs.get_func(here()) - Assistant
Ошибка
TypeError: 'mblock_t' object is not iterableуказывает на то, что вы пытаетесь итерировать объект, который не поддерживает итерацию. В данном контексте это происходит, когда вы пытаетесь обойти инструкции вmblock_tс использованиемfor m_insn in blk. Это связано с тем, чтоmblock_tне является самойтерабельным объектом и требует использования специфичных методов для получения списка инструкций.Исправление
Чтобы исправить это, можно использовать доступный интерфейс для итерации инструкций в блоке. В API Hex-Rays для этого используется цикл с
mblock_t.headи проход по инструкции при помощи методаsucc().Вот пример кода, исправленного для обхода инструкций внутри блока:
import ida_hexrays import ida_kernwin def modify_microcode(cfunc): mba = cfunc.mba # Пройти по всем блокам for blk in mba.blocks: # Пройти по последовательной цепочке инструкций в блоке insn = blk.head while insn: for i, op in enumerate(insn.ops): if op.is_lvar(): # Проверка, является ли операция локальной переменной lvar = op.get_lvar() if lvar.name == "retaddr": # Укажите имя переменной, значение которой вы хотите изменить # Меняем значение на уровне микрокода - используйте mop_t для этого new_value = ida_hexrays.mop_t() new_value.make_number(ida_kernwin.get_imagebase()+0x1000, lvar.width) # Пример: устанавливаем новое числовое значение insn.set_m(i, new_value) # Переход к следующей инструкции insn = insn.succ() return cfunc class DecompiledCodeViewer(ida_kernwin.simplecustviewer_t): def Create(self, title, content): if not ida_kernwin.simplecustviewer_t.Create(self, title): return False for line in content.splitlines(): self.AddLine(line) self.Show() return True def get_func_stack_var_info(func_addr): try: decompilation = ida_hexrays.decompile(func_addr) except ida_hexrays.DecompilationFailure: return {} if decompilation is None: return {} modify_microcode(decompilation) decompiled_code = "\n".join([sline.line for sline in decompilation.get_pseudocode()]) ida_kernwin.msg("Opening decompiled code in a new window...\n") viewer = DecompiledCodeViewer() viewer.Create("RetAddrOptimized", decompiled_code) return None # Вызов функции для текущего адреса в IDA print(get_func_stack_var_info(ida_kernwin.here()))Объяснение изменений:
-
Цикл инструкций: Используем инструкция
succ()для перехода к следующей инструкции в блокеmblock_t. -
Операции внутри инструкции: Используем доступ к
insn.opsдля работы с каждой операцией в инструкции. -
Использование
get_imagebase(): Функцияida_kernwin.get_imagebase()обеспечивает правильный доступ для получения базового адреса образа.
Эти изменения решают проблему и обеспечивают правильный обход инструкций в каждом блоке микрокода.
-
- User
for i, op in enumerate(ins): TypeError: 'minsn_t' object is not iterable
- Assistant
Ошибка
TypeError: 'minsn_t' object is not iterableуказывает на то, что вы пытаетесь итерировать объектminsn_t, который не поддерживает итерацию.minsn_tпредставляет одну инструкцию в микрокоде, и её операнды нужно осматривать индивидуально.Вот правильный подход:
-
Доступ к операндам инструкции: Операнды
minsn_tдоступны через поляl,r, иd. -
Проверка и изменение мопов: Используйте поля
l,r, иdдля проверки и изменения операндов.
Ниже представлен исправленный код:
import ida_hexrays import ida_kernwin def modify_microcode(cfunc): mba = cfunc.mba # Пройти по всем блокам for blk in mba.blocks: # Пройти по всем инструкциям в блоке insn = blk.head while insn: # Проверяем каждый операнд инструкции for mop in [insn.l, insn.r, insn.d]: if mop.is_lvar(): # Проверяем, является ли операнд локальной переменной lvar = mop.get_lvar() if lvar.name == "retaddr": # Укажите имя переменной, значение которой вы хотите изменить # Меняем значение на уровне микрокода new_value = ida_hexrays.mop_t() new_value.make_number(ida_kernwin.get_imagebase()+0x1000, lvar.width) # Пример нового значения mop.assign(new_value) # Переход к следующей инструкции insn = insn.succ return cfunc class DecompiledCodeViewer(ida_kernwin.simplecustviewer_t): def Create(self, title, content): if not ida_kernwin.simplecustviewer_t.Create(self, title): return False for line in content.splitlines(): self.AddLine(line) self.Show() return True def get_func_stack_var_info(func_addr): try: decompilation = ida_hexrays.decompile(func_addr) except ida_hexrays.DecompilationFailure: return {} if decompilation is None: return {} modify_microcode(decompilation) decompiled_code = "\n".join([sline.line for line in decompilation.get_pseudocode()]) ida_kernwin.msg("Opening decompiled code in a new window...\n") viewer = DecompiledCodeViewer() viewer.Create("RetAddrOptimized", decompiled_code) return None # Вызов функции для текущего адреса в IDA print(get_func_stack_var_info(ida_kernwin.here()))Объяснение изменений:
- Использование операндов
l,r,d: Каждый операнд инструкции представлен одним из этих полей, и вы можете изменить их, проверяя и устанавливая новые значения. mop.assign(): Методassign()применяется для изменения значения операнда.- Проверка
is_lvar(): Используется для выявления, если операнд связан с локальной переменной.
Это исправление позволяет корректно проверять и изменять операнды каждой инструкции внутри блока.
-
- User
import ida_bytes import ida_segment import ida_xref import ida_allins import ida_hexrays from ida_hexrays import *
import def_util import hexrays_util
def _optimize(blk, ins): if blk: blk.optimize_insn(ins) else: ins.optimize_solo()
def _optimize_and_success(blk, ins, rc=1): _optimize(blk, ins) return rc
For microinstructions with two or more operands (in l and r), check to see
if one of them is a mop_d (result of another microinstruction), where the
provider microinstruction is has opcode type mc. If successful, return the
provider microinstruction and the non-matching micro-operand in the
appropriately-named arguments. Otherwise, return false.
This helper function is useful for performing pattern-matching upon
commutative operations. Without it, we'd have to write each of our patterns
twice: once for when the operation we were looking for was on the left-hand
side, and once for when the operation was on the right-hand side.
def extract_by_opcode_type(ins, mc): op_poss_no_match, poss_match = None, None
# Does the left-hand side contain the operation we're looking for? # Update possNoMatch or possMatch, depending. if not ins.l.is_insn() or ins.l.d.opcode != mc: op_poss_no_match = ins.l else: poss_mach = ins.l.d # Perform the same check on the right-hand side. if not ins.r.is_insn() or ins.r.d.opcode != mc: op_poss_no_match = ins.r else: poss_match = ins.r.d # If both sides matched, or neither side matched, fail. return (op_poss_no_match and poss_match), poss_match, op_poss_no_matchThe obfuscation techniques upon conditional operations have "&1"
miscellaneously present or not present within them. Writing pattern-matching
rules for all of the many possibilities would be extremely tedious. This
helper function reduces the tedium by checking to see whether the provided
microinstruction is "x & 1" (or "1 & x"), and it extracts x (as both an
operand, and, if the operand is a mop_d (result of another
microinstruction), return the provider instruction also.
def tunnel_through_and_1(ins, require_size_1=True, op_is_ok=False): op_inner, inner = None, ins
# Microinstruction must be AND if ins.opcode != m_and: return False, inner, op_inner # One side must be numeric, the other one non-numeric op_and_num, op_and_non_num = ins.find_num_op() if not op_and_num: return False, inner, op_inner # The number must be the value 1 if op_and_num.nnn.value != 1: return False, inner, op_inner if require_size_1 and op_and_num.size != 1: return False, inner, op_inner op_inner = op_and_non_num # If the non-numeric operand is an instruction, extract the # microinstruction and pass that back to the caller. if op_and_non_num.is_insn(): return True, op_and_non_num.d, op_inner # Otherwise, if the non-numeric part wasn't a mop_d, check to see whether # the caller specifically wanted a mop_d. If they did, fail. If the caller # was willing to accept another operand type, return true. return op_is_ok, inner, op_innerdef tunnel_through_and_1_loop(ins): while True: ok, inner, op_inner = tunnel_through_and_1(ins) if ok: ins = inner else: break return ins
The obfuscator implements boolean inversion via "x ^ 1". Hex-Rays, or one of
our other deobfuscation rules, could also convert these to m_lnot
instructions. This function checks to see if the microinstruction passed as
argument matches one of those patterns, and if so, extracts the negated
term as both a micro-operand and a microinstruction (if the negated operand
was of mop_d type).
def extract_logically_negated_term(ins, op_is_ok=False): ins_negated, op_negated = None, None
# Check the m_lnot case. if ins.opcode == m_lnot: # Extract the operand op_negated = ins.l if ins.l.is_insn(): # If the operand was mop_d (i.e., result of another microinstruction), # retrieve the provider microinstruction. Get rid of the pesky "&1" # terms while we're at it. ins_negated = ins.l.d ins_negated = tunnel_through_and_1_loop(ins_negated) return True, ins_negated, op_negated else: # Otherwise, if the operand was not of type mop_d, "success" depends # on whether the caller was willing to accept a non-mop_d operand. ins_negated = None return op_is_ok, ins_negated, op_negated # If the operand wasn't m_lnot, check the m_xor case. if ins.opcode != m_xor: return False, None, None # We're looking for XORs with one constant and one non-constant operand op_xor_num, op_xor_non_num = ins.find_num_op() if not op_xor_num: return False, None, None # The constant must be the 1-byte value 1 if op_xor_num.nnn.value != 1 or op_xor_num.size != 1: return False, None, None # The non-numeric part must also be 1. This check is probably unnecessary. if op_xor_non_num.size != 1: return False, None, None op_negated = op_xor_non_num # If the operand was mop_d (result of another microinstruction), extract # it and remove the &1 terms. if op_xor_non_num.is_insn(): ins_negated = op_xor_non_num.d ins_negated = tunnel_through_and_1_loop(ins_negated) return True, ins_negated, op_negated # Otherwise, if the operand was not of type mop_d, "success" depends on # whether the caller was willing to accept a non-mop_d operand. ins_negated = None return op_is_ok, ins_negated, op_negatedThis function checks whether two conditional terms are logically opposite.
For example, "eax <s 1" and "eax >=s 1" would be considered logically
opposite. The check is purely syntactic; semantically-equivalent conditions
that were not implemented as syntactic logical opposites will not be
considered the same by this function.
def are_conditions_opposite(lhs_cond, rhs_cond):
# Get rid of pesky &1 terms lhs_cond = tunnel_through_and_1_loop(lhs_cond) rhs_cond = tunnel_through_and_1_loop(rhs_cond) # If the conditions were negated via m_lnot or m_xor by 1, get the # un-negated part as a microinstruction. lhs_was_negated, negated, op_negated = extract_logically_negated_term(lhs_cond, op_is_ok=False) if lhs_was_negated: lhs_cond = negated rhs_was_negated, negated, op_negated = extract_logically_negated_term(rhs_cond, op_is_ok=False) if rhs_was_negated: rhs_cond = negated # lhsCond and rhsCond will be None if their original terms were # negated, but the thing that was negated wasn't the result of another # microinstruction. if not lhs_cond or not rhs_cond: return False # If one was negated and the other wasn't, compare them for equality. # If the non-negated part of the negated comparison was identical to # the non-negated comparison, then the conditions are clearly opposite. # I guess this could also be extended by incorporating the logic from # below, but I didn't need to do that in practice. if lhs_was_negated != rhs_was_negated: return lhs_cond.equal_insns(rhs_cond, EQ_IGNSIZE|EQ_IGNCODE) # Otherwise, if both were negated or both were non-negated, compare the # conditionals term-wise. First, ensure that both microoperands are # setXX instructions. if is_mcode_set(lhs_cond.opcode) and is_mcode_set(rhs_cond.opcode): # Now we have two possibilities. if negate_mcode_relation(lhs_cond.opcode) == rhs_cond.opcode: # 1: Condition codes are opposite, LHS and RHS are both equal return lhs_cond.l.equal_mops(rhs_cond.l, EQ_IGNSIZE) \ and lhs_cond.r.equal_mops(rhs_cond.r, EQ_IGNSIZE) if lhs_cond.opcode == rhs_cond.opcode: # 2: Condition codes are the same, LHS and RHS are swapped return lhs_cond.l.equal_mops(rhs_cond.r, EQ_IGNSIZE) \ and lhs_cond.r.equal_mops(rhs_cond.l, EQ_IGNSIZE) # No dice. return Falseclass xor_simplifier_t: def init(self):
# The set of terms in the XOR chain that aren't constant numbers. self.const_ops = [] self.inserted_const = 0 # The set of terms in the XOR chain that aren't constant numbers. self.non_const_ops = [] self.inserted_non_const = 0 # This contains references to the operands that can be zeroed out. I.e., # the terms that were cancelled out, before we actually erase them from # the microcode itself. self.zero_out = [] # Insert a micro-operand into one of the two sets above. Remove # duplicates -- meaning, if the operand we're trying to insert is already # in the set, remove the existing one instead. This is the "cancellation" # in practice. def _insert_in_list(self, _list, op): # Because mop_t types currently cannot be compared or hashed in the # current microcode API, I had to use a slow linear search procedure # to compare the micro-operand we're trying to insert against all # previously-inserted values in the relevant set. for other_op in _list: # If the micro-operand was already in the set, get rid of it. if op.equal_mops(other_op, EQ_IGNSIZE): _list.remove(other_op) # Mark these operands as being able to be deleted. self.zero_out.append(op) self.zero_out.append(other_op) # Couldn't insert. return False # Otherwise, if it didn't match an operand already in the set, insert # it into the set and return true on successful insertion. _list.append(op) return True # Wrapper to insert constant and non-constant terms def insert_const(self, op): self.inserted_const += 1 return self._insert_in_list(self.const_ops, op) def insert_non_const(self, op): self.inserted_non_const += 1 return self._insert_in_list(self.non_const_ops, op) # Insert one micro-operand. If the operand is the result of another XOR # microinstruction, recursively insert the operands being XORed. # Otherwise, insert the micro-operand into the proper set (constant or # non-constant) depending upon its operand type. def insert_op(self, op): # If operand is m_xor microinstruction, recursively insert children if op.t == mop_d and op.d.opcode == m_xor: self.insert_op(op.d.l) self.insert_op(op.d.r) else: # Otherwise, insert it into the constant or non-constant set if op.t == mop_n: self.insert_const(op) else: self.insert_non_const(op) # This function takes an XOR microinstruction and inserts its operands # by calling the function above def insert_ins(self, ins): if ins.opcode == m_xor: # Insert children self.insert_op(ins.l) self.insert_op(ins.r) # Were any cancellations performed? def did_simplify(self): return len(self.zero_out) > 0 # Top-level functionality to simplify an XOR microinstruction. Insert the # instruction, then see if any simplifications could be performed. If so, # remove the simplified terms. def simplify(self, ins, blk): # Only insert XOR instructions if ins.opcode != m_xor: return False self.insert_ins(ins) # Were there common terms that could be cancelled? if not self.did_simplify(): return False # Perform the cancellations by zeroing out the common micro-operands for zo in self.zero_out: zo.make_number(0, zo.size) # Trigger Hex-Rays' ordinary optimizations, which will remove the # XOR 0 terms. Return true. return _optimize_and_success(blk, ins, rc=True)Our pattern-based deobfuscation is implemented as an optinsn_t structure,
which allows us to hook directly into the microcode generation phase and
perform optimizations automatically, whenever code is decompiled.
class obf_compiler_optimizer_t(optinsn_t):
def report_success(self, ins, blk): import inspect stack = inspect.stack() frame, _, _, _, _, _ = stack[1] fun_name = inspect.getframeinfo(frame)[2] if fun_name.startswith("pat_"): parts = fun_name.split("_") parts2 = list(map(str.capitalize, parts)) fun_name = "pat_" + "".join(parts2[1:]) hexrays_util.report_success("%s: blk.start=%08X, ins.ea=%08X" % ( fun_name, blk.start if blk else ida_idaapi.BADADDR, ins.ea)) # This function simplifies microinstruction patterns that look like # either: (x & 1) | (y & 1) ==> (x | y) & 1 # or: (x & 1) ^ (y & 1) ==> (x ^ y) & 1 # Though it may not seem like much of an "obfuscation" or "deobfuscation" # technique on its own, getting rid of the "&1" terms helps reveal other # patterns so they can be deobfuscated. def pat_logic_and_1(self, ins, blk): # Only applies to OR / XOR microinstructions if ins.opcode not in [m_or, m_xor]: return 0 # Only applies when the operands are results of other # microinstructions (since, after all, we are expecting them to be # ANDed by 1, which is represented in terms of a microinstruction # provider mop_d operand). if ins.l.t != mop_d or ins.r.t != mop_d: return 0 # Get rid of & 1. ok is true if there was an &1. ok, ins_left, op_left = tunnel_through_and_1(ins.l.d, True) if not ok: return 0 # Same for right-hand side ok, ins_right, op_right = tunnel_through_and_1(ins.r.d, True) if not ok: return 0 # If we get here, then the pattern matched. # Move the logical operation (OR or XOR) to the left-hand side, # with the operands that have the &1 removed. ins.l.d.opcode = ins.opcode ins.l.d.l.swap(op_left) ins.l.d.r.swap(op_right) # Change the top-level instruction from OR or XOR to AND, and set the # right-hand side to the 1-bit constant value 1. ins.opcode = m_and ins.r.make_number(1, 1) self.report_success(ins, blk) # Return 1 to indicate that we changed the instruction. return 1 # One of the obfuscation patterns involves a subtraction by 1. In the # assembly code, this is implemented by something like: # # add eax, 2 # add eax, ecx ; or whatever # sub eax, 3 # # Usually, Hex-Rays will automatically simplify this to (eax+ecx)-1. # However, I did experience situations where Hex-Rays still represented # the decompiled output as 2+(eax+ecx)-3. This function, then, determines # when Hex-Rays has represented the subtraction as just mentioned. If so, # it extracts the term that is being subtracted by 1. def pat_is_sub_by_1(self, ins): # We're looking for x+(y-z), where x and z are numeric if ins.opcode != m_add: return False # Extract x and (y-z) op_add_num, op_add_non_num = ins.find_num_op(); if not op_add_num: return False # Ensure that the purported (y-z) term actually is a subtraction if op_add_non_num.t != mop_d or op_add_non_num.d.opcode != m_nsub: return False # Extract y and z op_sub_num, op_sub_non_num = op_add_non_num.d.find_num_op() if op_sub_num != op_add_non_num.d.r: return False # the constant must to be on the right side. # Pass y back to the caller op = op_sub_non_num # x-z must be -1, or, equivalently, z-x must be 1. ok = (op_sub_num.nnn.value - op_add_num.nnn.value) == 1 return ok, op # This function performs the following pattern-substitution: # (x * (x-1)) & 1 ==> 0 def pat_mul_sub(self, and_ins, blk): # Topmost term has to be &1. The 1 is not required to be 1-byte large. ins = and_ins ok, ins, _ = tunnel_through_and_1(ins, require_size_1=False) if not ok: return 0 # Looking for multiplication terms if ins.opcode != m_mul: return 0 # We have two different mechanisms for determining if there is a # subtraction by 1. b_was_sub_by_1 = False # Ultimately, we need to find thse things: # ins_sub : subtraction instruction x-1 # op_mul_non_sub : operand of multiply that isn't a subtraction # op_sub_non_num : x from the x-1 instruction # Try first method for locating subtraction by 1, i.e., simply # subtraction by the constant number 1. ok, ins_sub, op_mul_non_sub = extract_by_opcode_type(ins, m_sub) if ok: op_sub_num, op_sub_non_num = ins_sub.find_num_op() if op_sub_num: b_was_sub_by_1 = op_sub_num.nnn.value == 1 # If we didn't find the subtraction, see if we have an add/sub pair # instead, which totals to subtraction minus one. if not b_was_sub_by_1: ok, ins_sub, op_mul_non_sub = extract_by_opcode_type(ins, m_add) if ok: b_was_sub_by_1, op_sub_non_num = self.pat_is_sub_by_1(ins_sub) # If both methods failed, bail. if not b_was_sub_by_1: return 0 # We know we're dealing with (x-1) * y. ensure x==y. if not op_mul_non_sub.equal_mops(op_sub_non_num, EQ_IGNSIZE): return 0 # If we get here, the pattern matched. # Replace the whole multiplication instruction by 0. ins.l.make_number(0, ins.l.size) self.report_success(ins, blk) return _optimize_and_success(blk, and_ins) # This function looks tries to replace patterns of the form # either: (x&y)|(x^y) ==> x|y # or: (x&y)|(y^x) ==> x|y def pat_or_via_xor_and(self, ins, blk): # Looking for OR instructions... if ins.opcode != m_or: return 0 # ... where one side is a compound XOR, and the other is not ... ok, xor_ins, op_non_xor = extract_by_opcode_type(ins, m_xor) if not ok: return 0 # .. and the other side is a compound AND ... if op_non_xor.t != mop_d or op_non_xor.d.opcode != m_and: return 0 # Extract the operands for the AND and XOR terms op_xor_1 = xor_ins.l op_xor_2 = xor_ins.r op_and_1 = op_non_xor.d.l op_and_2 = op_non_xor.d.r # The operands must be equal if (not (op_xor_1.equal_mops(op_and_1, EQ_IGNSIZE) and op_xor_2.equal_mops(op_and_2, EQ_IGNSIZE))) \ or (op_xor_1.equal_mops(op_and_2, EQ_IGNSIZE) and op_xor_2.equal_mops(op_and_1, EQ_IGNSIZE)): return 0 # Move the operands up to the top-level OR instruction ins.l.swap(op_xor_1) ins.r.swap(op_xor_2) self.report_success(ins, blk) return _optimize_and_success(blk, ins) # This pattern replaces microcode of the form (x|!x), where x is a # conditional, and !x is its syntactically-negated version, with 1. def pat_or_negated_same_condition(self, ins, blk): # Only applies to (x|y) if ins.opcode != m_or: return 0 # Only applies when x and y are compound expressions, i.e., results # of other microcode instructions. if ins.l.t != mop_d or ins.r.t != mop_d: return 0 # Ensure x and y are syntactically-opposite versions of the same # conditional. if not are_conditions_opposite(ins.l.d, ins.r.d): return 0 # If we get here, the pattern matched. Replace both sides of OR with # 1, and then call optimize_flat to fold the constants. ins.l.make_number(1, 1) ins.r.make_number(1, 1) self.report_success(ins, blk) return _optimize_and_success(blk, ins) # Replace patterns of the form (x&c)|(~x&d) (when c and d are numbers such # that c == ~d) with x^d. def pat_or_and_not(self, ins, blk): # Looking for OR instructions... if ins.opcode != m_or: return 0 # ... with compound operands ... if ins.l.t != mop_d or ins.r.t != mop_d: return 0 lhs1, rhs1 = ins.l.d, ins.r.d # ... where each operand is an AND ... if lhs1.opcode != m_and or rhs1.opcode != m_and: return 0 # Extract the numeric and non-numeric operands from both AND terms # ... both AND terms must have one constant ... op_lhs_num, op_lhs_non_num = lhs1.find_num_op() if not op_lhs_num: return 0 op_rhs_num, op_rhs_non_num = rhs1.find_num_op() if not op_rhs_num: return 0 # .. both constants have a size, and are the same size ... if op_lhs_num.size == NOSIZE or op_lhs_num.size != op_rhs_num.size: return 0 # ... and the constants are bitwise inverses of one another ... if (op_lhs_num.nnn.value & op_rhs_num.nnn.value) != 0: return 0 # One of the non-numeric parts must have a binary not (i.e., ~) on it # Check the left-hand size for binary not op_non_notted, op_notted_num, op_notted = None, None, None if op_lhs_non_num.t == mop_d and op_lhs_non_num.d.opcode == m_bnot: # Extract the NOTed term op_notted = op_lhs_non_num.d.l # Make note of the corresponding constant value op_notted_num = op_lhs_num else: op_non_notted = op_lhs_non_num # Check the left-hand size for binary not if op_rhs_non_num.t == mop_d and op_rhs_non_num.d.opcode == m_bnot: # Both sides NOT? Not what we want, return 0 if op_notted is not None: return 0 # Extract the NOTed term op_notted = op_rhs_non_num.d.l # Make note of the corresponding constant value op_notted_num = op_rhs_num else: # Neither side has a NOT? Bail if op_non_notted is not None: return 0 op_non_notted = op_rhs_non_num # The expression that was NOTed must match the non-NOTed operand if not op_notted.equal_mops(op_non_notted, EQ_IGNSIZE): return 0 # Okay, all of our conditions matched. Make an XOR(x,d) instruction ins.opcode = m_xor ins.l.swap(op_non_notted) ins.r.swap(op_notted_num) self.report_success(ins, blk) return 1 # Remove XOR chains with common terms. E.g. x^5^y^6^5^x ==> y^6. # This uses the XorSimplifier class from PatternDeobfuscateUtil. def pat_xor_chain(self, ins, blk): if ins.opcode != m_xor: return 0 # Automagically find duplicated expressions and erase them xs = xor_simplifier_t() if not xs.simplify(ins, blk): return 0 self.report_success(ins, blk) return 1 # Compare two sets of mop_t * element-by-element. Return true if they match. def non_const_sets_match(self, s1, s2): # Iterate over one set for eL in s1: found = False # Iterate over the other set for eR in s2: # Compare the element from the first set against the ones in # the other set. if eL.equal_mops(eR, EQ_IGNSIZE): found = True break # If we can't find some element from the first set in the other, we're done if not found: return False # All elements matched return True # Compare two sets of mop_t * (number values) element-by-element. There # should be one value in the larger set that's not in the smaller set. # Find and return it if that's the case. def find_non_common_constant(self, smaller, bigger): op_no_match = None # Iterate through the larger set for eL in bigger: found = False # Find each element in the smaller set for eR in smaller: if eL.equal_mops(eR, EQ_IGNSIZE): found = True break # We're looking for one constant in the larger set that isn't # present in the smaller set. if not found: # If noMatch was not NULL, then there was more than one # constant in the larger set that wasn't in the smaller one, # so return NULL on failure. if op_no_match is not None: return None op_no_match = eL # Return the constant from the larger set that wasn't in the smaller return op_no_match # Matches patterns of the form: # (a^b^c^d) & (a^b^c^d^e) => (a^b^c^d) & ~e, where e is numeric # The terms don't necessarily have to be in the same order; we extract the # XOR subterms from both sides and find the missing value from the smaller # XOR chain. def pat_and_xor(self, ins, blk): # Instruction must be AND ... if ins.opcode != m_and: return 0 # ... at least one side must be XOR ... left_is_not_xor = ins.l.t != mop_d or ins.l.d.opcode == m_xor right_is_not_xor = ins.r.t != mop_d or ins.r.d.opcode == m_xor if not left_is_not_xor and not right_is_not_xor: return 0 # Collect the constant and non-constant parts of the XOR chains. We # use the XorSimplifier class, but we don't actually simplify the # instruction; we just make use of the existing functionality to # collect the operands that are XORed together. xsL, xsR = xor_simplifier_t(), xor_simplifier_t() xsL.insert_op(ins.l) xsR.insert_op(ins.r) # There must be the same number of non-constant terms on both sides if len(xsL.non_const_ops) != len(xsR.non_const_ops): return 0 lxsLc, lxsRc = len(xsL.const_ops), len(xsR.const_ops) if lxsLc == lxsRc + 1: # Either the left is one bigger than the right... smaller, bigger, left_is_smaller = xsR.const_ops, xsL.const_ops, False elif lxsRc == lxsLc + 1: # Or the right is one bigger than the left... smaller, bigger, left_is_smaller = xsL.const_ops, xsR.const_ops, True else: # Or, the pattern doesn't match, so return 0. return 0 # The sets of non-constant operands must match if not self.non_const_sets_match(xsL.non_const_ops, xsR.non_const_ops): return 0 # Find the one constant value that wasn't common to both sides # If there wasn't one, the pattern failed, so return 0 no_match = self.find_non_common_constant(smaller, bigger) if not no_match: return 0 # Invert the non-common number and truncate it down to its proper size no_match.nnn.update_value(~no_match.nnn.value & ((1 << (no_match.size * 8)) - 1)) # Replace the larger XOR construct with the now-inverted value if left_is_smaller: ins.r.swap(no_match) else: ins.l.swap(no_match) self.report_success(ins, blk) return 1 # Replaces conditionals of the form !(!c1 || !c2) with (c1 && c2). def pat_lnot_or_lnot_lnot(self, ins, blk): # The whole expression must be logically negated. ok, inner, _ = extract_logically_negated_term(ins, op_is_ok=False) if not ok or inner is None: return 0 # The thing that was negated must be an OR with compound operands. if inner.opcode != m_or or inner.l.t != mop_d or inner.r.t != mop_d: return 0 # The two compound operands must also be negated ok, _, op_left = extract_logically_negated_term(inner.l.d) if not ok: return 0 ok, _, op_right = extract_logically_negated_term(inner.r.d) if not ok: return 0 # If we're here, the pattern matched. Make the AND. ins.opcode = m_and ins.l.swap(op_left) ins.r.swap(op_right) self.report_success(ins, blk) return 1 # Replaces terms of the form ~(~x | n), where n is a number, with x & ~n. def pat_bnot_or_bnot_const(self, ins, blk): # We're looking for BNOT instructions (~y)... if ins.opcode != m_bnot or ins.l.t != mop_d: return 0 # ... where x is an OR instruction ... inner = ins.l.d if inner.opcode != m_or: return 0 # ... and one side is constant, where the other one isn't ... op_or_num, op_or_non_num = inner.find_num_op() if not op_or_num: return 0 # ... and the non-constant part is itself a BNOT instruction (~x) if op_or_non_num.t != mop_d or op_or_non_num.d.opcode != m_bnot: return 0 # Once we found it, rewrite the top-level BNOT with an AND ins.opcode = m_and ins.l.swap(op_or_non_num.d.l) # Invert the numeric part not_num = ~(op_or_num.nnn.value) & ((1 << (op_or_num.size * 8)) - 1) ins.r.make_number(not_num, op_or_num.size) self.report_success(ins, blk) return 1 # This function just inspects the instruction and calls the # pattern-replacement functions above to perform deobfuscation. def optimize(self, ins, blk): handlers = { m_bnot : self.pat_bnot_or_bnot_const, m_or : [ self.pat_or_and_not, self.pat_or_via_xor_and, self.pat_or_negated_same_condition, self.pat_logic_and_1, ], m_and : [ self.pat_and_xor, self.pat_mul_sub, ], m_xor : [ self.pat_xor_chain, self.pat_lnot_or_lnot_lnot, self.pat_logic_and_1, ], m_lnot : self.pat_lnot_or_lnot_lnotm_ldx : self.modify_retaddr_read vars
} rc = 0 for mop in [ins.l, ins.r, ins.d]: if mop.is_lvar(): lvar = mop.get_lvar() print(lvar.name) if lvar.name == "retaddr": new_value = ida_hexrays.mop_t() new_value.make_number(ida_kernwin.get_imagebase()+0x1000, lvar.width) # Пример нового значения mop.assign(new_value) hs = handlers.get(ins.opcode, None) if hs is not None: if not isinstance(hs, list): hs = [hs] for h in hs: rc = h(ins, blk) if rc: break return rc # Callback function. Do pattern-deobfuscation. def func(self, blk, ins): ret_val = self.optimize(ins, blk) # This callback doesn't seem to get called for subinstructions of # conditional branches. So, if we're dealing with a conditional branch, # manually optimize the condition expression if (is_mcode_jcond(ins.opcode) or \ is_mcode_set(ins.opcode)) and \ ins.l.t == mop_d: # In order to optimize the jcc condition, we actually need a different # structure than optinsn_t: in particular, we need a minsn_visitor_t. # This local structure declaration just passes the calls to # minsn_visitor_t::visit_minsn onto the Optimize function in this # optinsn_t object. class blah_t(minsn_visitor_t): def __init__(self, oco): minsn_visitor_t.__init__(self) self.oco = oco def visit_minsn(self): try: return self.oco.optimize(self.curins, self.blk) except: import traceback traceback.print_exc() raise # Optimize all subinstructions of the JCC conditional ret_val += ins.for_all_insns(blah_t(self)) # If any optimizations were performed... if ret_val: # ... inform the user ... hexrays_util.report_debug("ObfCompilerOptimizer: replaced by %s" % hexrays_util.mcode_t_to_string(ins)) _optimize(blk, ins) # I got an INTERR if I optimized jcc conditionals without marking the lists dirty. blk.mark_lists_dirty() blk.mba.verify(True) return ret_valошибка for mop in [ins.l, ins.r, ins.d]: TypeError: 'minsn_t' object is not iterable
- Assistant
Ошибка
TypeError: 'minsn_t' object is not iterableвозникает, потому чтоminsn_t— это объект, представляющий одну микрокоманду в микрокоде Hex-Rays, а поляl,r, иdне предоставляют способ итерировать поminsn_t. Вместо этого они являются экземплярамиmop_t, и вы не должны итерировать по ним напрямую.Вы можете получить доступ к этим полям индивидуально. Вот пример подхода для работы с полями
l,r, иdобъектаminsn_t:import ida_hexrays import ida_kernwin class obf_compiler_optimizer_t(ida_hexrays.optinsn_t): def optimize(self, ins, blk): # Проверка и обработка операндов l, r и d индивидуально for mop in [ins.l, ins.r, ins.d]: if mop.is_lvar(): lvar = mop.get_lvar() print(lvar.name) if lvar.name == "retaddr": new_value = ida_hexrays.mop_t() new_value.make_number(ida_kernwin.get_imagebase() + 0x1000, lvar.width) mop.assign(new_value) # Обработка других специфических паттернов микрокода handlers = { # Пример для других операций, добавьте здесь больше обработчиков ida_hexrays.m_xor: self.pat_xor_chain } # Проверка, если обработчик существует для текущего кода операции hs = handlers.get(ins.opcode, None) if hs is not None: if not isinstance(hs, list): hs = [hs] for h in hs: if h(ins, blk): return True return False # Пример паттерн-обработчика def pat_xor_chain(self, ins, blk): if ins.opcode == ida_hexrays.m_xor: # пример: автоснятие дубликатов выражений и их удаление print("Обнаружен xor, можно применять оптимизацию.") return True return False def func(self, blk, ins): if self.optimize(ins, blk): # Информировать пользователя о замене ida_kernwin.msg(f"Обфускатор: Замена в {ida_hexrays.mcode_t_to_string(ins)}\n") blk.mark_lists_dirty() return 0Основные изменения:
- Индивидуальная работа с
mop_t: Вы не должны итерироватьminsn_t, но можете получить доступ к егоmop_tкомпонентам (l,r,d) непосредственно. - Добавление обработчиков: Хранить специфическую логику в отдельных методах (например
pat_xor_chain) и вызывать их по мере необходимости.
Эти изменения помогут избежать ошибки и обеспечить корректную обработку микрокода. Обязательно убедитесь в том, что вы корректно обрабатываете все переменные и используете методы из API Hex-Rays должным образом.
- Индивидуальная работа с
- User
я делаю
for mop in [ins.l, ins.r, ins.d]: print(mop.dstr()) - Assistant
Кажется, у вас возникло недопонимание API Hex-Rays.
minsn_tпредставляет собой одну инструкцию на уровне микрокода, и она может содержать до трех операндов (l,r, иd), которые представлены в виде объектовmop_t.Метод
dstr()используется для получения строкового представления объектаminsn_tилиmop_t.Если вы хотите вывести представление каждого операнда (если оно существует), сделайте это отдельно для каждого из
l,rиd, а не через итерацию. Вот как это можно сделать:import ida_hexrays import ida_kernwin class obf_compiler_optimizer_t(ida_hexrays.optinsn_t): def optimize(self, ins, blk): # Печать строкового представления каждого операнда if ins.l: print("Left operand:", ins.l.dstr()) if ins.r: print("Right operand:", ins.r.dstr()) if ins.d: print("Destination operand:", ins.d.dstr()) # Ваше дальнейшее взаимодействие и обработка... def func(self, blk, ins): if self.optimize(ins, blk): ida_kernwin.msg(f"Обфускатор: Замена в {ida_hexrays.mcode_t_to_string(ins)}\n") blk.mark_lists_dirty() return 0Объяснение:
- Проверка и вывод: Для каждого операнда (
l,r,d) проверяется его наличие и выводится его строковое представление с помощьюdstr().
Это решение позволяет безопасно обращаться к операндам и выводить их строковое представление, если они существуют. Убедитесь, что все попытки обращения к элементам инструкции проверяют их наличие, чтобы избежать ошибок.
- Проверка и вывод: Для каждого операнда (
- User
сделай так что бы эта функция выводила микрокод в отдельное окно а не через print
for mop in [ins.l, ins.r, ins.d]: print(mop.dstr()) - Assistant
Чтобы вывести микрокод в отдельное окно в IDA, вы должны использовать API для создания пользовательского интерфейса. В этом случае можно использовать
simplecustviewer_tдля отображения строк в новом окне.Вот как можно изменить ваш код, чтобы выводить микрокодовые инструкции в отдельное окно:
import ida_hexrays import ida_kernwin class MicrocodeViewer(ida_kernwin.simplecustviewer_t): def Create(self, title): if not ida_kernwin.simplecustviewer_t.Create(self, title): return False self.Show() return True def AddLine(self, line): self.AddLine(line) return True class obf_compiler_optimizer_t(ida_hexrays.optinsn_t): def __init__(self): # Инициализируем окно для отображения микрокода self.viewer = MicrocodeViewer() self.viewer.Create("Microcode Instructions") def display_microcode(self, ins): # Выводим строковое представление каждого операнда в окно if ins.l: self.viewer.AddLine("Left operand: " + ins.l.dstr()) if ins.r: self.viewer.AddLine("Right operand: " + ins.r.dstr()) if ins.d: self.viewer.AddLine("Destination operand: " + ins.d.dstr()) def optimize(self, ins, blk): # Вызов отображения микрокода self.display_microcode(ins) # Ваше дальнейшее взаимодействие и обработка... def func(self, blk, ins): if self.optimize(ins, blk): ida_kernwin.msg(f"Обфускатор: Замена в {ida_hexrays.mcode_t_to_string(ins)}\n") blk.mark_lists_dirty() return 0 # Создаем экземпляр вашего оптимизатора optimizer = obf_compiler_optimizer_t() # Пример вызова вашей функции в контексте, где blk и ins определены # optimizer.func(blk, ins) # В этом примере blk и ins должны быть предоставленыОбъяснение:
-
Создание класса
MicrocodeViewer: Подклассsimplecustviewer_t, который используется для создания окна пользовательского интерфейса в IDA. -
Метод
display_microcode: ВызываетAddLineдля добавления строк в окно, что позволяет визуализировать микрокодовые операции. -
Инициализация окна при создании объекта
obf_compiler_optimizer_t.
Этот пример создаёт и показывает новое окно в IDA, в котором выводятся микрокодовые инструкции. Помните, что этот код должен быть выполнен в среде IDA, и доступ к объектам
blkиinsдолжен быть в контексте действия функций IDA Pro. -