strcpy(szBuf, “stricmp(“1”,“2”);”);
这里的szBuf只是保存了字符串值而已,并不是可执行的代码,从代码到可执行的二进制指令需要编译,亦或者通过解释执行.需要调用C编译器或解释器.
若要直接执行Buffer里面的代码必须是二进制指令.
这里提供一种可直接执行题主给的stricmp的例子:
x86
char szBuf[] = “\x8b\x44\x24\x04\x8b\x5c\x24\x08\x0f\xbe\x08\x85\xc9\x74\x0f\x0f\xbe\x13\x85\xd2\x74\x08\x3b\xca\x75\x04\x40\x43\xeb\xea\x0f\xbe\x08\x0f\xbe\x13\x2b\xca\x8b\xc1\xc3”;
调用:
int(_stricmp)(char[], char[]) = (int()(char[], char[]))&szBuf;printf("%d",_stricmp(“str1”, “str2”));
前提是szBuf的内存为可执行,Windows下使用VirtualProtectEx修改,Linux下使用mmap修改该内存位置为可执行.
Windows下的例子:
#include <stdio.h>#include <Windows.h>char szBuf[] = “\x8b\x44\x24\x04\x8b\x5c\x24\x08\x0f\xbe\x08\x85\xc9\x74\x0f\x0f\xbe\x13\x85\xd2\x74\x08\x3b\xca\x75\x04\x40\x43\xeb\xea\x0f\xbe\x08\x0f\xbe\x13\x2b\xca\x8b\xc1\xc3”;
int main(){
int(_stricmp)(char[], char[]) = (int()(char[], char[]))&szBuf;
DWORD OldProtect;
VirtualProtectEx(GetCurrentProcess(), \
&szBuf, sizeof(szBuf), \
PAGE_EXECUTE_READWRITE, \
&OldProtect);
printf("%d",_stricmp("str1", "str2"));
VirtualProtectEx(GetCurrentProcess(), \
&szBuf, sizeof(szBuf), \
OldProtect, \
NULL);
return 0;}
szBuf里的代码对应的汇编如下:
mov eax, [esp+0x04]
mov ebx, [esp+0x08]
__loop :
movsx ecx, byte ptr[eax]
test ecx, ecx
je __exit
movsx edx, byte ptr[ebx]
test edx, edx
je __exit
cmp ecx, edx
jne __exit
inc eax
inc ebx
jmp __loop
__exit :
movsx ecx, byte ptr [eax]
movsx edx, byte ptr [ebx]
sub ecx, edx
mov eax, ecx
ret
not about
https://www.jianshu.com/p/511fcb035fcb