ar
Feedback

لا تقع ضحية للمخادعين! تيليمتريو يكتشف ويُميّز هذه القنوات 👉 إذا كنت تريد رؤية العلامة، اشترك

ReverseEngineering

ReverseEngineering

الذهاب إلى القناة على Telegram
1 730
المشتركون
-124 ساعات
-47 أيام
+28230 أيام
أرشيف المشاركات
Struct Reconstruction وقتی یه فایل باینری رو مهندسی معکوس میکنی خیلی وقت‌ ها سورس کد اصلی رو ندارید یعنی نمیدونید برنامه‌ نویس چه ساختار هایی تعریف کرده و هر کدوم چه فیلدهایی داشتن اینجا باید از روی نحوه دسترسی برنامه به حافظه ساختار داده‌ ها رو دوباره بازسازی کنید به این کار میگیم Struct Reconstruction فرض کنید برنامه اصلی همچین ساختاری داشته باشه C struct User { int id; int age; char active; }; این ساختار سه تا فیلد داره یکی برای شناسه کاربر یکی برای سن و یکی هم برای وضعیت فعال بودن. ولی وقتی فایل کامپایل‌ شده رو بررسی میکنی ممکنه اصلا اسم User یا فیلد های id و age رو نبینید فقط دستور های اسمبلی رو دارید که با آدرس‌ های حافظه کار میکنن حالا ببین چطور میتونیم ساختار رو از روی همین دستورها تشخیص بدیم پیدا کردن آدرس فیلد ها فرض کنید توی اسمبلی به دستورهای زیر رسیدید Asm mov eax, dword ptr [rcx] mov edx, dword ptr [rcx+4] movzx eax, byte ptr [rcx+8] بیایم یکی یکی بررسیشون کنیم Asm mov eax, dword ptr [rcx] چهار بایت از آدرسی که داخل RCX قرار داره میخونه Asm mov edx, dword ptr [rcx+4] چهار بایت از چهار بایت بعدی میخونه Asm movzx eax, byte ptr [rcx+8] یک بایت از هشت بایت بعد از آدرس اولیه میخونه حالا اطلاعاتی که داریم این شکلیه `text آدرس پایه + 0 چهار بایت آدرس پایه + 4 چهار بایت آدرس پایه + 8 یک بایت این الگو میتونه نشون بده که برنامه داره به سه فیلد مختلف از یه ساختار دسترسی پیدا میکنه تشخیص اندازه و نوع فیلد ها از روی اندازه دسترسی‌ ها میتونیم حدس بزنیم هر فیلد چه نوعی داره دسترسی چهار بایتی ممکنه مربوط به int یا float باشه دسترسی یک بایتی ممکنه مربوط به char یا یک مقدار بولی باشه اما حواستون باشه که اندازه به‌ تنهایی نوع دقیق داده رو مشخص نمیکنه مثلا یه مقدار چهار بایتی میتونه بخشی از یه عدد بزرگ‌تر هم باشه برای تشخیص بهتر باید ببینید مقدار کجا استفاده میشه اگه مقدار با دستورهای محاسباتی عددی استفاده بشه ممکنه یه عدد معمولی باشه اگه با دستور های مربوط به عملیات ممیز شناور کار کنه احتمال float بودنش بیشتره بازسازی ساختار با توجه به دسترسی‌ هایی که دیدیم میتونیم یه ساختار احتمالی بسازیم C struct Unknown { int field_0; int field_4; char field_8; }; اسم فیلد ها رو فعلا بر اساس آفست حافظه گذاشتیم چون هنوز کاربرد واقعی اون‌ها رو نمیدونیم بعد از بررسی بخش‌ های بیشتری از برنامه ممکنه بفهمیم field_0 شناسه کاربره field_4 سن کاربره و field_8 وضعیت فعال بودنشه سم‌ گذاری بر اساس معنی داده‌ ها انجام میشه نه صرفا ظاهر اسمبلی بررسی نوشتن روی فیلدها فقط دستور های خوندن رو بررسی نکنید نوشتن روی حافظه هم اطلاعات مهمی بهمون میده مثلا: Asm mov dword ptr [rcx], 123 mov dword ptr [rcx+4], 25 mov byte ptr [rcx+8], 1 این دستورها به‌ ترتیب روی آفست‌ های صفر چهار و هشت مینویسن اگه جاهای مختلف برنامه ببینید که همین آفست‌ ها برای خوندن و نوشتن استفاده میشن احتمال اینکه با فیلد های یه ساختار طرف باشی بیشتر میشه البته هنوز باید بررسی کنید که RCX واقعا آدرس پایه یک ساختار مشترکه یا فقط در اون لحظه به یه بخش از حافظه اشاره میکنه پیدا کردن اندازه واقعی ساختار اینجا یه نکته مهم وجود داره بزرگ‌ ترین آفستی که پیدا میکنید لزوما اندازه کامل ساختار نیست مثلا اگه آخرین فیلد از آفست هشت شروع بشه و یک بایت اندازه داشته باشه حداقل فضای مورد نیاز برای این فیلدها 9 بایته اما اندازه نهایی ساختار ممکنه به خاطر قوانین هم‌ترازی حافظه یا همون Alignment بزرگ‌تر باشه برای مثال کامپایلر ممکنه ساختار رو به 12 بایت گرد کنه تا همترازی مورد نیاز رعایت بشه همچنین ممکنه ساختار فیلد های دیگه‌ای داشته باشه که هنوز در مسیرهای بررسی‌ شده بهشون دسترسی پیدا نکردیم پس اندازه ساختار رو باید با بررسی تخصیص حافظه نحوه استفاده از اشاره‌گرها و تمام آفست‌ های قابل مشاهده تخمین بزنید این کار توی Ghidra چطور انجام میشه؟ وقتی ساختار احتمالی رو پیدا کردید میتونید از بخش Data Type Manager یه ساختار جدید بسازی و فیلدهاش رو تعریف کنید بعد در صورت مشخص بودن نوع متغیر و آدرس پایه میتونید نوع ساختار رو به متغیر مربوط اختصاص بدید تا به‌ جای آفست‌های خام دسترسی‌ها خواناتر بشن مثلا به‌جای اینکه فقط ببینید برنامه از [rcx+4] میخونه بتونید تشخیص بدید که داره فیلد دوم ساختار رو میخونه
15 أكتوبر, 11:05

Simple view: Program.exe | v Import Table | v Required DLLs | v Resolve Functions | v Import Address Table (IAT) The IAT table contains the addresses used to access imported functions That is why the contents of some PE parts in Memory can differ from the corresponding initial values ​​in the file on disk So why might the PE on disk and the memory be different? There are several important reasons: Relocation: Some addresses are modified depending on the loading location Import Resolution: The address of imported functions is determined during the loading process Memory Layout: The structure of sections in memory is not necessarily the same as the raw layout of the file Runtime Changes: The program itself may change some memory data during execution Zero Initialization: Some sections may have allocated space in memory but have no corresponding data in the file and are initialized to zero So if an analyst wants to compare a file on disk with Memory, he should not expect all the bytes to be exactly the same How is this useful in Memory Forensics? Suppose you are examining a Memory Dump and you find a section whose content looks like the structure of a PE A few questions to ask yourself: Is the header valid? Are the sections recognizable? Are the addresses consistent with the Image structure? Memory Related to a known module? Can the differences between file and memory be explained? It is important to note that finding an MZ signature alone does not prove that a valid or executable PE exists in that area. A complete set of evidence must be examined for proper analysis. A PE may also not be easily recoverable due to the way it was uploaded, runtime changes, or even incomplete memory dump data. How this relates to EDR. EDR can use information about memory modules and process behavior to investigate suspicious activity. For example, we might put this information together: Process Information + Loaded Modules + Memory Regions + Thread Activity + Call Stack + Execution Timeline If the memory information is inconsistent with the expected state of a program, further investigation may be warranted. However, it is important to note that: A difference in memory from a file on disk alone is not a definitive indication of malicious manipulation. Some differences are a natural consequence of program upload and execution. So far, we have been familiar with some important concepts. We are: These concepts are very, very important to us in the discussion of memory analysis and program behavior analysis @reverseengine
09 أكتوبر, 17:23

The Loader processes these dependencies and places the addresses of the required functions in the associated structures
09 أكتوبر, 17:21

PE In memory and its difference from a file on disk So far we have talked about Private Memory Image-backed Memory and VAD Now we want to see what happens to a Windows executable file when it enters memory and why the version in memory is usually not the same as the version on disk First, let's see what PE is The main executable format of many Windows programs is PE or Portable Executable Consider files like these: program.exe library.dll The general structure of a PE file is approximately as follows: PE File │ ├── DOS Header ├── PE Signature ├── COFF File Header ├── Optional Header ├── Section Headers │ ├── .text ├── .rdata ├── .data ├── .pdata └── Other Sections Not all files necessarily have all of these sections, and their order and details may also vary Each section has specific information, and Windows uses the PE structure to recognize and load the executable file. Uses What happens when PE enters Memory? Suppose this file is on disk: C:\Program Files\App\program.exe When Windows runs it, the operating system Loader performs the upload process Simple view: PE File on Disk | v Windows Loader | v Virtual Address Space | v Program Execution But the point is that Windows usually does not copy the file into memory as a raw, unmodified piece The Loader must process the file structures and place the different parts in the appropriate places The difference between Raw Offset and RVA Here we come to one of the basic concepts Raw Offset Is the address used to find the information inside the file itself on the disk For example: File Offset = 0x600 That is, the desired information starts from Offset equal to 0x600 in the file RVA is the abbreviation for this phrase: RVA = Relative Virtual Address That is, the address of a section relative to ImageBase in the virtual address space For example: ImageBase = 0x140000000 RVA = 0x2000 In this example: VA = ImageBase + RVA So: VA = 0x140002000 This calculation calculates the desired virtual address It is obtained provided that we use the actual ImageBase of the loaded Image The main difference Raw Offset | +--> Location in the file on disk RVA | +--> Location relative to ImageBase in the virtual address space Raw Offset and RVA are not necessarily equal To convert between them, we must also consider the relevant Section information such as PointerToRawData and VirtualAddress In addition, not all virtual addresses necessarily correspond to a section of the data in the file, for example, a section in memory may be larger than its corresponding raw data ImageBase Each Image in the uploaded state has a Base Address, which we call ImageBase For example: Preferred ImageBase: 0x140000000 This address is the preferred location defined for loading the Image in the PE file But Windows does not always have to place the Image exactly there Why? Because that range of virtual address space may not be available or there may be other conditions In this situation, the Image may be loaded to another address Here we go to the next concept Base Relocation Suppose the program expects the Image to be located at this address: Preferred Base = 0x140000000 But in practice it is uploaded to another address: Actual Base = 0x180000000 The difference between these two addresses: Delta = Actual Base - Preferred Base If the Image needs Relocation and the necessary information is available, the Loader uses the Relocation information to correct the addresses dependent on the loading location We call this process Base Relocation The important point is that not all addresses in the file require Relocation, only those that are dependent on the loading location according to the PE structure and have appropriate Relocation information must be corrected What does Import Table do? Programs often use functions from other libraries For example, a program might need functions from these modules: KERNEL32.dll USER32.dll ADVAPI32.dll Information about external dependencies is placed in the Import structures of the PE file
09 أكتوبر, 17:21

نمای ساده : Program.exe | v Import Table | v Required DLLs | v Resolve Functions | v Import Address Table (IAT) جدول IAT شامل آدرس‌ هایی میشه که برای دسترسی به توابع Import شده استفاده میشن به همین دلیل محتوای بعضی قسمت‌ های PE در Memory میتونه با مقادیر اولیه‌ ی متناظر در فایل روی دیسک متفاوت باشه پس چرا PE روی دیسک و Memory ممکنه فرق داشته باشن؟ چند دلیل مهم وجود داره: Relocation: بعضی آدرس‌های وابسته به محل بارگذاری اصلاح میشن Import Resolution: آدرس توابع Importشده در فرایند بارگذاری مشخص میشه Memory Layout: ساختار Section ها در حافظه لزوما مثل چیدمان خام فایل نیست Runtime Changes: خود برنامه ممکنه هنگام اجرا بعضی داده‌ های حافظه رو تغییر بده Zero Initialization: بعضی قسمتها ممکنه در حافظه فضای اختصاص‌ یافته داشته باشن ولی داده‌ی متناظری در فایل نداشته باشن و با صفر مقداردهی بشن پس اگر یک تحلیل‌گر بخواد فایل روی دیسک رو با Memory مقایسه کنه نباید انتظار داشته باشه تمام Byte ها دقیقا یکی باشن این موضوع در Memory Forensics چه کاربردی داره؟ فرض کنی در حال بررسی یک Memory Dump هستید یک بخش پیدا میکنید که محتوای اون شبیه ساختار یک PE به نظر میرسه چند سوال از خودتون میپرسید: Header معتبره؟ Section ها قابل شناسایی هستن؟ آدرس‌های موجود با ساختار Image سازگارن؟ Memory به یک Module شناخت ه‌شده مربوطه؟ تفاوت‌ های فایل و Memory قابل توضیح هستن؟ نکته‌ی مهم اینه که پیدا کردن امضای MZ به‌تنهایی اثبات نمیکنه که یک PE معتبر یا قابل اجرا در اون ناحیه وجود داره برای تحلیل درست باید مجموعه‌ ای از شواهد رو بررسی کرد همچنین یک PE ممکنه به دلیل نحوه‌ ی اپلود تغییرات زمان اجرا یا حتی ناقص بودن داده‌های Memory Dump به‌سادگی قابل بازیابی نباشه ارتباط این با EDR EDR میتونه از اطلاعات مربوط به Module ها حافظه و رفتار Process برای بررسی فعالیت‌ های مشکوک استفاده کنه برای مثال ممکنه این اطلاعات رو کنار هم قرار بدیم: Process Information + Loaded Modules + Memory Regions + Thread Activity + Call Stack + Execution Timeline اگر اطلاعات Memory با وضعیت مورد انتظار یک برنامه سازگار نباشه ممکنه نیاز به بررسی بیشتری وجود داشته باشه ولی باید دقت کنیم که: تفاوت Memory با فایل روی دیسک به‌تنهایی نشونه‌ی قطعی دست‌کاری مخرب نیست بعضی تفاوت‌ ها نتیجه‌ی طبیعی اپلود و اجرای برنامه هستن تا اینجا با چند مفهوم مهم آشنا شدیم: این مفاهیم برای ما داخل بحث تحلیل حافظه و بررسی رفتار برنامه‌ ها خیلی خیلی مهمن @reverseengine
09 أكتوبر, 17:20

PE در حافظه و تفاوتش با فایل روی دیسک تا اینجا درباره‌ ی Private Memory Image-backed Memory و VAD صحبت کردیم حالا میخوایم ببینیم وقتی یک فایل اجرایی ویندوز وارد حافظه میشه چه اتفاقی براش میوفته و چرا نسخه‌ی داخل Memory معمولا با نسخه‌ی روی دیسک یکی نیست اول ببینیم PE چیه فرمت اجرایی اصلی بسیاری از برنامه‌های ویندوز PE یا Portable Executable هست فایل‌هایی مثل اینها رو در نظر بگیرید: program.exe library.dll ساختار کلی یک فایل PE تقریبا به این شکله: PE File │ ├── DOS Header ├── PE Signature ├── COFF File Header ├── Optional Header ├── Section Headers │ ├── .text ├── .rdata ├── .data ├── .pdata └── Other Sections همه‌ ی فایل‌ها لزوما تمام این Section ها رو ندارن و ترتیب و جزئیاتشون هم ممکنه متفاوت باشه هر بخش اطلاعات مشخصی داره و Windows از ساختار PE برای شناخت و بارگذاری فایل اجرایی استفاده میکنه وقتی PE وارد Memory میشه چه اتفاقی میوفته؟ فرض کنید این فایل روی دیسک وجود داره: C:\Program Files\App\program.exe وقتی Windows اون رو اجرا میکنه Loader سیستم‌ عامل فرایند اپلود رو انجام میده نمای ساده‌ : PE File on Disk | v Windows Loader | v Virtual Address Space | v Program Execution اما نکته اینجاست که Windows معمولا فایل رو به شکل یک تیکه ی خام و بدون تغییر داخل Memory کپی نمیکنه Loader باید ساختارهای فایل رو پردازش کنه و بخش‌های مختلف رو در جای مناسب قرار بده تفاوت Raw Offset و RVA اینجا به یکی از مفاهیم پایه‌ای میرسیم Raw Offset آدرسیه که برای پیدا کردن اطلاعات داخل خود فایل روی دیسک استفاده میشه مثلا: File Offset = 0x600 یعنی اطلاعات موردنظر از Offset برابر با 0x600 در فایل شروع میشه RVA مخفف این عبارته: RVA = Relative Virtual Address یعنی آدرس یک بخش نسبت به ImageBase در فضای آدرس مجازی مثلا: ImageBase = 0x140000000 RVA = 0x2000 در این مثال: VA = ImageBase + RVA پس: VA = 0x140002000 این محاسبه آدرس مجازی موردنظر رو به دست میاره به شرطی که از ImageBase واقعی Image بارگذاری‌ شده استفاده کنیم تفاوت اصلی Raw Offset | +--> موقعیت در فایل روی دیسک RVA | +--> موقعیت نسبت به ImageBase در فضای آدرس مجازی Raw Offset و RVA لزوما برابر نیستن برای تبدیل بین اونها باید اطلاعات Section مربوطه مثل PointerToRawData و VirtualAddress رو هم در نظر بگیریم ضمن اینکه همه‌ ی آدرس‌ های مجازی لزوما معادل یک بخش از داده‌های موجود در فایل نیستن برای مثال ممکنه یک بخش در حافظه بزرگتر از داده‌ی خام متناظر با خودش باشه ImageBase هر Image در حالت اپلود شده یک Base Address داره که بهش ImageBase میگیم برای مثال: Preferred ImageBase: 0x140000000 این آدرس محل ترجیحی تعریف‌ شده برای بارگذاری Image در فایل PE هست ولی Windows همیشه مجبور نیست Image رو دقیقا همون‌جا قرار بده چرا؟ چون ممکنه اون محدوده از فضای آدرس مجازی در دسترس نباشه یا شرایط دیگه‌ای وجود داشته باشه در این شرایط Image ممکنه در آدرس دیگری بارگذاری بشه اینجا میریم سراغ مفهوم بعدی Base Relocation فرض کنید برنامه انتظار داره Image در این آدرس قرار بگیره: Preferred Base = 0x140000000 اما در عمل در آدرس دیگه ای اپلود میشه: Actual Base = 0x180000000 اختلاف این دو آدرس: Delta = Actual Base - Preferred Base در صورتی که Image به Relocation نیاز داشته باشه و اطلاعات لازم موجود باشه Loader از اطلاعات Relocation برای اصلاح آدرس‌های وابسته به محل بارگذاری استفاده میکنه به این فرایند Base Relocation میگیم نکته‌ی مهم اینه که تمام آدرس‌ های داخل فایل نیازمند Relocation نیستن فقط مواردی که طبق ساختار PE به محل بارگذاری وابسته هستن و اطلاعات Relocation مناسب براشون وجود داره باید درست بشن Import Table چه کاری انجام میده؟ برنامه‌ها معمولا از توابع کتابخانه‌های دیگه استفاده میکنن برای مثال یک برنامه ممکنه به توابعی از این Moduleها نیاز داشته باشه: KERNEL32.dll USER32.dll ADVAPI32.dll اطلاعات مربوط به وابستگی‌های خارجی در ساختارهای Import فایل PE قرار میگیره Loader این وابستگی‌ ها رو پردازش میکنه و آدرس توابع موردنیاز رو در ساختار های مرتبط قرار میده
09 أكتوبر, 17:20

Waiting Time Process How much time did it spend just waiting for the CPU? This is: Waiting Time For example, a process enters the system: Arrival ↓ Waiting ↓ Running ↓ Waiting ↓ Running ↓ Finished The entire time the process spent waiting for the CPU in the ready queue counts as Waiting Time. Waiting Time = Time spent waiting for the CPU @reverseengine
09 أكتوبر, 17:05

Waiting Time Process چه مقدار از زمان رو فقط منتظر CPU بوده؟ این میشه: Waiting Time مثلا Process وارد سیستم میشه: Arrival ↓ Waiting ↓ Running ↓ Waiting ↓ Running ↓ Finished تمام زمانی که Process در صف آماده اجرا منتظر CPU بوده در Waiting Time حساب میشه Waiting Time = زمان انتظار برای CPU @reverseengine
09 أكتوبر, 17:03

Turnaround Time How long does it take for a job to complete from the time it is entered into the system until it is completely finished? Formula: Turnaround Time = Completion Time - Arrival Time For example: Arrival Time = 0 Completion = 10 So: Turnaround Time = 10 This means that the process has been in the system for 10 time units from start to finish @reverseengine
09 أكتوبر, 17:01

Turnaround Time از زمانی که یک Job وارد سیستم میشه تا زمانی که کاملا تموم بشه چقدر زمان گذشته؟ فرمول: Turnaround Time = Completion Time - Arrival Time مثلا: Arrival Time = 0 Completion = 10 پس: Turnaround Time = 10 یعنی Process از اول تا اخر 10 واحد زمانی داخل سیستم بوده @reverseengine
09 أكتوبر, 17:01

C change(&a); read_value(&b); read_value(&a); } Determine which function call reads the value modified by change and why. Note that the read_value function reads the value but does not return it, nor does it store it elsewhere. A key takeaway from this section: When analyzing data flow, it is not always necessary for two functions to interact directly. Sometimes, one function modifies a value in memory, and another function—with no direct connection to the first—accesses that same location via a pointer. In reverse engineering, tracing these very indirect connections can reveal the program's actual behavior. @reverseengine
09 أكتوبر, 16:55

If we mistakenly assume that two pointers are the same, we may analyze the order of memory changes and, as a result, the actual path of program execution incorrectly. Exercise: Examine the following code. C void change(int *p) { *p = 50 ; } void read_value(int *q) { int x = *q; int main() int a = 10; int b = 20;
09 أكتوبر, 16:54

Memory Aliasing Between Functions So far we have seen how two pointers can point to the same memory address. Now we want to examine a more important case. Suppose we have a function that modifies a value in memory and another function reads the same value from memory. The point here is that these two functions may use different pointers, but both access the same address. This is very important in reverse engineering, because we may not notice the relationship between them by examining each function alone. A simple example with C void set_value(int *p) { *p = 100; } int get_value(int *q) { return *q; } int main() { int value = 10; set_value(&value); int result = get_value(&value); return result; } What is happening here? First, the value is 10 Then the address of value is given to the set_value function The set_value function changes the value at that address to 100 Then the same address is given to the get_value function and the new value is read from memory Finally, the result is 100 The main point is that the pointers p and q have different names and variables, but in this example they both access the same memory location So the connection between these two functions is formed through memory, not through the return value of one of them Now, how does this look in assembly? Suppose the set_value function is compiled like this set_value: mov dword ptr [rdi], 100 ret This example is written according to the System V AMD64 calling convention, which is commonly used in 64-bit Linux programs According to this convention, the first argument of the function is placed in the RDI register Now let's examine this instruction mov dword ptr [rdi], 100 This instruction writes the number 100 to the address located in RDI Note that the value of RDI is different from the value stored in memory RDI holds the address and the expression [rdi] refers to the data located at that address Now look at the get_value function get_value: mov eax, dword ptr [rdi] ret Here the four-byte value located at the address RDI is read and placed in EAX. Since the return value of the function is of type int, this value is stored in the register EAX is returned, so one function writes to memory and another function reads from the same memory, provided that the address given to both functions points to the same memory location. Now how do we find it in reverse engineering? When you are examining a binary file in Ghidra or Disassembler, follow these steps Step 1: Examine the function arguments See what pointers the function receives and whether it uses them to read or write memory Step 2: Look for memory write instructions For example, consider the following command mov dword ptr [rdi], 100 This command shows that the function uses the address in RDI to write data Step 3: Examine other functions See if other functions receive the same address or associated pointer and later read something from that memory Don't just look for identical variable or register names - the address may be passed through another register, stored in a structure, or obtained with several calculation steps Step 4: Look for relationships between functions Don't just look at the function name or its return value - a function may change memory It does not return any value, but the effect of its change can be seen in another function a few steps later. Therefore, you must follow the path of transferring pointers and reading and writing memory. An important point about Must Alias ​​and May Alias. We cannot always be sure that two pointers point to the same address based on the similarity of the arguments. Here we have two important concepts. Must Alias ​​means that according to the information and conditions we have examined, two pointers must point to the same memory location. May Alias ​​means that two pointers may point to the same memory location, but the available information is not enough to prove this definitively. This difference is very important in program analysis.
09 أكتوبر, 16:54

C change(&a); read_value(&b); read_value(&a); } مشخص کنید کدوم فراخوانی مقدار تغییر کرده توسط change رو میخونه و چرا به این نکته هم توجه کن که تابع read_value مقدار رو میخونه اما اون رو برنمیگردونه و در جای دیگه‌ای هم ذخیره نمیکنه نکته‌ای که باید از این قسمت یادتون بمونه برای تحلیل جریان داده همیشه لازم نیست دو تابع مستقیما با هم ارتباط داشته باشن بعضی وقتا یه تابع مقداری رو داخل حافظه تغییر میده و تابع دیگه‌ای که هیچ ارتباط مستقیمی با اون نداره از طریق یه Pointer به همون محل دسترسی پیدا میکنه در مهندسی معکوس دنبال کردن همین ارتباط‌ های غیرمستقیم میتونه رفتار واقعی برنامه رو مشخص کنه @reverseengine
09 أكتوبر, 16:51

Memory Aliasing بین توابع تا اینجا دیدیم که چطور ممکنه دو تا Pointer به یه آدرس از حافظه اشاره کنن حالا میخوایم یه حالت مهم‌تر رو بررسی کنیم فرض کنید یه تابع داریم که یه مقدار رو توی حافظه تغییر میده و یه تابع دیگه همون مقدار رو از حافظه میخونه نکته اینجاست که ممکنه این دو تا تابع از Pointer های متفاوتی استفاده کنن ولی هر دو به یه آدرس مشترک دسترسی داشته باشن این موضوع توی مهندسی معکوس خیلی مهمه، چون ممکنه با بررسی هر تابع به‌تنهایی متوجه ارتباط بین اون‌ها نشیم یه مثال ساده با C void set_value(int *p) { *p = 100; } int get_value(int *q) { return *q; } int main() { int value = 10; set_value(&value); int result = get_value(&value); return result; } اینجا چه اتفاقی میفته؟ اول مقدار value برابر 10 هست بعد آدرس value به تابع set_value داده میشه تابع set_value مقدار موجود در اون آدرس رو به 100 تغییر میده بعد همون آدرس به تابع get_value داده میشه و مقدار جدید از حافظه خونده میشه در نهایت result برابر 100 میشه نکته اصلی اینه که Pointerهای p و q اسم و متغیرهای متفاوتی دارن اما در این مثال هر دو به همون خانه حافظه دسترسی دارن پس ارتباط بین این دو تابع از طریق حافظه شکل گرفته، نه از طریق مقدار برگشتی یکی از اون‌ها حالا این موضوع توی اسمبلی چه شکلیه؟ فرض کنید تابع set_value به این شکل کامپایل شده set_value: mov dword ptr [rdi], 100 ret این مثال بر اساس قرارداد فراخوانی System V AMD64 نوشته شده که معمولا توی برنامه‌های 64 بیتی لینوکس استفاده میشه طبق این قرار داد آرگومان اول تابع داخل رجیستر RDI قرار میگیره حالا این دستور رو بررسی کنیم mov dword ptr [rdi], 100 این دستور عدد 100 رو در آدرسی مینویسه که داخل RDI قرار داره دقت کنید که مقدار RDI با مقداری که داخل حافظه ذخیره شده فرق داره RDI آدرس رو نگه میداره و عبارت [rdi] به داده‌ ای اشاره میکنه که در اون آدرس قرار داره حالا تابع get_value رو ببینید get_value: mov eax, dword ptr [rdi] ret اینجا مقدار چهار بایتی موجود در آدرس RDI خونده میشه و داخل EAX قرار میگیره از اونجایی که مقدار برگشتی تابع از نوع int هست این مقدار در رجیستر EAX برگردونده میشه پس یه تابع توی حافظه مینویسه و تابع دیگه از همون حافظه میخونه البته به شرطی که آدرسی که به هر دو تابع داده شده به همون محل حافظه اشاره کنه حالا توی مهندسی معکوس چطور پیداش کنیم؟ وقتی داری یه فایل باینری رو توی Ghidra یا Disassembler بررسی میکنید این مراحل رو دنبال کنید مرحله اول: آرگومان‌های تابع رو بررسی کنید ببینید تابع چه Pointer هایی دریافت میکنه و از اون‌ها برای خوندن یا نوشتن حافظه استفاده میکنه یا نه مرحله دوم: دنبال دستورهای نوشتن حافظه بگردید مثلا دستور زیر رو در نظر بگیرید mov dword ptr [rdi], 100 این دستور نشون میده تابع از آدرسی که داخل RDI قرار داره برای نوشتن داده استفاده میکنه مرحله سوم: تابع‌های دیگه رو بررسی کنید ببینید تابع‌ های دیگه همون آدرس یا Pointer مرتبطی رو دریافت میکنن و بعدا از اون حافظه مقداری میخونن یا نه فقط دنبال اسم یکسان متغیرها یا رجیسترها نباشید ممکنه آدرس از طریق رجیستر دیگه‌ای منتقل بشه داخل ساختار ذخیره بشه یا با چند مرحله محاسبه به دست بیاد مرحله چهارم: ارتباط بین تابع‌ ها رو دنبال کنید فقط به اسم تابع یا مقدار برگشتی اون توجه نکنیدممکنه یه تابع حافظه رو تغییر بده و هیچ مقداری برنگردونه ولی اثر تغییرش چند مرحله بعد داخل تابع دیگه‌ای دیده بشه برای همین باید مسیر انتقال Pointer ها و خوندن و نوشتن حافظه رو دنبال کنید یه نکته مهم درباره Must Alias و May Alias همیشه نمیتونیم از روی شباهت آرگومان ها مطمئن بشیم که دو Pointer به یه آدرس اشاره میکنن اینجا دو مفهوم مهم داریم Must Alias یعنی طبق اطلاعات و شرایطی که بررسی کردیم دو Pointer حتما به یه محل حافظه اشاره میکنن May Alias یعنی ممکنه دو Pointer به یه محل حافظه اشاره کنن ولی اطلاعات موجود برای اثبات قطعی این موضوع کافی نیست این تفاوت توی تحلیل برنامه اهمیت زیادی داره اگه دو Pointer رو اشتباهی یکی فرض کنیم ممکنه ترتیب تغییرات حافظه و در نتیجه مسیر واقعی اجرای برنامه رو اشتباه تحلیل کنیم تمرین: کد زیر رو بررسی کنید C void change(int *p) { *p = 50; } void read_value(int *q) { int x = *q; } int main() { int a = 10; int b = 20;
09 أكتوبر, 16:51

In previous sections, we learned where data enters the program and how it moves through registers and memory. Now, we have learned how to analyze program execution conditions to determine which inputs can reach a specific path. When we combine these concepts: Fuzzing ↓ Interesting Input ↓ Dynamic Taint Analysis ↓ Data Flow ↓ Symbolic Execution ↓ Path Constraints ↓ New Input ↓ Crash Analysis ↓ Root Cause We no longer limit ourselves to simply running the program and observing crashes; we can trace the data path, understand the necessary conditions, and generate more targeted inputs to explore new paths. @reverseengine
09 أكتوبر, 16:30

C return 0; } Standard analysis reveals that the program has several conditions. However, Symbolic Execution can extract the conditions necessary to reach a successful execution path: value >= 100 AND value <= 200 AND value == 150 Consequently, value = 150 is a valid solution. This capability has significant applications in reverse engineering: Analyzing validation logic Finding the inputs required to reach a specific path Examining complex conditions within the binary Analyzing paths that lead to a crash Determining which constraints make a path reachable or unreachable Of course, in a real binary, conditions might depend on multiple functions or be influenced by memory and runtime state. In such scenarios, constructing an accurate model of the program becomes more difficult. The difference between Symbolic and Concrete Execution Make sure to understand this distinction well. Concrete Execution The program runs with a concrete (actual) value. x = 15 In this case, program processing is based on the actual value of 15. Symbolic Execution Instead of a concrete value, we use a symbolic value. x = X When conditions are encountered, constraints are generated for X. A simple comparison An important point Symbolic Execution does not guarantee that all program paths will be examined. Loops and a large number of branches can lead to a "path explosion." 😁 Complex calculations and interactions with the operating system or external libraries can also complicate the analysis. So, even Symbolic Execution has its own limitations. Connection to Fuzzing This is where Symbolic Execution demonstrates one of its most interesting applications. Suppose a fuzzer has generated an input and executed the program: Input ↓ Program ↓ Branch A ↓ Branch B ↓ Crash Now, let's examine a new execution path—one that wasn't reached because a specific condition wasn't met. For instance, the program contains a condition like this: JavaScript if (x == 1337) dangerous_function(); If our current input is: x = 10 The program does not enter dangerous_function. Symbolic Execution can extract the branch condition: X == 1337 Then, the solver suggests a valid value: X = 1337 This value can be used as a new input in the testing process. So, the overall workflow looks like this: Fuzzer ↓ Input ↓ Program Execution ↓ Uncovered Branch ↓ Path Constraint ↓ SMT Solver ↓ New Input ↓ Fuzzing Again This concept is also utilized in methods like Concolic Execution. In Concolic Execution, the program runs with concrete inputs while values ​​and conditions are simultaneously tracked symbolically; thus, concrete execution and symbolic analysis are combined. Practical Tools To gain hands-on experience with this topic, a few tools are worth exploring: angr A binary analysis framework featuring capabilities such as Symbolic Execution and path analysis. Official repository: https://github.com/angr/angr Triton A framework for Dynamic Binary Analysis that offers capabilities like Taint Analysis and Symbolic Execution. Official repository: https://github.com/JonathanSalwan/Triton Z3 An SMT solver capable of solving symbolic constraints. Official repository: https://github.com/Z3Prover/z3 These tools do not perform exactly the same function: angr Is primarily used for constructing and analyzing symbolic paths within binaries. Triton Provides capabilities for dynamic binary analysis and symbolic computation. Z3 Is a constraint solver; while it is not a standalone, all-in-one tool for analyzing binary execution paths, it is a crucial component of such systems. It’s not possible [to do it that way]. The general workflow of Symbolic Execution looks like this: Symbolic Input ↓ Program Analysis ↓ Branch Conditions ↓ Path Constraints ↓ SMT Solver ↓ Possible Input
09 أكتوبر, 16:30

This is crucial for binary analysis, as processors operate on values ​​of specific sizes—such as 8, 32, or 64 bits—and their behavior is always... It is not like mathematical integers. The connection between Symbolic Execution and Reverse Engineering Suppose you have a binary containing the following logic: C++ int validate(int value) { if (value < 100) return 0; if (value > 200) return 0; if (value == 150) return 1;
09 أكتوبر, 16:30

Part 34 Buffer Overflow
Symbolic Execution From Concrete Inputs to Symbolic Inputs So far, we have learned how to trace data through a program and understand how it leads to a crash. Now, let's take it a step further. Imagine a program has a conditional statement that directs execution along a specific path only when the input holds a particular value. We could try thousands of different inputs until we finally reach that path. However, there is a better way. Instead of testing only concrete values, we can use *Symbolic Execution* to determine the conditions that must be met for the program to enter the desired path. Here, rather than guessing inputs, we attempt to extract and solve the path constraints. What is the core idea? Suppose we have the following program: C int check(int x) { if (x == 1337) return 1; return 0; } During a standard execution, we might provide this value: x = 10 The program checks the condition: 10 == 1337 The result is false, and the program follows the second path. Now, suppose we use a symbolic value instead of a concrete one: x = X In this case, the program's condition becomes: X == 1337 A solver can deduce that for the program to enter the first path, the value of X must be 1337. Thus, instead of testing various values, we can determine the necessary condition to reach that path. Path Constraint Suppose we have this code: C int process(int x) { if (x > 10) { if (x < 20) return 1; } return 0; } To reach return 1, both conditions must be met: x > 10 AND x < 20 We refer to the set of conditions that must hold true to reach a specific path as a Path Constraint So, the constraint for the path in question is: 10 < x < 20 Since x is of type int, the possible solutions include these values: 11, 12, 13, 14, 15, 16, 17, 18, 19 The solver can find one of the valid solutions For example: x = 15 An important point is that Symbolic Execution does not consider only the current condition; it also maintains the path's preceding conditions, because reaching a specific point in the program requires all necessary conditions along the path to be satisfied. When paths branch out Consider this program: C int process(int x) { if (x > 10) return 1; if (x == 5) return 2; return 0; } Using Symbolic Execution, we can examine the program's various paths: x = X | x > 10 ? / \ Yes No | | return 1 x == 5 ? / \ Yes No | | return 2 return 0 Now, suppose we want to reach the return 2 statement. First, the initial condition must *not* hold: X <= 10 Then, the second condition *must* hold: X == 5 So, our path constraint becomes: X <= 10 AND X == 5 Consequently: X = 5 ...is considered a valid input to reach this path. This is the core idea behind Symbolic Execution: We trace the program and gather the necessary conditions for each path. The Role of the SMT Solver So far, we’ve seen that we can extract path conditions, but solving them manually becomes difficult when the number of conditions grows. This is where we use a tool called an SMT Solver. One well-known tool in this field is Z3, developed by Microsoft Research. Suppose our conditions are: X > 10 X < 20 X != 15 The solver must find a value that satisfies all conditions simultaneously. For example: X = 11 This value is valid because it satisfies all three conditions. However, if the conditions were: X > 20 X < 10 No real value could satisfy both conditions at the same time. Consequently, the problem is Unsatisfiable; meaning there is no valid solution. Conversely, if at least one solution exists, the problem is considered Satisfiable. Of course, SMT solvers aren't limited to simple numerical conditions. Depending on the logic and capabilities of the solver, they can also handle constraints involving bit-vectors and bitwise operations.
09 أكتوبر, 16:30

C return 0; } با تحلیل معمولی میفهمیم برنامه چند شرط داره ولی Symbolic Execution میتونه شرط‌های لازم برای رسیدن به مسیر موفق رو استخراج کنه value >= 100 AND value <= 200 AND value == 150 در نتیجه value = 150 یک جواب معتبره این قابلیت در Reverse Engineering کاربردهای مهمی داره تحلیل منطق اعتبارسنجی پیدا کردن ورودی‌ های لازم برای رسیدن به یک مسیر مشخص بررسی شرطهای پیچیده داخل باینری تحلیل مسیر های منتهی به Crash و بررسی اینکه چه محدودیت‌ هایی باعث میشن یک مسیر قابل دسترسی یا غیرقابل دسترسی باشه البته در باینری واقعی ممکنه شرط‌ها به چند تابع مختلف وابسته باشن یا تحت تأثیر حافظه و وضعیت Runtime قرار بگیرن در این شرایط ساختن مدل دقیق برنامه سخت‌تر میشه تفاوت Symbolic و Concrete Execution این تفاوت رو خوب یاد بگیرید Concrete Execution برنامه با مقدار واقعی اجرا میشه x = 15 در این حالت پردازش برنامه بر اساس مقدار واقعی 15 انجام میشه Symbolic Execution به جای مقدار واقعی از یک مقدار نمادین استفاده میکنیم x = X وقت رسیدن به شرط‌ ها محدودیت‌ هایی روی X ساخته میشن مقایسه ساده یک نکته مهم Symbolic Execution تضمین نمیکنه تمام مسیرهای برنامه رو بررسی کنه حلقه‌ها و تعداد زیاد شاخه‌ ها میتونن باعث ترکیدن مسیرها بشن😁 محاسبات پیچیده و تعامل با سیستم‌ عامل یا کتابخونه‌ های خارجی هم ممکنه تحلیل رو دشوار کنن پس حتی Symbolic Execution هم محدودیت‌های خودش رو داره ارتباط با Fuzzing اینجا Symbolic Execution به یکی از کاربردهای جذابش میرسه فرض کنید Fuzzer یک ورودی تولید کرده و برنامه رو اجرا کرده Input ↓ Program ↓ Branch A ↓ Branch B ↓ Crash حالا میخوایم مسیر جدیدی رو بررسی کنیم که به خاطر برقرار نشدن یک شرط بهش نرسیدیم مثلا برنامه چنین شرطی داره Javascript if (x == 1337) dangerous_function(); اگر ورودی فعلی ما این باشه x = 10 برنامه وارد dangerous_function نمیشه Symbolic Execution میتونه شرط شاخه رو استخراج کنه X == 1337 بعد حل‌ کننده یک مقدار معتبر پیشنهاد میده X = 1337 این مقدار رو میشه به عنوان ورودی جدید در فرآیند تست استفاده کرد پس ارتباط کلی میشه Fuzzer ↓ Input ↓ Program Execution ↓ Uncovered Branch ↓ Path Constraint ↓ SMT Solver ↓ New Input ↓ Fuzzing Again این ایده در روش‌هایی مثل Concolic Execution هم استفاده میشه در Concolic Execution برنامه با ورودی‌ های واقعی اجرا میشه ولی همزمان مقادیر و شرط‌ها به شکل نمادین هم دنبال میشن به این ترتیب اجرای واقعی و تحلیل نمادین در کنار هم قرار میگیرن ابزارهای کاربردی برای آشنایی عملی با این مبحث چند ابزار ارزش بررسی دارن angr یک Framework برای تحلیل باینری که قابلیت‌هایی مثل Symbolic Execution و تحلیل مسیر داره مخزن رسمی: https://github.com/angr/angr Triton یک Framework برای Dynamic Binary Analysis که قابلیت‌هایی مثل Taint Analysis و Symbolic Execution ارائه میده مخزن رسمی: https://github.com/JonathanSalwan/Triton Z3 یک SMT Solver که میتونه محدودیت‌های نمادین رو حل کنه مخزن رسمی: https://github.com/Z3Prover/z3 این ابزارها دقیقا یک کار رو انجام نمیدن angr بیشتر برای ساختن و تحلیل مسیرهای نمادین در باینری کاربرد داره Triton قابلیت‌های تحلیل پویای باینری و محاسبات نمادین رو فراهم می‌کنه Z3 هم حل‌ کننده محدودیت‌ هاست و به‌ تنهایی یک ابزار کامل برای تحلیل مسیرهای اجرایی باینری محسوب نمیشه مسیر کلی Symbolic Execution این شکلیه Symbolic Input ↓ Program Analysis ↓ Branch Conditions ↓ Path Constraints ↓ SMT Solver ↓ Possible Input در قسمت‌های قبلی یاد گرفتیم داده از کجا وارد برنامه میشه و چطور در Register ها و Memory حرکت میکنه حالا یاد گرفتیم چطور شرطهای اجرای برنامه رو تحلیل کنیم تا بفهمیم چه ورودی‌ هایی می‌تونن به یک مسیر خاص برسن وقتی این مفاهیم رو کنار هم قرار بدیم Fuzzing ↓ Interesting Input ↓ Dynamic Taint Analysis ↓ Data Flow ↓ Symbolic Execution ↓ Path Constraints ↓ New Input ↓ Crash Analysis ↓ Root Cause دیگه فقط به اجرای برنامه و دیدن Crash اکتفا نمیکنیم میتونیم مسیر داده رو دنبال کنیم شرطهای لازم رو بفهمیم و برای بررسی مسیر های جدید ورودی‌ های هدفمند تری بسازیم @reverseengine
09 أكتوبر, 16:28

نستخدم ملفات تعريف الارتباط لتحسين تجربة التصفح لديك.
بالنقر على «قبول الكل»، فإنك توافق على سياسة استخدام ملفات تعريف الارتباط.