Reverse Engineering for Beginners

(avery) #1

CHAPTER 38. NETWORK ADDRESS CALCULATION EXAMPLE CHAPTER 38. NETWORK ADDRESS CALCULATION EXAMPLE


; EAX=000000dd
push eax
push OFFSET $SG2973 ; '%d.%d.%d.%d'
call DWORD PTR impprintf
add esp, 20
pop ebp
ret 0
_print_as_IP ENDP


Optimizing MSVC 2012 does almost the same, but without unnecessary reloading of the input value:


Listing 38.5: Optimizing MSVC 2012 /Ob0

_a$ = 8 ; size = 4
_print_as_IP PROC
mov ecx, DWORD PTR _a$[esp-4]
; ECX=ddccbbaa
movzx eax, cl
; EAX=000000aa
push eax
mov eax, ecx
; EAX=ddccbbaa
shr eax, 8
; EAX=00ddccbb
and eax, 255
; EAX=000000bb
push eax
mov eax, ecx
; EAX=ddccbbaa
shr eax, 16
; EAX=0000ddcc
and eax, 255
; EAX=000000cc
push eax
; ECX=ddccbbaa
shr ecx, 24
; ECX=000000dd
push ecx
push OFFSET $SG3020 ; '%d.%d.%d.%d'
call DWORD PTR impprintf
add esp, 20
ret 0
_print_as_IP ENDP


38.4 form_netmask() and set_bit()


form_netmask()makes a network mask value fromCIDRnotation. Of course, it would be much effective to use here
some kind of a precalculated table, but we consider it in this way intentionally, to demonstrate bit shifts. We will also write
a separate functionset_bit(). It’s a not very good idea to create a function for such primitive operation, but it would be
easy to understand how it all works.


Listing 38.6: Optimizing MSVC 2012 /Ob0

_input$ = 8 ; size = 4
_bit$ = 12 ; size = 4
_set_bit PROC
mov ecx, DWORD PTR _bit$[esp-4]
mov eax, 1
shl eax, cl
or eax, DWORD PTR _input$[esp-4]
ret 0
_set_bit ENDP


_netmask_bits$ = 8 ; size = 1
_form_netmask PROC
push ebx
push esi
movzx esi, BYTE PTR _netmask_bits$[esp+4]

Free download pdf