-
-
我也来mygetprocaddress
-
发表于: 2005-9-1 18:45 5032
-
以前看到有很多人写自己的这个函数...我也写过一个,是用在我的壳中的,用delphi写的,不过编译出来的代码完全支持重定位...
代码由于壳的某些原因和我的菜鸟级别因素,有些地方很乱,有什么错误的地方还请大家提出...
kongfoo的替换代码:
procedure PackMove(ibase,idest,isize:Cardinal);stdcall;assembler;
asm
pushad
pushfd
mov ecx,isize
mov esi,ibase
mov edi,idest
rep movsb
popfd
popad
end;
///////////////////////////////////////
其中有很多地方代码可以简便的地方没有简便是因为能力不足,还有是因为考虑了在壳中的一些因素,所以代码不是最有效率的,其中pushad,pushfd,popfd,popad也可以不要的,所以引用时,如果不是特别要求,可以去掉...
procedure PackMove(ibase,idest,isize:Cardinal);stdcall;assembler;
asm
pushad
pushfd
mov eax,isize
mov ebx,ibase
mov ecx,idest
@move:
mov dl, Byte [ebx]
mov Byte[ecx],dl
dec eax
inc ebx
inc ecx
cmp eax,0
jnz @move
popfd
popad
end;
function MygetProcAddress(iMoudle:Cardinal;sProcName:LPSTR):Cardinal;stdcall;assembler;
var
imDosHeaders:TImageDosHeader;
imNtheaders:TImageNtHeaders;
imExport:TImageExportDirectory;
i:Cardinal;
isize:Cardinal;
ipoint:Cardinal;
iNtheader:Cardinal;
iNOorName:Cardinal;
iNameIndex:Cardinal;
Comsign:Byte;
a,b:byte;
begin
isize:=SizeOf(imDosHeaders);
ipoint:= Cardinal(@imDosHeaders) ;
PackMove(imoudle,ipoint,isize);
ipoint :=imoudle + imDosHeaders._lfanew;
isize := SizeOf(imNtheaders);
iNtheader := Cardinal(@imNtheaders) ;
PackMove(ipoint,iNtheader,isize);
if imNtheaders.OptionalHeader.DataDirectory[0].VirtualAddress = 0 then
begin
Result := 0;
exit;
end;
ipoint :=imoudle + imNtheaders.OptionalHeader.DataDirectory[0].VirtualAddress;
isize := SizeOf(imExport);
iNtheader := Cardinal(@imExport) ;
PackMove(ipoint,iNtheader,isize);
asm
// push eax
mov eax,sprocname
Cmp Sprocname,0FFFFh
jnc @TrueS
mov iNOorName,eax
jmp @ends
@Trues:
mov iNOorName,0
@Ends:
// pop eax
end;
if not (iNOorName =0 ) then
begin
iNOorName := iNOorName - imExport.Base;
iNOorName := (iNOorName * 4);
iNOorName :=iMoudle + iNOorName + Cardinal( imExport.AddressOfFunctions);
asm
// push eax
mov eax,iNOorName
mov eax,dword [eax]
mov iNOorName,eax
// pop eax
end;
iNOorName := iNOorName + iMoudle;
Result := iNOorName ;
Exit;
end;
for i := 0 to imExport.NumberOfNames -1 do
begin
iNOorName := (i * 4) + Cardinal(imExport.AddressOfNames);
asm
mov eax,iNOorName
add eax,imoudle
mov eax,dword [eax]
add eax,imoudle
mov iNOorName,eax
mov ecx,sprocname
dec eax
dec ecx
@nextChar:
inc eax
inc ecx
mov dl,Byte [eax]
cmp dl,0h
jz @outcom
cmp byte [ecx],0
jz @bad
cmp Byte [ecx],dl
jz @nextChar
jmp @bad
@outcom:
cmp byte [ecx],0
jz @LastCom
@bad:
mov Comsign,0
jmp @eed
@LastCom:
mov Comsign,1
@eed:
end;
if Comsign = 1 then Break;
end;
if Comsign = 0 then
begin
Result :=0;
Exit;
end;
i := i + 1;
iNameIndex := (i * 2)+ Cardinal(imExport.AddressOfNameOrdinals) + iMoudle;
asm
mov eax,inameindex
mov ebx,0
mov bx,word [eax]
mov inameindex,ebx
end;
iNOorName := iNameIndex;
iNOorName := iNOorName - imExport.Base;
iNOorName := (iNOorName * 4);
iNOorName :=iMoudle + iNOorName + Cardinal( imExport.AddressOfFunctions);
asm
// push eax
mov eax,iNOorName
mov eax,dword [eax]
mov iNOorName,eax
// pop eax
end;
iNOorName := iNOorName + iMoudle;
Result := iNOorName ;
end;
代码由于壳的某些原因和我的菜鸟级别因素,有些地方很乱,有什么错误的地方还请大家提出...
kongfoo的替换代码:
procedure PackMove(ibase,idest,isize:Cardinal);stdcall;assembler;
asm
pushad
pushfd
mov ecx,isize
mov esi,ibase
mov edi,idest
rep movsb
popfd
popad
end;
///////////////////////////////////////
其中有很多地方代码可以简便的地方没有简便是因为能力不足,还有是因为考虑了在壳中的一些因素,所以代码不是最有效率的,其中pushad,pushfd,popfd,popad也可以不要的,所以引用时,如果不是特别要求,可以去掉...
procedure PackMove(ibase,idest,isize:Cardinal);stdcall;assembler;
asm
pushad
pushfd
mov eax,isize
mov ebx,ibase
mov ecx,idest
@move:
mov dl, Byte [ebx]
mov Byte[ecx],dl
dec eax
inc ebx
inc ecx
cmp eax,0
jnz @move
popfd
popad
end;
function MygetProcAddress(iMoudle:Cardinal;sProcName:LPSTR):Cardinal;stdcall;assembler;
var
imDosHeaders:TImageDosHeader;
imNtheaders:TImageNtHeaders;
imExport:TImageExportDirectory;
i:Cardinal;
isize:Cardinal;
ipoint:Cardinal;
iNtheader:Cardinal;
iNOorName:Cardinal;
iNameIndex:Cardinal;
Comsign:Byte;
a,b:byte;
begin
isize:=SizeOf(imDosHeaders);
ipoint:= Cardinal(@imDosHeaders) ;
PackMove(imoudle,ipoint,isize);
ipoint :=imoudle + imDosHeaders._lfanew;
isize := SizeOf(imNtheaders);
iNtheader := Cardinal(@imNtheaders) ;
PackMove(ipoint,iNtheader,isize);
if imNtheaders.OptionalHeader.DataDirectory[0].VirtualAddress = 0 then
begin
Result := 0;
exit;
end;
ipoint :=imoudle + imNtheaders.OptionalHeader.DataDirectory[0].VirtualAddress;
isize := SizeOf(imExport);
iNtheader := Cardinal(@imExport) ;
PackMove(ipoint,iNtheader,isize);
asm
// push eax
mov eax,sprocname
Cmp Sprocname,0FFFFh
jnc @TrueS
mov iNOorName,eax
jmp @ends
@Trues:
mov iNOorName,0
@Ends:
// pop eax
end;
if not (iNOorName =0 ) then
begin
iNOorName := iNOorName - imExport.Base;
iNOorName := (iNOorName * 4);
iNOorName :=iMoudle + iNOorName + Cardinal( imExport.AddressOfFunctions);
asm
// push eax
mov eax,iNOorName
mov eax,dword [eax]
mov iNOorName,eax
// pop eax
end;
iNOorName := iNOorName + iMoudle;
Result := iNOorName ;
Exit;
end;
for i := 0 to imExport.NumberOfNames -1 do
begin
iNOorName := (i * 4) + Cardinal(imExport.AddressOfNames);
asm
mov eax,iNOorName
add eax,imoudle
mov eax,dword [eax]
add eax,imoudle
mov iNOorName,eax
mov ecx,sprocname
dec eax
dec ecx
@nextChar:
inc eax
inc ecx
mov dl,Byte [eax]
cmp dl,0h
jz @outcom
cmp byte [ecx],0
jz @bad
cmp Byte [ecx],dl
jz @nextChar
jmp @bad
@outcom:
cmp byte [ecx],0
jz @LastCom
@bad:
mov Comsign,0
jmp @eed
@LastCom:
mov Comsign,1
@eed:
end;
if Comsign = 1 then Break;
end;
if Comsign = 0 then
begin
Result :=0;
Exit;
end;
i := i + 1;
iNameIndex := (i * 2)+ Cardinal(imExport.AddressOfNameOrdinals) + iMoudle;
asm
mov eax,inameindex
mov ebx,0
mov bx,word [eax]
mov inameindex,ebx
end;
iNOorName := iNameIndex;
iNOorName := iNOorName - imExport.Base;
iNOorName := (iNOorName * 4);
iNOorName :=iMoudle + iNOorName + Cardinal( imExport.AddressOfFunctions);
asm
// push eax
mov eax,iNOorName
mov eax,dword [eax]
mov iNOorName,eax
// pop eax
end;
iNOorName := iNOorName + iMoudle;
Result := iNOorName ;
end;
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!
赞赏
他的文章
看原图
赞赏
雪币:
留言: