探索Apple高级调试与逆向工程:实战LLDB、Python与DTrace

5星 · 超过95%的资源 需积分: 10 22 下载量 86 浏览量 更新于2024-07-18 1 收藏 64.78MB PDF 举报
《高级苹果调试与逆向工程:第二版》是一本由Derek Selander编写的关于iOS开发领域的深入指南,专为那些想要探索Apple平台代码调试、LLDB(Low-Level Debugger)、Python编程以及DTrace技术的开发者设计。这本书是Razeware LLC的版权作品,所有权利受到严格保护,未经版权所有者事先书面许可,不得复制或分发任何部分,包括文本、图像和源代码。 本书的核心内容围绕苹果高级调试技巧展开,旨在帮助读者理解如何高效地调试iOS应用程序,特别是在使用Apple的内置调试工具LLDB进行底层调试时。作者Derek Selander凭借对调试的热情和专业技能,深入浅出地讲解了这些复杂的技术,让读者能够更好地诊断和修复iOS应用中的问题。 书中还涉及Python在iOS开发中的应用,Python作为一种强大的脚本语言,可以帮助开发者编写自动化测试和调试工具,提升调试效率。此外,DTrace技术也被详细剖析,这是一种强大的系统性能分析工具,可以深入到操作系统级别追踪程序运行的内部细节。 值得注意的是,本书是以“原样提供,无任何形式保证”的方式发布的,不承担任何明示或暗示的保证,包括适销性、特定用途的适用性和非侵权性。这意味着读者在使用软件或相关材料时应自行承担风险,作者和版权所有者不对任何索赔、损失或其他责任负责。 在版权方面,书中的所有商标和注册商标均为其各自所有者的所有物。如果你是寻找深入学习iOS调试和逆向工程的专业资源,这本书无疑是一个宝贵的学习资源,它源自raywenderlich.com这样的知名技术平台,提供了丰富的实践经验和理论知识。 《高级苹果调试与逆向工程:第二版》是一本实用且深入的教程,适合iOS开发者提升调试和分析技能,同时对于希望通过Python和DTrace工具深入了解iOS系统内部运作的读者来说,这本书也是一份不可或缺的参考资料。
2018-04-04 上传
Advanced Apple Debugging & Reverse Engineering v0.9.5 Explore code through LLDB, Python and DTrace, to discover more about any program than you ever thought possible. Table of Contents 1. Getting Started In this chapter, you’re going to get acquainted with LLDB and investigate the process of introspecting and debugging a program. You’ll start off by introspecting a program you didn’t even write — Xcode! 2. Help & Apropos Just like any respectable developer tool, LLDB ships with a healthy amount of documentation. Knowing how to navigate through this documentation — including some of the more obscure command flags — is essential to mastering LLDB. 3. Attaching with LLDB Now that you’ve learned about the two most essential commands, help and apropos, it’s time to investigate how LLDB attaches itself to processes. You’ll learn all the different ways you can attach LLDB to processes using various options, as well as what happens behind the scenes when attaching to processes. 4. Stopping in Code Whether you’re using Swift, Objective-C, C++, C, or an entirely different language in your technology stack, you’ll need to learn how to create breakpoints. It’s easy to click on the side panel in Xcode to create a breakpoint using the GUI, but the LLDB console can give you much more control over breakpoints. 5. Expression Now that you’ve learned how to set breakpoints so the debugger will stop in your code, it’s time to get useful information out of whatever software you’re debugging. In this chapter you’ll learn about the expression command, which allows you to execute arbitrary code in the debugger. 6. Thread, Frame & Stepping Around You’ve learned how to create breakpoints, how to print and modify values, as well as how to execute code while paused in the debugger. But so far you’ve been left high and dry on how to move around in the debugger and inspect data beyond the immediate. In this chapter, you’ll learn how to move the debugger in and out of functions while LLDB is currently paused. 7. Image it’s time to explore one of the best tools for finding code of interest through the powers of LLDB. In this chapter, you’ll take a deep dive into the image command. 8. Persisting & Customizing Commands In this chapter, you’ll learn how to persist these choices through the .lldbinit file. By persisting your choices and making convenience commands for yourself, your debugging sessions will run much more smoothly and efficiently. This is also an important concept because from here on out, you’ll use the .lldbinit file on a regular basis. 9. Regex Commands In the previous chapter, you learned about the command alias command as well as how to persist commands through the lldbinit file. Unfortunately, command alias has some limitations. The LLDB command command regex acts much like command alias, except you can provide a regular expression for input which will be parsed and applied to the action part of the command. 10. Assembly Register Calling Convention Now you’ve gained a basic understanding of how to maneuver around the debugger, it’s time to take a step down the executable Jenga tower and explore the 1s and 0s that make up your source code. This section will focus on the low-level aspects of debugging. 11. Assembly & Memory In this chapter, you’ll explore how a program executes. You’ll look at a special register used to tell the processor where it should read the next instruction from, as well as how different sizes and groupings of memory can produce very different results. 12. Assembly and the Stack What does being "passed on the stack" mean exactly? It’s time to take a deeper dive into what happens when a function is called from an assembly standpoint by exploring some “stack related” registers as well as the contents in the stack. 13. Hello, Ptrace As alluded to in the introduction to this book, debugging is not entirely about just fixing stuff. Debugging is the process of gaining a better understanding of what’s happening behind the scenes. In this chapter, you’ll explore the foundation of debugging, namely, a system call responsible for a process attaching itself to another process: ptrace. 14. Dynamic Frameworks With dynamic frameworks comes a very interesting aspect of learning, debugging, and reverse engineering. Since you have the ability to load the framework at runtime, you can use LLDB to explore and execute code at runtime, which is great for spelunking in both public and private frameworks. 15. Hooking & Executing Code with dlopen & dlsym It’s time to learn about the complementary skills of developing with these frameworks. In this chapter, you’re going to learn about methods and strategies to “hook” into Swift and C code as well as execute methods you wouldn’t normally have access to. 16. Exploring and Method Swizzling Objective-C Frameworks You’ll cap off this round of dynamic framework exploration by digging into Objective-C frameworks using the Objective-C runtime to hook and execute methods of interest. 17. Hello Script Bridging Next up in the tradeoff between convenience and complexity is LLDB’s script bridging. With script bridging, you can do nearly anything you like. Script bridging is a Python interface LLDB uses to help extend the debugger to accomplish your wildest debugging dreams. 18. Debugging Script Bridging You need a methodical way to figure out what went wrong in your LLDB script so you don’t pull your hair out. In this chapter, you’ll explore how to inspect your LLDB Python scripts using the Python pdb module, which is used for debugging Python scripts. 19. Script Bridging Classes and Hierarchy You’ve learned the essentials of working with LLDB’s Python module, as well as how to correct any errors using Python’s PDB debugging module. Now you’ll explore the main players within the lldb Python module for a good overview of the main parts. In this chapter, you’ll add some arguments to this script and deal with some annoying edge cases, such handling commands differently between Objective-C and Swift. 20. Script Bridging with Options & Arguments When you’re creating a custom debugging command, you’ll often want to slightly tweak functionality based upon options or arguments supplied to your command. A custom LLDB command that can do a job only one way is a boring one-trick pony. In this chapter, you’ll explore how to pass optional parameters (aka options) as well as arguments (parameters which are expected) to your custom command to alter functionality or logic in your custom LLDB scripts. 21. Script Bridging with SBValue & Memory So far, when evaluating JIT code (i.e. Objective-C, Swift, C, etc. code that’s executed through your Python script), you’ve used a small set of APIs to evaluate the code. It’s time to talk about a new class in the lldb Python module, SBValue, and how it can simplify the parsing of JIT code output. 22. SB Examples, Improved Lookup For the rest of the chapters in this section, you'll focus on Python scripts. As alluded to in the previous chapter, the image lookup -rn command is on its way out. When you finish this chapter, you’ll have a new script named "lookup" which queries in a much cleaner way. 23. SB Examples, Resymbolicating a Stripped ObjC Binary When LLDB comes up against a stripped executable (an executable devoid of DWARF debugging information), LLDB won’t have the symbol information to give you the stack trace. Instead, LLDB will generate a synthetic name for a method it recognizes as a method, but doesn’t know what to call it. In this chapter, you’ll build an LLDB script that will resymbolicate stripped Objective-C functions in a stack trace. 24. SB Examples, Malloc Logging For the final chapter in this section, you’ll go through the same steps I myself took to understand how the MallocStackLogging environment variable is used to get the stack trace when an object is created. From there, you’ll create a custom LLDB command which gives you the stack trace of when an object was allocated or deallocated in memory — even after the stack trace is long gone from the debugger. 25. Hello, DTrace You’ll explore a very small section of what DTrace is capable of doing by tracing Objective-C code in already compiled applications. Using DTrace to observe iOS frameworks (like UIKit) can give you an incredible insight into how the authors designed their code. 26. Intermediate DTrace This chapter will act as a grab-bag of more DTrace fundamentals, destructive actions (yay!), as well as how to use DTrace with Swift. In this chapter, you'll learn additional ways DTrace can profile code, as well as how to augment existing code without laying a finger on the actual executable itself. 27. DTrace vs objc_msgSend In this chapter, you'll use DTrace to hook objc_msgSend's entry probe and pull out the class name along with the Objective-C selector for that class. By the end of this chapter, you'll have LLDB generating a DTrace script which only generates tracing info for code implemented within the main executable that calls objc_msgSend.
2023-05-22 上传