Arrays are used by programmers to define an ordered set of similar data items. Malware sometimes uses an array of pointers to strings that contain multiple hostnames that are used as options for connections.
Example 6-24 shows two arrays used by one program, both of which are
set during the iteration through the for
loop. Array a
is locally defined, and array b
is
globally defined. These definitions will impact the assembly code.
Example 6-24. C code for an array
int b[5]
= {123,87,487,7,978}; void main() { int i;int a[5];
for(i = 0; i<5; i++) {a[i] = i;
b[i] = i;
} }
In assembly, arrays are accessed using a base address as a starting point. The size of each element is not always obvious, but it can be determined by seeing how the array is being indexed. Example 6-25 shows the assembly code for Example 6-24.
Example 6-25. Assembly code for the array in Example 6-24
00401006 mov [ebp+var_18], 0 0040100D jmp short loc_401018 0040100F loc_40100F: 0040100F mov eax, [ebp+var_18] 00401012 add eax, 1 00401015 mov [ebp+var_18], eax 00401018 loc_401018: 00401018 cmp [ebp+var_18], 5 0040101C jge short loc_401037 0040101E mov ecx, [ebp+var_18] 00401021 mov edx, [ebp+var_18] 00401024 mov [ebp+ecx*4+var_14], edx ❶ 00401028 mov eax, [ebp+var_18] 0040102B mov ecx, [ebp+var_18] 0040102E mov dword_40A000[ecx*4], eax ❷ 00401035 jmp short loc_40100F
In this listing, the base address of array b
corresponds to dword_40A000
, and the base address of array
a
corresponds to var_14
. Since
these are both arrays of integers, each element is of size 4, although the instructions at ❶ and ❷ differ for
accessing the two arrays. In both cases, ecx
is used as the
index, which is multiplied by 4 to account for the size of the elements. The resulting value is
added to the base address of the array to access the proper array element.
18.227.46.69