cgy12306
[pwnable.kr] memcpy 본문
[memcpy]
// compiled with : gcc -o memcpy memcpy.c -m32 -lm
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/mman.h>
#include <math.h>
unsigned long long rdtsc(){
asm("rdtsc");
}
char* slow_memcpy(char* dest, const char* src, size_t len){
int i;
for (i=0; i<len; i++) {
dest[i] = src[i];
}
return dest;
}
char* fast_memcpy(char* dest, const char* src, size_t len){
size_t i;
// 64-byte block fast copy
if(len >= 64){
i = len / 64;
len &= (64-1);
while(i-- > 0){
__asm__ __volatile__ (
"movdqa (%0), %%xmm0\n"
"movdqa 16(%0), %%xmm1\n"
"movdqa 32(%0), %%xmm2\n"
"movdqa 48(%0), %%xmm3\n"
"movntps %%xmm0, (%1)\n"
"movntps %%xmm1, 16(%1)\n"
"movntps %%xmm2, 32(%1)\n"
"movntps %%xmm3, 48(%1)\n"
::"r"(src),"r"(dest):"memory");
dest += 64;
src += 64;
}
}
// byte-to-byte slow copy
if(len) slow_memcpy(dest, src, len);
return dest;
}
int main(void){
setvbuf(stdout, 0, _IONBF, 0);
setvbuf(stdin, 0, _IOLBF, 0);
printf("Hey, I have a boring assignment for CS class.. :(\n");
printf("The assignment is simple.\n");
printf("-----------------------------------------------------\n");
printf("- What is the best implementation of memcpy? -\n");
printf("- 1. implement your own slow/fast version of memcpy -\n");
printf("- 2. compare them with various size of data -\n");
printf("- 3. conclude your experiment and submit report -\n");
printf("-----------------------------------------------------\n");
printf("This time, just help me out with my experiment and get flag\n");
printf("No fancy hacking, I promise :D\n");
unsigned long long t1, t2;
int e;
char* src;
char* dest;
unsigned int low, high;
unsigned int size;
// allocate memory
char* cache1 = mmap(0, 0x4000, 7, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
char* cache2 = mmap(0, 0x4000, 7, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
src = mmap(0, 0x2000, 7, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
size_t sizes[10];
int i=0;
// setup experiment parameters
for(e=4; e<14; e++){ // 2^13 = 8K
low = pow(2,e-1);
high = pow(2,e);
printf("specify the memcpy amount between %d ~ %d : ", low, high);
scanf("%d", &size);
if( size < low || size > high ){
printf("don't mess with the experiment.\n");
exit(0);
}
sizes[i++] = size;
}
sleep(1);
printf("ok, lets run the experiment with your configuration\n");
sleep(1);
// run experiment
for(i=0; i<10; i++){
size = sizes[i];
printf("experiment %d : memcpy with buffer size %d\n", i+1, size);
dest = malloc( size );
memcpy(cache1, cache2, 0x4000); // to eliminate cache effect
t1 = rdtsc();
slow_memcpy(dest, src, size); // byte-to-byte memcpy
t2 = rdtsc();
printf("ellapsed CPU cycles for slow_memcpy : %llu\n", t2-t1);
memcpy(cache1, cache2, 0x4000); // to eliminate cache effect
t1 = rdtsc();
fast_memcpy(dest, src, size); // block-to-block memcpy
t2 = rdtsc();
printf("ellapsed CPU cycles for fast_memcpy : %llu\n", t2-t1);
printf("\n");
}
printf("thanks for helping my experiment!\n");
printf("flag : ----- erased in this source code -----\n");
return 0;
}
소스 코드를 설명하자면 값을 10번 입력을 받는데 이 값은 주어진 범위를 지켜야 하고, 그 값이 64보다 작으면 slow_memcpy를 호출하고, 64이상이면 fast_memcpy 함수가 호출이 된다. 그리고 rdtsc를 호출해서 경과 시간을 알려준다.
그리고 해당 코드가 모두 정상적으로 실행되면 flag를 보여준다.
readme 파일을 읽어보자.
9022 port에 붙어서 실행을 하라고 한다.
실행해보면 5번째에서 fast_memcpy를 호출했을 때 끝나버리고만다.
그러면 fast_memcpy 부분에서 이상이 생긴다는 말이다.
c 파일 맨위에 컴파일을 하라고 시킨다.
// compiled with : gcc -o memcpy memcpy.c -m32 -lm
컴파일해서 다시 돌려보자.
5번째에서 Segmentation fault가 뜬다.
디버거로 들어가서 확인해보자.
fast_memcpy + 52 지점에서 명령어를 수행할 때 프로그램이 터진다.
movdqa와 movntps 명령어를 알아봤다.
Description
Moves a double quadword from the source operand (second operand) to the destination operand (first operand). This instruction can be used to move a double quadword to and from an XMM register and a 128-bit memory location, or between two XMM registers. When the source or destination operand is a memory operand, the operand must be aligned on a 16-byte boundary or a general-protection exception (#GP) will be generated.
To move a double quadword to or from unaligned memory locations, use the MOVDQU instruction.
While executing in 16-bit addressing mode, a linear address for a 128-bit data access that overlaps the end of a 16-bit segment is not allowed and is defined as reserved behavior. A specific processor implementation may or may not generate a #GP exception in this situation, and the address that spans the end of the segment may or may not wrap around to the beginning of the segment.
16byte 값을 옮길 때 사용하는데 16byte만큼 정렬을 해줘야 한다고 한다.
우리가 직접 컴파일 할 수 있기 때문에 값을 직접 확인하면서 해보자.
char* fast_memcpy(char* dest, const char* src, size_t len){
size_t i;
// 64-byte block fast copy
printf("%p\n", dest);
if(len >= 64){
i = len / 64;
len &= (64-1);
while(i-- > 0){
__asm__ __volatile__ (
"movdqa (%0), %%xmm0\n"
"movdqa 16(%0), %%xmm1\n"
"movdqa 32(%0), %%xmm2\n"
"movdqa 48(%0), %%xmm3\n"
"movntps %%xmm0, (%1)\n"
"movntps %%xmm1, 16(%1)\n"
"movntps %%xmm2, 32(%1)\n"
"movntps %%xmm3, 48(%1)\n"
::"r"(src),"r"(dest):"memory");
dest += 64;
src += 64;
}
}
// byte-to-byte slow copy
if(len) slow_memcpy(dest, src, len);
return dest;
}
바이너리를 다시 실행해보자.
5번째에서 주소값이 0x84e84a8로 출력이 된다.
이 주소값은 16byte로 정렬이 되어 있지 않기 때문에 8이 남는 것을 볼 수 있다.(주소는 16진수)
128일때의 주소값은 64를 입력했을 때의 주소값과의 차이는 입력한 값 + 8만큼 차이가 난다.
그러면 64값이 아닌 64+8=72 값을 입력하자.
6번째로 넘어가는 것을 확인할 수 있다.
여기서도 메모리 정렬이 되어있지 않으니 이런식으로 계속하다면 flag를 출력해줄 것이다.
'Wargame > pwnable.kr' 카테고리의 다른 글
[pwnable.kr] horcruxes (0) | 2020.02.02 |
---|---|
[pwnable.kr] uaf (0) | 2020.02.02 |
[pwnable.kr] blukat (0) | 2020.01.27 |
[pwnable.kr] unlink (0) | 2020.01.27 |
[pwnable.kr] asm (0) | 2020.01.27 |