Search

memcpy()

[- Disclaimer -] 아래 내용은 정보보안 공부 목적으로 작성된 것이나, 이를 토대로 허가되지 않은 대상에 실습을 진행할 경우 해킹 시도로 간주하여 법적 처벌을 받을 수 있음을 알려 드립니다.
memcpy()
✦ <memory.h> 혹은 <string.h>
✦ Destination 주소에 지정한 크기만큼 지정한 주소의 값 복사
✦ Parameter1에 전달되는 Argument1
✧ Destination 주소
✦ Parameter2에 전달되는 Argument2
✧ Source 주소
✦ Parameter3에 전달되는 Argument3
✧ Byte 단위 범위
✦ Return 값
✧ 없음
✦ BOF 취약 함수
✧ 문자열 길이 미검증
→ 할당된 공간을 넘어서도 문자열 길이를 검증하지 않아 계속 Read/Write가 가능하며 이 경우 Error가 날 수도 안 날 수도 있음
void *memcpy(void *dest, const void *src, size_t count);
Plain Text
복사
Ex)
#include <stdio.h> #include <string.h> int main(void) { char *str = "hello world"; char arr[32] = {'\0'}; memcpy(arr, str, 10); arr[10] = '\0'; printf("%s", arr); return 0; }
Plain Text
복사
hello worl -------------------------------- Process exited after 1.227 seconds with return value 0 계속하려면 아무 키나 누르십시오 . . .
Plain Text
복사
#include <stdio.h> #include <string.h> int main(void) { int a = 23; int b = 0; memcpy(&b, &a, sizeof(int) ); printf("%d", b); return 0; }
Plain Text
복사
23 -------------------------------- Process exited after 0.1388 seconds with return value 0 계속하려면 아무 키나 누르십시오 . . .
Plain Text
복사
#include <stdio.h> void *memcpy(void *destination, const void *source, size_t num); int main(void) { int a = 0x123456; int b = 0; memcpy(&b, &a, 2); printf("%d", b); return 0; } void *memcpy(void *destination, const void *source, size_t num) { size_t i = 0; for(i = 0; i < num; i++) ((unsigned char*)destination)[i] = ((unsigned char*)source)[i]; return destination; }
Plain Text
복사
13398 -------------------------------- Process exited after 0.258 seconds with return value 0 계속하려면 아무 키나 누르십시오 . . .
Plain Text
복사