したらばTOP ■掲示板に戻る■ 全部 1-100 最新50 | メール | |

管理人の独り言(プログラミング関連)

1057774さん:2008/12/10(水) 23:29:23 ID:CENbthlg0
あの・・・
アセンブリ言語ってこんなに疲れるんですか?
-----
section .text
global _start
_start:
;GetBaseAddressの呼び出し
push dword [esp] ;RetAdをプッシュ
call GetBaseAddress
add esp, 4

cmp eax, 0
je error

;OreGetProcAddressの呼び出し
push psGetProcAddress
push eax
call OreGetProcAddress
add esp, 8

cmp eax, 0
je error

;終わり
xor eax, eax
ret
error:
mov eax, 1
ret

psGetProcAddress:
db "GetProcAddress", 0

;第一引数 DLLのベースポインタ
;第二引数 手に入れる関数の名前
;返却 見つかったらそのアドレス、無いならば0
OreGetProcAddress:
;スタックフレームる
;[ebp] : NumberOfNames
;[ebp - 4] : AddressOfFunctions
;[ebp - 8] : AddressOfNames
;[ebp - 0Ch] : AddressOfNameOrdinals
;[ebp - 10h] : baseAddress
push ebp
mov ebp, esp
sub esp, 14h

;IMAGE_NT_HEADERS32構造体へのアドレスを取得(edi)
mov ecx, dword [ebp + 8] ;第一引数
mov edi, dword [ecx + 3Ch] ;IMAGE_NT_HEADERS32
add edi, ecx ;ベースポインタを足す

;IMAGE_NT_HEADERS32::IMAGE_OPTIONAL_HEADER32::DataDirectoryのアドレス取得(eax)&
;エクスポートテーブル(IMAGE_EXPORT_DIRECTORY)のアドレス取得(edi)
xor eax, eax
mov ax, word [edi + 14h] ;SizeOfOptionalHeader
lea eax, [edi + eax + 18h] ;IMAGE_NT_HEADERS32全体の大きさを足す
sub eax, 80h ;DataDirectory分引く 80h == sizeof(IMAGE_DATA_DIRECTORY) * 16
mov edi, dword [eax] ;エクスポートテーブルのアドレス取得
add edi, ecx

;ここまでで
;ecx : DLLのベースポインタ, edi : エクスポートテーブル(IMAGE_EXPORT_DIRECTORY)のアドレス

;NumberOfNames格納
mov eax, dword [edi + 18h]
mov [ebp], eax

;AddressOfFunctions格納
mov eax, dword [edi + 1Ch]
add eax, ecx
mov dword [ebp - 4], eax

;AddressOfNames格納
mov eax, dword [edi + 20h]
add eax, ecx
mov dword [ebp - 8], eax

;AddressOfNameOrdinals格納
mov eax, dword[edi + 24h]
add eax, ecx
mov dword [ebp - 0Ch], eax

;ベースアドレス退避
mov dword [ebp - 10h], ecx

;API名の検索
mov esi, dword [ebp - 8]
mov edx, esi
mov edi, dword [esi]
add edi, ecx
xor ecx, ecx
mov ebx, dword [ebp + 0Ch]


OreGetP_loop:
mov esi, ebx

OreGetP_check:
;API名が一致か?
cmpsb
jne OreGetP_next

cmp byte [edi], 0 ;終端か?
jne OreGetP_check
cmp byte [esi], 0 ;終端か?
jne OreGetP_check
jmp OreGetP_found

OreGetP_next:
inc ecx
cmp ecx, dword [ebp]
jge OreGetP_notfound

add edx, 4
mov esi, edx
mov edi, dword [esi]
add edi, dword [ebp - 10h]
jmp OreGetP_loop

OreGetP_found:
mov ebx, esi
inc ebx
shl ecx, 1
mov esi, dword [ebp - 0Ch]
add esi, ecx
xor eax, eax
mov ax, word [esi]
shl eax, 2
mov esi, [ebp - 4]
add esi, eax
mov edi, dword [esi]
add edi, dword [ebp - 10h]
mov eax, edi

leave
ret

OreGetP_notfound:
xor eax, eax
leave
ret

;第一引数 適当なアドレス
;返却 PEヘッダとかが見つかったらそのアドレス、無いならば0
GetBaseAddress:
;リミッター
mov ecx, 10h
;Kernel32へのRetAd
mov eax, dword [esp + 4]
;マスク
and eax, 0FFFF0000h
GetK_roop:
;リミッターのチェック
test ecx, ecx ;cmp ecx, 0
je GetK_notfound
;MZシグネチャのチェック
cmp word[eax], 'MZ'
je GetK_check_PE
GetK_next:
;次へ
sub eax, 10000h
dec ecx
jmp GetK_roop
GetK_check_PE:
;PEのチェック
mov edi, [eax + 3Ch] ;IMAGE_DOS_HEADER::e_lfanew : File address of new exe header
add edi, eax
cmp dword [edi], 00004550h ;'PE\0\0'
je GetK_end
jmp GetK_next
GetK_notfound:
xor eax, eax
GetK_end:
ret


新着レスの表示


名前: E-mail(省略可)

※書き込む際の注意事項はこちら

※画像アップローダーはこちら

(画像を表示できるのは「画像リンクのサムネイル表示」がオンの掲示板に限ります)

掲示板管理者へ連絡 無料レンタル掲示板