1 730
دوستان عزیز من لطفا همه نظر بدید به یک نتیجه ای برسیم ممنون 😁🫂
My dear friends please everyone share your opinions so we can reach a conclusion tnx 😁🫂
09 11月, 14:24
11 11月, ~16:32
.?AVDog@@ .?AVAnimal@@ .?AVPlayer@@These names may be seen in RTTI sections By identifying and demangle them, we can recover the names of some classes In Ghidra, we can also use the Demangling feature Of course, not all the named information is necessarily RTTI The remaining symbols may be Debug information or even regular strings So we should also check the location of the name and the references associated with it RTTI What is the difference between GCC and Clang The RTTI structure in MSVC is not the same as the common ABI of GCC and Clang on Linux On systems that use the Itaniu C++ ABI, there are structures like this
__class_type_info __si_class_type_info __vmi_class_type_info Each one contains different information about the Type and Inheritance is maintainedFor example:
__class_type_info Used to maintain basic class type information
__si_class_type_info Used for some Single Inheritance cases
__vmi_class_type_info Used for more complex structures such as Multiple InheritanceSo if we find these names in a Linux binary, they can be good clues to reconstruct the Class Hierarchy Of course, the existence of these structures depends on whether RTTI is enabled and how the Binary is generated by the Compiler How do we check in Ghidra? A good way to start the analysis is as follows: First, identify possible Vtables Check the references around the Vtable Look for RTTI-related pointers If the binary is MSVC, check the CompleteObjectLocator and TypeDescriptor structures If the binary uses the Itanium ABI, look for __class_type_info structures and similar ones Demangle mangled names Extract base class information Compare the result with constructors and virtual calls The important thing is that RTTI may be disabled or its information may not be left in the binary In this case, the absence of RTTI does not mean the absence of inheritance The program may still use classes with inheritance but the RTTI information may not be present in the executable file Limitations of RTTI RTTI is a powerful tool, but it does not always provide us with complete information Some important points It is possible that the compiler with special RTTI options has disabled The Binary may have lost some of its information due to Stripping or Optimization The class names may have remained in a mangled form The RTTI structure differs between different ABIs The existence of a TypeDescriptor alone does not mean that there is a live Object of that type So we should examine RTTI along with Disassembly, Vtable Analysis, and Constructor Analysis In Reverse Engineering C++ Programs, RTTI can provide us with valuable information about Types and the relationship between classes The general analysis path is as follows:
VTable | v RTTI Reference | v TypeDescriptor | v Class Name | v Class Hierarchy | v Object Layout ReconstructionSo far we have learned how to get to the internal structure of C++ classes from Memory Layout, Vtable, Inheritance, and RTTI @reverseengine
class Animal {
public:
virtual ~Animal() {}
};
class Dog : public Animal {
public:
void bark() {}
};
Animal* ptr = new Dog;
Here the Pointer type is Animal* but the real Object is made of type Dog
Using RTTI, we can check the real type of Object under appropriate conditions
For example:
if (dynamic_cast<Dog*>(ptr)) {
// Object is compatible with Dog
}
If the conversion is successful, it means that the Object is of type Dog or one of its derived classes
The important point is that dynamic_cast requires Polymorphic classes to check the type at runtime, that is, the class must have at least one virtual function
Also, RTTI activation depends on the Compiler settings
Why is RTTI important in Reverse Engineering
Suppose we have an unknown Binary
In the normal case, we may only encounter a few addresses related to the Vtable
vtable_140005000 vtable_140005100 vtable_140005200The real names of the classes are unknown But if the RTTI information remains inside the Binary, we may end up with things like this `` Animal Dog Cat Bird
There may even be information We also find out about the inheritance relationship between classes As a result, instead of just working with a few anonymous addresses, we can reconstruct part of the program's Class Hierarchy This information, along with analyzing Constructors and Virtual Calls, helps us better understand the internal structure of the program How is RTTI stored in MSVC In the MSVC family of compilers, the RTTI structure contains several different types of information One of the important structures TypeDescriptor This structure stores information about the Type and usually includes the encoded class attribute For example, the name of a class may be stored as follows.?AVDog@@
This is a mangled name in the MSVC naming format By demangled, we can get a more understandable nameDog
The next important structure CompleteObjectLocator This structure provides information about the complete Object type and the location of the Subobject related to the Vtable to the Runtime There is also a structure called ClassHierarchyDescriptor which stores information about the class hierarchy In The end of the structure BaseClassDescriptor describes information about the base classes So in MSVC we usually have a chain like thisVTable Address Point | v CompleteObjectLocator | +----> TypeDescriptor | +----> ClassHierarchyDescriptor | v BaseClassDescriptor
This is a conceptual representation of the relationship between the structures and the exact details of the Layout depend on the ABI version and architecture How to find RTTI from a Vtable? Suppose we find a Vtable in a Windows Binary In the common MSVC implementation, a Pointer to CompleteObjectLocator is placed just before the Address Point of the Vtable Conceptually:Address +-----------------------------+ | CompleteObjectLocator ptr | +-----------------------------+ | Function 0 | +-----------------------------+ | Function 1 | +-----------------------------+ | Function 2 | +-----------------------------+
If we find the Pointer related to CompleteObjectLocator, we can follow the RTTI information chain In 64-bit versions of MSVC, references inside RTTI structures are usually stored as RVA, not as complete pointers RVA stands for Relative Virtual Address To convert RVA to virtual address, we use the following relationshipVA = ImageBase + RVA
For example, ifImageBase = 0x180000000 RVA = 0x12340
the virtual address is equal toVA = 0x180012340
`
Of course, it should be clear that the value we are checking is really an RVA and not a regular Pointer.?AVDog@@ .?AVAnimal@@ .?AVPlayer@@این اسم ها ممکنه در بخشهای مربوط به RTTI دیده بشن با شناسایی و Demangle کردن اونها میتونیم نام بعضی کلاسها رو بازیابی کنیم در Ghidra هم میتونیم از قابلیت Demangling استفاده کنیم البته همه اطلاعات اسم گزاری شده حتما RTTI نیستن ممکنه Symbol های باقی مونده یا اطلاعات Debug یا حتی رشتههای معمولی هم باشن پس باید محل قرارگیری نام و Reference های مرتبط با اون رو هم بررسی کنیم RTTI در GCC و Clang چه تفاوتی داره ساختار RTTI در MSVC با ABI رایج GCC و Clang روی Linux یکی نیست در سیستمهایی که از
Itaniu C++ ABI استفاده میکنن ساختارهایی مثل این وجود دارن
__class_type_info __si_class_type_info __vmi_class_type_infoهرکدوم اطلاعات متفاوتی درباره Type و Inheritance نگه میدارن برای مثال:
__class_type_info
برای نگهداری اطلاعات پایه نوع کلاس استفاده میشه
__si_class_type_info
برای بعضی حالتهای Single
Inheritance کاربرد داره
__vmi_class_type_info
برای ساختارهای پیچیدهتر از جمله Multiple Inheritance استفاده میشه
پس اگر در یک Binary لینوکسی این اسم ها رو پیدا کنیم میتونن سرنخهای خوبی برای بازسازی Class Hierarchy باشن
البته وجود این ساختارها به فعال بودن RTTI و نحوه تولید Binary توسط Compiler بستگی داره
در Ghidra چطور بررسی کنیم؟
یک روند مناسب برای شروع تحلیل به این شکله
اول Vtable های احتمالی رو شناسایی کنید
Reference
های اطراف Vtable رو بررسی کنید
دنبال Pointer های مرتبط با RTTI بگردید
اگر Binary مربوط به MSVC هست ساختارهای CompleteObjectLocator و TypeDescriptor رو بررسی کنید
اگر Binary از Itanium ABI استفاده میکنه دنبال ساختارهای __class_type_info و موارد مشابه بگردید
اسم های Mangled شده رو Demangle کنید
اطلاعات کلاس های پایه رو استخراج کنید
نتیجه رو با Constructorها و Virtual Call ها مقایسه کنید
نکته مهم اینه که ممکنه RTTI غیرفعال شده باشه یا اطلاعاتش در Binary باقی نمونده باشه
در این حالت نبودن RTTI به معنی نبودن Inheritance نیست
ممکنه برنامه همچنان از کلاسهای دارای Inheritance استفاده کنه اما اطلاعات RTTI در فایل اجرایی وجود نداشته باشه
محدودیتهای RTTI
RTTI
ابزار قدرتمندیه اما همیشه اطلاعات کامل در اختیارمون نمیذاره
چند مورد مهم
ممکنه Compiler با گزینههای خاص RTTI رو غیرفعال کرده باشه
ممکنه Binary با Stripping یا Optimization بخشی از اطلاعاتش رو از دست داده باشه
ممکنه اسم کلاس ها به شکل Mangled باقی مونده باشن
ساختار RTTI بین ABIهای مختلف تفاوت داره
وجود یک TypeDescriptor بهتنهایی به معنی وجود یک Object زنده از اون نوع نیست
پس RTTI رو باید در کنار Disassembly و Vtable Analysis و Constructor Analysis بررسی کنیم
در Reverse Engineering برنامههای C++ RTTI میتونه اطلاعات ارزشمندی درباره Typeها و رابطه بین کلاسها در اختیارمون بذاره
مسیر کلی تحلیل به این شکله:
VTable | v RTTI Reference | v TypeDescriptor | v Class Name | v Class Hierarchy | v Object Layout Reconstructionتا اینجا یاد گرفتیم چطور از روی Memory Layout و Vtable و Inheritance و RTTI به ساختار داخلی کلاسهای ++C برسیم
RTTI
مخفف Run-Time Type Information هست
C++ داخل
قابلیتیه که به برنامه اجازه میده در زمان اجرا اطلاعاتی درباره نوع واقعی بعضی Object ها به دست بیاره
کد زیر رو در نظر بگیرید:
class Animal {
public:
virtual ~Animal() {}
};
class Dog : public Animal {
public:
void bark() {}
};
Animal* ptr = new Dog;
اینجا نوع Pointer برابر Animal* هست اما Object واقعی از نوع Dog ساخته شده
با استفاده از RTTI میتونیم در شرایط مناسب نوع واقعی Object رو بررسی کنیم
مثلا:
if (dynamic_cast<Dog*>(ptr)) {
// Object is compatible with Dog
}
اگر تبدیل موفق باشه یعنی Object موردنظر از نوع Dog یا یکی از کلاس های مشتقشده از اون هست
نکته مهم اینه که dynamic_cast برای بررسی نوع در زمان اجرا به کلاس های Polymorphic نیاز داره یعنی کلاس باید حداقل یک تابع مجازی داشته باشه
همچنین فعال بودن RTTI به تنظیمات Compiler بستگی داره
چرا RTTI در Reverse Engineering مهمه
فرض کنید یک Binary ناشناخته داریم
در حالت عادی ممکنه فقط با چند آدرس مربوط به Vtable مواجه بشیم
vtable_140005000 vtable_140005100 vtable_140005200اسم واقعی کلاس ها مشخص نیست اما اگر اطلاعات RTTI داخل Binary باقی مونده باشه ممکنه به مواردی مثل این برسیم
Animal Dog Cat Birdحتی ممکنه اطلاعاتی درباره رابطه ارثبری بین کلاسها هم پیدا کنیم در نتیجه به جای اینکه فقط با چند آدرس ناشناس کار کنیم میتونیم بخشی از Class Hierarchy برنامه رو بازسازی کنیم این اطلاعات در کنار تحلیل Constructor ها و Virtual Call ها کمک میکنن ساختار داخلی برنامه رو بهتر بشناسیم RTTI در MSVC چجوری ذخیره میشه در Compiler خانواده MSVC ساختار RTTI شامل چند نوع اطلاعات مختلفه یکی از ساختارهای مهم
TypeDescriptor
این ساختار اطلاعات مربوط به Type رو نگه میداره و معمولا شامل اطم رمزگذاری شده کلاس هم هست
برای مثال ممکنه نام یک کلاس به شکل زیر ذخیره شده باشه
.?AVDog@@این یک نام Mangled شده در قالب نامگذاری MSVC هست با Demangle کردن اون میتونیم به نام قابلفهم تری برسیم
Dogساختار مهم بعدی
CompleteObjectLocator
این ساختار اطلاعاتی درباره نوع کامل Object و محل قرارگیری Subobject مربوط به Vtable رو در اختیار Runtime قرار میده
همچنین ساختاری با اسم
ClassHierarchyDescriptor
وجود داره که اطلاعات مربوط به سلسلهمراتب کلاسها رو نگه میداره
در اخر ساختار
BaseClassDescriptor
اطلاعات مربوط به کلاسهای پایه رو توصیف میکنه
پس در MSVC معمولا با زنجیرهای شبیه این روبهرو هستیم
VTable Address Point
|
v
CompleteObjectLocator
|
+----> TypeDescriptor
|
+----> ClassHierarchyDescriptor
|
v
BaseClassDescriptor
این یک نمایش مفهومی از ارتباط ساختار هاست و جزئیات دقیق Layout به نسخه ABI و معماری بستگی داره
چطور RTTI را از روی Vtable پیدا کنیم؟
فرض کنید در یک Binary ویندوزی یک Vtable پیدا کردیم
در پیادهسازی رایج MSVC یک Pointer به CompleteObjectLocator درست قبل از Address Point مربوط به Vtable قرار میگیره
بهصورت مفهومی:
Address +-----------------------------+ | CompleteObjectLocator ptr | +-----------------------------+ | Function 0 | +-----------------------------+ | Function 1 | +-----------------------------+ | Function 2 | +-----------------------------+اگر Pointer مربوط به CompleteObjectLocator رو پیدا کنیم میتونیم زنجیره اطلاعات RTTI رو دنبال کنیم در نسخههای 64 بیتی MSVC معمولا Reference های داخل ساختارهای RTTI به شکل RVA ذخیره میشن نه Pointer کامل
RVA
مخفف Relative Virtual Address هست
برای تبدیل RVA به آدرس مجازی از رابطه زیر استفاده میکنیم
VA = ImageBase + RVAمثلا اگر
ImageBase = 0x180000000 RVA = 0x12340آدرس مجازی برابر میشه با
VA = 0x180012340البته باید مشخص باشه مقداری که بررسی میکنیم واقعا RVA هست و نه یک Pointer معمولی همچنین در MSVC نسخه 64 بیتی بعضی Referenceهای RTTI بهصورت RVA نسبی به محل فیلد ذخیره میشن و برای تحلیل اونها باید روش محاسبه آدرس رو متناسب با ساختار مربوطه در نظر بگیریم Name Mangling چه کمکی میکنه
Activity API | v Memory Events | v Thread Events | v Network Events | v Behavior CorrelationIn this model, EDR does not simply look for a specific API But it tries to detect the relationship between events over time For example, a CreateRemoteThread call alone is not necessarily a sign of an attack However, if it is accompanied by unusual access to another process, memory changes, and evidence of code execution, it is worth further investigation The important point is that EDRs do not necessarily record all API calls directly and may use a combination of ETW, system callbacks, and other telemetry sources to collect information Suitable tools for API Tracing To practice and examine the behavior of programs in Windows, we can use the following tools Process Monitor To observe the activities of the File System, Registry, and Processes This tool does not record all API calls of the program and focuses more on system events visible in its scope Official Process Monitor page API Monitor
To observe the calls to the program's APIs and examine their parameters and results API Monitor websiteWinDbg
For Debugging and Call Stack Inspection and Program Execution Analysis Official WinDbg DocumentationWindows Performance Recorder and WPA
For recording and analyzing system events and examining the program execution timeline Official Windows Performance Toolkit DocumentationThe important point is that these tools do not provide exactly the same type of Telemetry, and each is suitable for observing and analyzing a specific part of the system behavior Exercise: Run a simple and safe program in a laboratory environment For example: a program that Reads a text file Writes a few bytes into it Closes the file Reopens the file Reads its information Now check the program activity with Process Monitor Pay attention to these items:
Process Name Operation Path Result TimestampThen note the order of events Try to understand how each operation is related to the previous and next operations The goal is to understand what the program really did from the recorded activities, not just to remember the names of a few APIs API Call Tracing
To check Calls are at runtime The sequence of calls can give more information about the behavior of the applicationCall Stack
Helps identify the call pathEDR
Can investigate suspicious behavior by combining several types of TelemetryAn API or Sequence alone is not proof of malicious behavior @reverseengine

我们使用 cookie 来改善您的浏览体验。
点击“全部接受”,即表示您同意Cookie 使用政策。