[- Disclaimer -]
아래 내용은 정보보안 공부 목적으로 작성된 것이나, 이를 토대로 허가되지 않은 대상에 실습을 진행할 경우 해킹 시도로 간주하여 법적 처벌을 받을 수 있음을 알려 드립니다.
strcpy()
✦ <string.h>
✦ NULL String을 만날 때까지 복사
✦ Parameter1에 전달되는 Argument1
✧ Destination 문자열 주소
✦ Parameter2에 전달되는 Argument2
✧ Source 문자열 주소
✦ Return 값
✧ 성공
→ Destination 문자열
✧ 실패
→ return 값 없고 Segmentation Fault Error 발생
✦ BOF 취약 함수
✧ 문자열 길이 미검증
→ 할당된 공간을 넘어서도 문자열 길이를 검증하지 않아 계속 복사/덮어쓰기가 가능하며 이 경우 Error가 날 수도 안 날 수도 있음
char *strcpy(char *strDestination, const char *strSource);
Plain Text
복사
Ex)
#include <stdio.h>
#include <string.h>
int main(void) {
char buf[10] = {0};
char* string = "hello";
strcpy(buf, string);
printf("%s", buf);
}
Plain Text
복사
hello
--------------------------------
Process exited after 0.4629 seconds with return value 5
계속하려면 아무 키나 누르십시오 . . .
Plain Text
복사
strcpy() 기능 구현 - Ex)
#include <stdio.h>
char *self_strcpy(char *destination, const char *source);
int main(void) {
char buf[10] = {0};
char *string = "hello";
self_strcpy(buf, string);
printf("%s", buf);
}
char *self_strcpy(char *destination, const char *source) {
int i = 0;
while(source[i] != '\0') {
destination[i] = source[i];
++i;
}
destination[i] = '\0';
return destination;
}
Plain Text
복사
hello
--------------------------------
Process exited after 0.1589 seconds with return value 5
계속하려면 아무 키나 누르십시오 . . .
Plain Text
복사



