Created
November 6, 2011 06:06
-
-
Save mimura1133/1342542 to your computer and use it in GitHub Desktop.
三平方の定理 for Mac
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <mach/mach.h> | |
#include <stdio.h> | |
/************************************************ | |
とりあえず、中のデータは16進で記録してあって、 | |
リトルエンディアンですから、 | |
0x01020304 は、メモリ上に 0x04,0x03,0x02,0x01 の順で格納されます。 | |
要は逆順です。 | |
また、命令のオーダーについては、必ずしも何バイトということはありませんが、 | |
最長命令は 32bit アーキテクチャですんで 4 バイト ( 32 bit / 8 = 4 byte. ) | |
見やすくするために、2バイトや1バイト命令に関しては、 | |
0x90 (NOP) [NOP = なにもしない] を挿入して、アライメント(区切り)を合わせてあります。 | |
まー、適当に見てもらえれば。 | |
************************************************/ | |
int main(){ | |
unsigned long d; | |
int a = 0,b = 0,c = 0; | |
unsigned long code[32] = {0}; | |
printf("vm_protect : %s\n\n",vm_protect(mach_task_self(),(vm_address_t)code,32 * sizeof(long),FALSE,VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE) == KERN_SUCCESS ? "[OK]" : "[FALSE]"); | |
printf("INPUT [a,b,c] : "); | |
scanf("%d,%d,%d",&a,&b,&c); | |
code[0] = 0x0424448B; // MOV EAX,DWORD PTR SS:[ESP+4] | |
code[1] = 0x08244C8B; // MOV ECX,DWORD PTR SS:[ESP+8] | |
code[2] = 0x9090C13B; // CMP EAX,ECX | |
code[3] = 0x9090047E; // JLE SHORT ; EIP+4 | |
code[4] = 0x9090C88B; // MOV ECX,EAX | |
code[5] = 0x0C24548B; // MOV EDX,DWORD PTR SS:[ESP+C] | |
code[6] = 0x9090CA3B; // CMP ECX,EDX | |
code[7] = 0x9090147E; // JLE SHORT ; EIP+C | |
code[8] = 0x90909056; // PUSH ESI | |
code[9] = 0x9090F28B; // MOV ESI,EDX | |
code[10] = 0x9090D18B; // MOV EDX,ECX | |
code[11] = 0x9090CE8B; // MOV ECX,ESI | |
code[12] = 0x9090905E; // POP ESI | |
code[13] = 0x90C0AF0F; // IMUL EAX,EAX | |
code[14] = 0x90C9AF0F; // IMUL ECX,ECX | |
code[15] = 0x90D2AF0F; // IMUL EDX,EDX | |
code[16] = 0x9090C103; // ADD EAX,ECX | |
code[17] = 0x9090C22B; // SUB EAX,EDX | |
code[18] = 0x9090D8F7; // NEG EAX | |
code[19] = 0x9090C01B; // SBB EAX,EAX | |
code[20] = 0x90909040; // INC EAX | |
code[21] = 0x909090C3; // RETN | |
printf("\nRETURN : %s\n",((int(*)(int,int,int))code)(a,b,c) == 0 ? "[FALSE]" : "[OK]"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment