1 / 6

11/2 실습

11/2 실습. malloc ( ) 과 free() realloc () 과 _expand(); structure 와 union packed 와 padding Buffer overflow. Code example # 1 : Sizeof (A). // 소스 전체에 적용할 때는 # pragma pack(1) typedef struct __ attribute__((packed)) { int i; char c; } A; typedef struct { int i; char c;

cher
Download Presentation

11/2 실습

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 11/2 실습

  2. malloc( ) 과 free() • realloc()과 _expand(); • structure 와 union • packed 와 padding • Buffer overflow

  3. Codeexample #1 : Sizeof(A) //소스 전체에 적용할 때는 #pragmapack(1) typedefstruct__attribute__((packed)) { inti; char c; } A; typedefstruct { inti; char c; } B; int main(intargc, char *args[]) { A a; B b; printf("%d \n", sizeof(a)); printf("%d \n", sizeof(b)); } typedefstructure{ inti; char c; } A; visual studio에서 옵션보기 : 프로젝트속성 C/C++  코드생성  구조체멤버맞춤 기타 경고수준 설정하여 컴파일하기

  4. Codeexample #2 unsigned float2bit(float f) { arg.f = f; printf("arg.u : %x \n",arg.u); printf("arg.f : %f \n",arg.f); returnarg.u; } int main() { //4.0의 floating point format = 0x40800000 unsigned i= 0x40800000; float f= 4.0; printf(" %f \n", bit2float(i)); printf(" %x \n",float2bit(f)); } #include<stdio.h> typedefunion { float f; unsigned u; } bit_float_t; bit_float_targ; float bit2float(unsigned u) { arg.u = u; printf("arg.u : %x \n",arg.u); printf("arg.f : %f \n",arg.f); returnarg.f; }

  5. Codeexample #3 : buffer overflow 리눅스와 Visual 환경에서 모두 테스트해 보도록 하세요 Visual 환경에서는 메모리도 함께 보세요. 내용을 확인한 사람은, intmy_gets(char *str, int n);를 만듭니다. 기능: str사이즈만큼만 입력받는다. 사이즈가 초과하면 -1을 return한다. #include<stdio.h> int main() { char test[4]="1"; charbuf[4]="0"; /* Way too small! */ charstr[4]=“”; printf(" input string \n"); scanf("%s",str); printf(" str: %s",str); fflush(stdin); //했을 때와 안했을 때의 차이 확인하세요. printf("Type a string: "); gets(buf); //입력값 puts(buf); //결과값 puts(test); //확인하세요 }

  6. Codeexample #4 : buffer overflow #include <stdio.h> void fn1(char *str){ char local[4]; printf(" == this is fn1 %s\n",str); strcpy(local, str); printf(" == strcpy to 'local : %s\n",local); } void fn2() { printf(" == this is fn2 \n"); printf(" == if you see this, stack overflow has been executed \n"); } int main(intargc, char *args[]) { printf("address of fn1 : 0x%x \n",fn1); printf("address of fn2 : 0x%x \n",fn2); printf("address of main : 0x%x \n",main); fn1(args[1]); }

More Related