常見軟體安全性漏洞範例代碼

來源:互聯網
上載者:User

標籤:style   blog   io   ar   color   os   sp   for   on   

緩衝區溢位:

 1 Example1.1: 2         ... 3     char buf[BUFSIZE];  4     gets(buf);//user control 5     ... 6  7 Example1.2(c++): 8     ... 9     char buf[BUFSIZE]; 10     cin >> (buf);//user control11     ...12 13 Example2:14     ...15     char buf[64], in[MAX_SIZE];16     printf("Enter buffer contents:\n");17     read(0, in, MAX_SIZE-1);18     printf("Bytes to copy:\n");19     scanf("%d", &bytes);//user control20     memcpy(buf, in, bytes);21     ...22     char *lccopy(const char *str) {23         char buf[BUFSIZE];24         char *p;25     26         strcpy(buf, str);27         for (p = buf; *p; p++) {28              if (isupper(*p)) {29                 *p = tolower(*p);30             }31         }    32         return strdup(buf);33     }34 35 Example4:36     if (!(png_ptr->mode & PNG_HAVE_PLTE)) {37         /* Should be an error, but we can cope with it */38         png_warning(png_ptr, "Missing PLTE before tRNS");39     }40     else if (length > (png_uint_32)png_ptr->num_palette) {41         png_warning(png_ptr, "Incorrect tRNS chunk length");42         png_crc_finish(png_ptr, length);43         return;44     }45     ...46     png_crc_read(png_ptr, readbuf, (png_size_t)length);47 48 Example5:49     void getUserInfo(char *username, struct _USER_INFO_2 info){50         WCHAR unicodeUser[UNLEN+1];51         MultiByteToWideChar(CP_ACP, 0, username, -1,52                             unicodeUser, sizeof(unicodeUser));53     NetUserGetInfo(NULL, unicodeUser, 2, (LPBYTE *)&info);54     }

格式化字串:

 1 Example1: 2     int main(int argc, char **argv){ 3         char buf[128]; 4         ... 5         snprintf(buf,128,argv[1]); 6     } 7  8 Example2: 9 printf("%d %d %1$d %1$d\n", 5, 9);10 11 Example3:12     ...13     syslog(LOG_ERR, cmdBuf);14     ...15 16 Example4:17 #include <stdio.h>18 19 void printWrapper(char *string) {   20   printf(string);21 }22 23 int main(int argc, char **argv) {   24   char buf[5012];    25   memcpy(buf, argv[1], 5012);    26   printWrapper(argv[1]);    27   return (0);28 }

整數溢出:

Example1:short int bytesRec = 0;char buf[SOMEBIGNUM];while(bytesRec < MAXGET) {  bytesRec += getFromInput(buf+bytesRec);}Example2:    nresp = packet_get_int();    if (nresp > 0) {     response = xmalloc(nresp*sizeof(char*));     for (i = 0; i < nresp; i++)      response[i] = packet_get_string(NULL);    }Example3:     char* processNext(char* strm) {     char buf[512];     short len = *(short*) strm;     strm += sizeof(len);     if (len <= 512) {      memcpy(buf, strm, len);      process(buf);      return strm + len;     } else {      return -1;     }    }Example4:[email protected] ~/labs/integer $ cat add.c#include <stdio.h>#include <limits.h>int main(void){ int a;//  a=2147483647; a=INT_MAX; printf("int a (INT_MAX) = %d (0x%x), int a (INT_MAX) + 1 = %d (0x%x)\n", a,a,a+1,a+1); return 0;}[email protected] ~/labs/integer $ ./addint a (INT_MAX) = 2147483647 (0x7fffffff), int a (INT_MAX) + 1 = -2147483648 (0x80000000)Example5:[email protected] ~/labs/integer $ cat multiplication.c#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>int main(int argc, char **argv){ int i, j, z=0x00000001; char *tab; if(argc<2) _exit(1); i=atoi(argv[1]); if(i>0) {   tab = malloc(i * sizeof(char *));   if(tab == NULL) _exit(2); } for(j=0; j<i; j++)   tab[j]=z++; for(j=0; j<i; j++)   printf("tab[j]=0x%x\n", tab[j]); return 0;}[email protected] ~/labs/integer $ ./multiplication 1073741824Segmentation fault

 

常見軟體安全性漏洞範例代碼

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.