C/C++安全编码

C/C++安全编码

字符串

  1. 字符串以第一个空字符(0x00)作为结束的连续字符序列组成

    1 2 3 4 5 6
    h e l l o \0
  2. 当函数中传入数组指针时无法使用sizeof计算得到数组长度,此时sizeof计算的是指针的长度

    #include<cstdio>
    #include<cstdlib>
    
    void f(int a[]) {
    	if (a != nullptr) {
    		printf("%d\n", sizeof(a) / sizeof(a[0]));
    	}
    }
    
    int main() {
    	int a[10] = {};
    	printf("%d\n", sizeof(a) / sizeof(a[0]));  //10
    	f(a); //1
    	
    	system("pause");
    	return 0;
    }
    
  3. C语言使用setlocale()函数设置字符编码

无界字符串复制

  1. 发生于从源数据复制数据到一个定长的字符串数组时

  2. 无界字符串复制可能发生在以下几种情况

    • 从标准输出读取数据到一个定长缓存区中,如main函数中的argv参数
    • 复制和连接字符串
  3. 相关的函数有gets(),strlen(),strcpy(),strcat(),sprintf(),strcmp()这些函数均有对应的安全函数

    char *gets(char *dest) {
    	int c = getchar();
    	char *p = dest;
    	while (c!=EOF && c!='\n')
    	{
    		*p++ = c;
    		c = getchar();
    	}
    	*p = '\0';
    	return dest;
    }
    //由于事先不知道dest缓冲区长度,因此存在越界问题
    
    int main(int argc,int *argv[]){}
    //argc表示参数个数,agrv是参数数组的首地址,任何条件下argv[argc]==null成立
    
  4. 解决无界字符串复制的方法

    • 设置足够长的缓冲区(治标不治本)

    • 使用循环,存入一个字符串检测一次有无越界

    • C++在可以设置域宽

      #include<iostream>
      
      int main(void){
          char buf[1024];
          std::cin.width(1024);
          std::cin >> buf;
          std::cout<<buf<<endl;
          
          return 0
      }
      

差一错误

  1. 差一错误是指当忽略掉\0结束符时从而计算字符串长度错误

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    
    int main() {
    	char s1[] = "012345678";
    	char s2[] = "0123456789";
    	char *dest;
    	int i;
    
    	strcpy_s(s1, sizeof(s2), s2); //s1已经越界
    	dest = (char *)malloc(strlen(s1));//strlen(s1)==strlen(s2)==10
    	for (i = 1; i < 11; i++) dest[i] = s1[i];//越界,0开始循环
    	dest[i] = '\0';   //越界
    	printf("dest=%s", dest);
    
    	system("pause");
    	return 0;
    }
    

空结尾错误

  1. 字符串没有正确的以空字符结尾,如果一个程序没有以空字符结尾,程序可能会被欺骗,导致在数组边界之外读取或者写入数据

    size_t i;
    char ntbs[16]
    
    for(i=0;i<sizeof(ntbs);++i){
      if(ntbs=='\0')break;
    }
    

字符串截断错误

  1. 当目标字符数组的长度不足以容纳一个字符串时,就会发生字符串截断。截断通常读取或者复制用户输入时,字符串截断会丢失数据,有时也会造成漏洞

字符串漏洞的利用

posted @ 2020-10-12 22:27  光头大魔王  阅读(162)  评论(0)    收藏  举报