Advertisement
cyberpunk7925

Untitled

Dec 11th, 2022
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.05 KB | None | 0 0
  1. STR30-C: DO NOT TRY TO MODIFY STRING LITERAL.
  2.  
  3. VULNERABILITY:
  4. //Str30-c : Don’t try to modify string literal.
  5.  #include<stdio.h>
  6.  
  7. int main ()
  8. {
  9.     char *p = "hii iam good";
  10.     p [0] = 'U';
  11.     return 0;
  12. }
  13. MITIGATION: WE CAN STORE STRING AS AN ARRAY SO, IT IS POSSIBLE TO MODIFY STRING.
  14. #include <stdio.h>
  15. int main()
  16. {
  17. char name[20];
  18. printf("Enter name: ");
  19. scanf("%s", name);
  20. name[0] = 'U';
  21. printf("Your name is %s.", name);
  22. return 0;
  23. }
  24. STR36-C. Do not specify the bound of a character array initialized with a string literal.”
  25. VULNERABILITY:
  26. #include<stdio.h>
  27. int main()
  28. {
  29.     char a[5] = "this is good one";
  30.     printf("%s",a);
  31.     return 0 ;
  32.  
  33. }
  34. MITIGATION:
  35. #include <stdio.h>
  36.        int main()
  37.      {
  38.         char a[] = “iam good”;
  39.          gets(a);
  40.          puts(a);
  41.          return 0;
  42.     }
  43. STR31-C. Guarantee that storage for strings has sufficient space for character data and the null terminator
  44.  
  45.  
  46. VULNERABILITY:
  47. #include <stdio.h>
  48. int main() {
  49.     char s1[4], s2[2], i;
  50.     printf("Enter string s1: ");
  51.     fgets(s1, sizeof(s1), stdin);
  52.  
  53.     for (i = 0; s1[i] != '\0'; ++i) {
  54.         s2[i] = s1[i];
  55.     }
  56.     s2[i] = '\0';
  57.     printf("String s2: %s", s2);
  58.     return 0;
  59. }
  60. MITIGATION:
  61. #include <stdio.h>
  62. int main() {
  63.     char s1[5], s2[5], i;
  64.     printf("Enter string s1: ");
  65.     fgets(s1, sizeof(s1), stdin);
  66. for (i = 0; i <= 4; ++i) {
  67.         s2[i] = s1[i];
  68.     }
  69.     s2[i] = '\0';
  70.     printf("String s2: %s", s2);
  71. return 0;
  72. }
  73. //MSC34-C. Do not use deprecated or obsolescent functions
  74. VULNERABILITY:
  75. #include<stdio.h>
  76. int main()
  77. {
  78.  char s[10];
  79.     gets(s);
  80.     //fgets(s,sizeof(s),stdin);
  81.     printf("%s",s);
  82. }
  83.  
  84.  
  85. //MITIGATION:
  86. #include<stdio.h>
  87. int main()
  88. {
  89.  char s[10];
  90.     //gets(s);
  91.     fgets(s,sizeof(s),stdin);
  92.     printf("%s",s);
  93. }
  94.  
  95. MSC00-C. Compile cleanly at high warning levels
  96. VULNERABILITY:
  97. #include <stdio.h>
  98. #include<string.h>
  99. int main(){
  100.     size_t len;
  101.  char cstr[] = "harley";
  102.  signed char scstr[] = "signed harley";
  103.  unsigned char ucstr[] = "unsigned harley";
  104.  len = strlen(cstr);
  105.  len = strlen(scstr);
  106.  len = strlen(ucstr);
  107.     return 0;
  108. }
  109. //MSC00-C.
  110. // mitigation
  111. #include <stdio.h>
  112. #include<string.h>
  113. int main () {
  114. size_t len;
  115. char cstr[] = "char string";
  116. len = strlen(cstr);
  117. printf("%ld",len);
  118. return(0);
  119. }
  120. TO EXECUTE AND MITIGATE HEAPOVERFLOW
  121. VULNERABILITY:
  122. #include<stdio.h>
  123. int main()
  124. {
  125.     for (int i=0; i<10000000; i++)
  126.     {
  127.        // Allocating memory without freeing it
  128.        int *ptr = (int *)malloc(sizeof(int));
  129.        free(ptr);
  130.     }
  131. }
  132. MITIGATION:
  133. #include<stdio.h>
  134. int main()
  135. {   for (int i=0; i<10000000; i++)
  136.     { // Allocating memory without freeing it
  137.        int *ptr = (int *)malloc(sizeof(int));
  138.        free(ptr);
  139.     }
  140. }
  141. TO EXECUTE AND MITIGATE STACK OVERFLOW
  142. VULNERABILITY:
  143. #include<stdio.h>
  144. void calculate(int a) {
  145.    if (a== 0){
  146.        return ;  }
  147.    else  {
  148.        a = a + 1 ;
  149.        calculate(a); }
  150. }
  151. int main() {
  152.    int a = 1;
  153.    calculate(a);
  154. }
  155. MITIGATION:
  156. #include<stdio.h>
  157. int caluclate(int a) {
  158.    if (a == 0){
  159.        return ;
  160.        }
  161.    else  {
  162.        a = a - 1 ;
  163.        caluclate(a);
  164.        printf("%d",caluclate(a));
  165.        }
  166. }
  167. int main() {
  168.    int a = 1;
  169.    caluclate(a);
  170.    return 0;
  171. }
  172. EXECUTE AND MITIGATE STACKSMASHING
  173. VULNERABILITY:
  174. #include <stdio.h>
  175. #include <string.h>
  176. int main(void)
  177. {
  178.     char buff[20];
  179.     int pass = 0;
  180.     printf("\n Enter the password : \n");
  181.     gets(buff);
  182.     if(strcmp(buff, "ram4444"))
  183.     {
  184.         printf ("\n Wrong Password \n");
  185.     }
  186.     else
  187.     {
  188.         printf ("\n Correct Password \n");
  189.         pass = 1;
  190.     }
  191.     if(pass)
  192.     {
  193.         printf ("granted");
  194.     }
  195.     return 0;
  196. }
  197. //if we enter password length more than buffer size then we will get an error like stack smashing detected
  198. MEM09-C. Do not assume memory allocation functions initialize memory
  199. VULNERABILITY:
  200. #include <stdio.h>
  201. #include<stdlib.h>
  202. int main()
  203. {
  204.     int *arr1=malloc(5*sizeof(int*));
  205.     int sum = 0 ;
  206.     for(int i=0;i<5;i++)
  207.     {
  208.         sum = sum + *(arr1+i);
  209.     }
  210.     printf("sum = %d\n",sum);
  211.   return 0;
  212. }
  213. MITIGATION:
  214. Use calloc() function as it will allocate memory and initialize the values also.
  215.  
  216. #include <stdio.h>
  217. #include<stdlib.h>
  218. int main()
  219. {
  220.     int *arr1=calloc(5,sizeof(int*));
  221.     int sum = 0 ;
  222.     for(int i=0;i<5;i++)
  223.     {
  224.         sum = sum + *(arr1+i);
  225.     }
  226.     printf("sum = %d\n",sum);
  227.   return 0;
  228. }
  229. MEM32-C: Detect and handle memory allocation errors
  230. VULNERABILITY:
  231. #include<stdio.h>
  232. #include<stdlib.h>
  233. int main()
  234. {
  235.  int *arr1=calloc(-5,sizeof(int));
  236.  return 0;
  237. }
  238. mitigation:
  239. #include<stdio.h>
  240. #include<stdlib.h>
  241. int main()
  242. {
  243.  int *arr1=calloc(-5,sizeof(int));
  244.  if(arr1!=NULL)
  245.  {
  246.  printf("Memory is allocated\n");
  247.  printf("The array is: ");
  248.  for(int i=0;i<5;i++)
  249.  {
  250.  printf("%d ",*(arr1+i));
  251.  }
  252.  }
  253.  else
  254.  {
  255.  printf("Memory isn't allocated\n");
  256.  }
  257.  return 0;
  258. }
  259. EXP34-C. Do not dereference null pointers – Deference Null or Invalid pointers
  260. VULNERABILITY:
  261. #include<stdio.h>
  262. #include<stdlib.h>
  263. int main(int argc, char **argv){
  264.     int *ptr = (int*)malloc(sizeof(int));
  265.     ptr = NULL;
  266.     printf("%d",*ptr);
  267.     return 0 ;    
  268. }
  269. MITIGATION:
  270. #include<stdio.h>
  271. #include<stdlib.h>
  272. int main(int argc, char **argv){
  273.     int *ptr = (int*)malloc(sizeof(int));
  274.     if ( ptr != NULL )
  275.     {
  276.          *ptr = 5 ;
  277.           printf("%d",*ptr);
  278.     }
  279.     else
  280.     {
  281.         printf("memory is not created");
  282.     }
  283.     return 0 ;
  284.  
  285. }
  286. CWE-416: Use After Free
  287. #include <stdio.h>
  288. #include <unistd.h>
  289. int main()
  290. {
  291.     int *p = (int*)malloc(sizeof(int));
  292.     *p = 6 ;
  293.     free(p);
  294.     *p = 4;
  295.     printf("%d",*p);
  296.     return 0 ;
  297. }
  298. After deallocating the memory user is still able to modify the value in the memory as the pointer still stores the address of that memory, so after deallocating the memory we need to point pointer to NULL.
  299.  
  300.  
  301. #include <stdio.h>
  302. #include <unistd.h>
  303. int main(int argc, char **argv)
  304. {
  305. int *p = (int*)malloc(sizeof(int));
  306. *p = 6 ;
  307. free(p);
  308. p = NULL;
  309. *p = 4;
  310. return 0 ;
  311. }
  312. Cwe415-c : double free
  313. VULNERABILITY:
  314. #include<stdio.h>
  315. int main(){
  316.     int * p = (int *)malloc(sizeof(int));
  317.     free(p);
  318.     free(p);
  319.     return 0;
  320. }
  321. MITIGATION:
  322. #include<stdio.h>
  323. int main(){
  324.     int * p = (int *)malloc(sizeof(int));
  325.     free(p);
  326.     p = NULL;
  327.     free(p);
  328.     return 0;
  329. }
  330. CWE-401: Missing Release of Memory after Effective Lifetime
  331. VULNERABILITY:
  332. #include <stdlib.h>
  333. #include<stdio.h>
  334. int main()
  335. {
  336.  int *ptr = (int *) malloc(sizeof(int));
  337.   return 0 ;
  338. }
  339. MITIGATION:
  340. #include <stdlib.h>
  341. #include<stdio.h>
  342. int main(){
  343.  int *ptr = (int *) malloc(sizeof(int));
  344.  if ( ptr != NULL)
  345.  {
  346.      free(ptr);
  347.      printf("memory freed");}
  348.  return 0 ;
  349.  }
  350. “MEM04-C. Do not perform zero-length allocations,” provides additional guidance about zero-length allocations.
  351. VULNERABILITY:
  352. #include <stdio.h>
  353. #include <unistd.h>
  354. int main()
  355. {
  356.     int *p = (int*)malloc(sizeof(int));
  357.     int size ;
  358.     printf("enter size to reallocate:");
  359.     scanf("%d",size);
  360.     p = (int*)realloc(p,size);
  361.     printf("%d",*p);
  362. }
  363. MITIGATION:
  364. #include <stdio.h>
  365. #include<stdlib.h>
  366. int main()
  367. {
  368.     int *p = (int*)malloc(sizeof(int));
  369.     int size ;
  370.     printf("enter size to reallocate:");
  371.     scanf("%d",&size);
  372.     if (size == 0)
  373.     {
  374.         printf("re-enter the size :");
  375.         scanf("%d",&size);
  376.     }
  377.     else
  378.     {
  379.         p = (int*)realloc(p,size);
  380.         printf("%d",p[0]);
  381.     }
  382.     return 0 ;
  383. }
  384. TRUNCATION VULNERABILITY:
  385. // storing a large data type in a short data type leads to loss of data
  386. #include <stdio.h>
  387. #include<stdlib.h>
  388. #include<limits.h>
  389. int main()
  390. {
  391.     unsigned int *p = (int*)malloc(sizeof( unsigned int));
  392.     *p = UINT_MAX;
  393.     printf("UINT_MAX = %u\n",*p);
  394.     unsigned int *c = (int*)malloc(sizeof(unsigned int));
  395.     *c = UINT_MAX+1;
  396.     printf("UINT_MAX =  = %u\n",*c);
  397.     unsigned short d = *p + *c;
  398.     printf("value out unsigned integer: %u\n", *p +*c);
  399.     printf("value out unsigned short integer: %u", d);
  400.  
  401.    return 0;
  402. }
  403. COVERSION OF INTERGERS
  404. // CHANGE IN SIGN WHEN CONVERTING FROM UNSIGNED TO SIGNED OR SIGNED TO UNSIGNED leads to loss of sign(conversion).
  405. #include <stdio.h>
  406. #include<stdlib.h>
  407. #include<limits.h>
  408. int main()
  409. {
  410.     unsigned int *a = (int*)malloc(sizeof(unsigned int));
  411.     *a = UINT_MAX;
  412.     int *b = (int*)malloc(sizeof(int));
  413.     *b = *a;
  414.      printf("signed range: %d <= x <= %d \n", INT_MIN,INT_MAX);
  415.      printf("unsigned range: %u <= x <= %u\n ", 0,UINT_MAX);
  416.      printf("unsigned int value = %u\n",*a);
  417.      printf(" signed  int value = %d",*b);
  418.  
  419.    return 0;
  420. }
  421. MITIGATION:
  422. #include <stdio.h>
  423. #include <stdlib.h>
  424. #include<string.h>
  425. int main(){
  426. unsigned int size=-1;
  427. char *input_str="ksjdbfks";
  428. int *array;
  429. if (size < 3) {
  430. char *c_str = (char *)malloc(size);
  431. memcpy(c_str, input_str, 30);
  432. printf("%s",c_str);}
  433. else{
  434. printf("cant pass negetive values into malloc");
  435. }
  436. return(0);
  437. }
  438. To execute and implement formatted outputs.
  439. frpintf():
  440. CODE:
  441. #include<stdio.h>
  442. int main(int argc, char** argv)
  443. {
  444. printf(argv[1]);
  445. return0;
  446. }
  447. MITIGATION:
  448. #include<stdio.h>
  449. int main(int argc, char** argv)
  450. {
  451. printf("%s",argv[1]);
  452. return 0;
  453. }
  454. sprintf():
  455. CODE:
  456. #include<stdio.h>
  457. int main()
  458. {
  459. char buffer[50];
  460. int a = 10, b = 20, c;
  461. c = a + b;
  462. sprintf(buffer, "sum of two numbersssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
  463. is %d", c);
  464. printf("%s", buffer);
  465. return 0;
  466. }
  467. MITIGATION:
  468. #include<stdio.h>
  469. int main()
  470. {
  471. char buffer[50];
  472. int a = 10, b = 20, c;
  473. c = a + b;
  474. snprintf(buffer,25, "Sum of %d and %d is %d k", a, b, c);
  475. printf("%d", (int)sizeof(buffer));
  476. return 0;
  477. }
  478. fprintf():
  479. CODE:
  480. #include<stdio.h>
  481. int main()
  482. {
  483. int i, n=2;
  484. char str[50];
  485. FILE *fptr = fopen("sample.txt", "w");
  486. if (fptr == NULL)
  487. {
  488. printf("Could not open file");
  489. return 0;
  490. }
  491. for (i=0; i<n; i++)
  492. {
  493. puts("Enter a name");
  494. gets(str);
  495. fprintf(fptr,"%d.%s\n", i, str);
  496. }
  497. fclose(fptr);
  498. return 0;
  499. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement