Let’s begin by thinking about how a malware author develops code to determine how to group instructions. Malware is typically developed using a high-level language, most commonly C. A code construct is a code abstraction level that defines a functional property but not the details of its implementation. Examples of code constructs include loops, if
statements, linked lists, switch
statements, and so on. Programs can be broken down into individual constructs that, when combined, implement the overall functionality of the program.
This chapter is designed to start you on your way with a discussion of more than ten different C code constructs. We’ll examine each construct in assembly, although the purpose of this chapter is to assist you in doing the reverse: Your goal as a malware analyst will be to go from disassembly to high-level constructs. Learning in this reverse direction is often easier, because computer programmers are accustomed to reading and understanding source code.
This chapter will focus on how the most common and difficult constructs, such as loops and conditional statements, are compiled. After you’ve built a foundation with these, you’ll learn how to develop a high-level picture of code functionality quickly.
In addition to discussing the different constructs, we’ll also examine the differences between compilers, because compiler versions and settings can impact how a particular construct appears in disassembly. We’ll evaluate two different ways that switch
statements and function calls can be compiled using different compilers. This chapter will dig fairly deeply into C code constructs, so the more you understand about C and programming in general, the more you’ll get out of it. For help with the C language, have a look at the classic The C Programming Language by Brian Kernighan and Dennis Ritchie (Prentice-Hall, 1988). Most malware is written in C, although it is sometimes written in Delphi and C++. C is a simple language with a close relationship to assembly, so it is the most logical place for a new malware analyst to start.
As you read this chapter, remember that your goal is to understand the overall functionality of a program, not to analyze every single instruction. Keep this in mind, and don’t get bogged down with the minutiae. Focus on the way programs work in general, not on how they do each particular thing.