Advertisement
cyberpunk7925

Untitled

Dec 11th, 2022
115
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.10 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. #include<string.h>
  77. int main()
  78. {
  79.     char a[8] ;
  80.     printf("enter name:");
  81.     gets(a);
  82.     printf("%d",a);
  83.  
  84.     return 0;
  85. }
  86.  
  87. MITIGATION:
  88. It is not possible to mention the size of the array in prior as we don’t know how much length the user will enter. We can use linked list.
  89. #include<stdio.h>
  90. #include<stdlib.h>
  91.  
  92. struct node
  93. {
  94.     char data;
  95.     struct node *next;
  96. };
  97.  
  98.  
  99. void addLast(struct node **head, char val)
  100. {
  101.     //create a new node
  102.     struct node *newNode = malloc(sizeof(struct node));
  103.     newNode->data = val;
  104.     newNode->next     = NULL;
  105.  
  106.     //if head is NULL, it is an empty list
  107.     if(*head == NULL)
  108.          *head = newNode;
  109.     //Otherwise, find the last node and add the newNode
  110.     else
  111.     {
  112.         struct node *lastNode = *head;
  113.  
  114.         //last node's next address will be NULL.
  115.         while(lastNode->next != NULL)
  116.         {
  117.             lastNode = lastNode->next;
  118.         }
  119.  
  120.         //add the newNode at the end of the linked list
  121.         lastNode->next = newNode;
  122.     }
  123.  
  124. }
  125.  
  126. void printList(struct node *head)
  127. {
  128.     struct node *temp = head;
  129.  
  130.     //iterate the entire linked list and print the data
  131.     while(temp != NULL)
  132.     {
  133.          printf("%c", temp->data);
  134.          temp = temp->next;
  135.     }
  136. }
  137.  
  138. int main()
  139. {
  140.      struct node *head = NULL;
  141.  
  142.      addLast(&head,'i');
  143.      addLast(&head,'a');
  144.      addLast(&head,'m');
  145.      addLast(&head,'g');
  146.      addLast(&head,'o');
  147.      addLast(&head,'o');
  148.      addLast(&head,'d');
  149.      printList(head);
  150.  
  151.      return 0;
  152. }
  153. MSC00-C. Compile cleanly at high warning levels
  154. VULNERABILITY:
  155. #include <stdio.h>
  156. #include<string.h>
  157. int main(){
  158.     size_t len;
  159.  char cstr[] = "harley";
  160.  signed char scstr[] = "signed harley";
  161.  unsigned char ucstr[] = "unsigned harley";
  162.  len = strlen(cstr);
  163.  len = strlen(scstr);
  164.  len = strlen(ucstr);
  165.     return 0;
  166. }
  167. TO EXECUTE AND MITIGATE HEAPOVERFLOW
  168. VULNERABILITY:
  169. #include<stdio.h>
  170. int main()
  171. {
  172.     for (int i=0; i<10000000; i++)
  173.     {
  174.        // Allocating memory without freeing it
  175.        int *ptr = (int *)malloc(sizeof(int));
  176.        free(ptr);
  177.     }
  178. }
  179. MITIGATION:
  180. #include<stdio.h>
  181. int main()
  182. {   for (int i=0; i<10000000; i++)
  183.     { // Allocating memory without freeing it
  184.        int *ptr = (int *)malloc(sizeof(int));
  185.        free(ptr);
  186.     }
  187. }
  188. TO EXECUTE AND MITIGATE STACK OVERFLOW
  189. VULNERABILITY:
  190. #include<stdio.h>
  191. void calculate(int a) {
  192.    if (a== 0){
  193.        return ;  }
  194.    else  {
  195.        a = a + 1 ;
  196.        calculate(a); }
  197. }
  198. int main() {
  199.    int a = 1;
  200.    calculate(a);
  201. }
  202. MITIGATION:
  203. #include<stdio.h>
  204. void calculate(int a) {
  205.    if (a== 0){
  206.        return ;  }
  207.    else  {
  208.        a = a - 1 ;
  209.        calculate(a); }
  210. }
  211. int main() {
  212.    int a = 1;
  213.    calculate(a);
  214. }
  215. EXECUTE AND MITIGATE STACKSMASHING
  216. VULNERABILITY:
  217. #include <stdio.h>
  218. #include <string.h>
  219. int main(void)
  220. {
  221.     char buff[20];
  222.     int pass = 0;
  223.     printf("\n Enter the password : \n");
  224.     gets(buff);
  225.     if(strcmp(buff, "ram4444"))
  226.     {
  227.         printf ("\n Wrong Password \n");
  228.     }
  229.     else
  230.     {
  231.         printf ("\n Correct Password \n");
  232.         pass = 1;
  233.     }
  234.     if(pass)
  235.     {
  236.         printf ("granted");
  237.     }
  238.     return 0;
  239. }
  240. //if we enter password length more than buffer size then we will get an error like stack smashing detected
  241. MEM09-C. Do not assume memory allocation functions initialize memory
  242. VULNERABILITY:
  243. #include <stdio.h>
  244. #include<stdlib.h>
  245. int main()
  246. {
  247.     int *arr1=malloc(5*sizeof(int*));
  248.     int sum = 0 ;
  249.     for(int i=0;i<5;i++)
  250.     {
  251.         sum = sum + *(arr1+i);
  252.     }
  253.     printf("sum = %d\n",sum);
  254.   return 0;
  255. }
  256. MITIGATION:
  257. Use calloc() function as it will allocate memory and initialize the values also.
  258.  
  259. #include <stdio.h>
  260. #include<stdlib.h>
  261. int main()
  262. {
  263.     int *arr1=calloc(5,sizeof(int*));
  264.     int sum = 0 ;
  265.     for(int i=0;i<5;i++)
  266.     {
  267.         sum = sum + *(arr1+i);
  268.     }
  269.     printf("sum = %d\n",sum);
  270.   return 0;
  271. }
  272. MEM32-C: Detect and handle memory allocation errors
  273. VULNERABILITY:
  274. #include<stdio.h>
  275. #include<stdlib.h>
  276. int main()
  277. {
  278.  int *arr1=calloc(-5,sizeof(int));
  279.  return 0;
  280. }
  281. mitigation:
  282. #include<stdio.h>
  283. #include<stdlib.h>
  284. int main()
  285. {
  286.  int *arr1=calloc(-5,sizeof(int));
  287.  if(arr1!=NULL)
  288.  {
  289.  printf("Memory is allocated\n");
  290.  printf("The array is: ");
  291.  for(int i=0;i<5;i++)
  292.  {
  293.  printf("%d ",*(arr1+i));
  294.  }
  295.  }
  296.  else
  297.  {
  298.  printf("Memory isn't allocated\n");
  299.  }
  300.  return 0;
  301. }
  302. EXP34-C. Do not dereference null pointers – Deference Null or Invalid pointers
  303. VULNERABILITY:
  304. #include<stdio.h>
  305. #include<stdlib.h>
  306. int main(int argc, char **argv){
  307.     int *ptr = (int*)malloc(sizeof(int));
  308.     ptr = NULL;
  309.     printf("%d",*ptr);
  310.     return 0 ;    
  311. }
  312. MITIGATION:
  313. #include<stdio.h>
  314. #include<stdlib.h>
  315. int main(int argc, char **argv){
  316.     int *ptr = (int*)malloc(sizeof(int));
  317.     if ( ptr != NULL )
  318.     {
  319.          *ptr = 5 ;
  320.           printf("%d",*ptr);
  321.     }
  322.     else
  323.     {
  324.         printf("memory is not created");
  325.     }
  326.     return 0 ;
  327.  
  328. }
  329. CWE-416: Use After Free
  330. #include <stdio.h>
  331. #include <unistd.h>
  332. int main()
  333. {
  334.     int *p = (int*)malloc(sizeof(int));
  335.     *p = 6 ;
  336.     free(p);
  337.     *p = 4;
  338.     printf("%d",*p);
  339.     return 0 ;
  340. }
  341. 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.
  342.  
  343.  
  344. #include <stdio.h>
  345. #include <unistd.h>
  346. int main(int argc, char **argv)
  347. {
  348. int *p = (int*)malloc(sizeof(int));
  349. *p = 6 ;
  350. free(p);
  351. p = NULL;
  352. *p = 4;
  353. return 0 ;
  354. }
  355. Cwe415-c : double free
  356. VULNERABILITY:
  357. #include<stdio.h>
  358. int main(){
  359.     int * p = (int *)malloc(sizeof(int));
  360.     free(p);
  361.     free(p);
  362.     return 0;
  363. }
  364. MITIGATION:
  365. #include<stdio.h>
  366. int main(){
  367.     int * p = (int *)malloc(sizeof(int));
  368.     free(p);
  369.     p = NULL;
  370.     free(p);
  371.     return 0;
  372. }
  373. CWE-401: Missing Release of Memory after Effective Lifetime
  374. VULNERABILITY:
  375. #include <stdlib.h>
  376. #include<stdio.h>
  377. int main()
  378. {
  379.  int *ptr = (int *) malloc(sizeof(int));
  380.   return 0 ;
  381. }
  382. MITIGATION:
  383. #include <stdlib.h>
  384. #include<stdio.h>
  385. int main(){
  386.  int *ptr = (int *) malloc(sizeof(int));
  387.  if ( ptr != NULL)
  388.  {
  389.      free(ptr);
  390.      printf("memory freed");}
  391.  return 0 ;
  392.  }
  393. “MEM04-C. Do not perform zero-length allocations,” provides additional guidance about zero-length allocations.
  394. VULNERABILITY:
  395. #include <stdio.h>
  396. #include <unistd.h>
  397. int main()
  398. {
  399.     int *p = (int*)malloc(sizeof(int));
  400.     int size ;
  401.     printf("enter size to reallocate:");
  402.     scanf("%d",size);
  403.     p = (int*)realloc(p,size);
  404.     printf("%d",*p);
  405. }
  406. MITIGATION:
  407. #include <stdio.h>
  408. #include<stdlib.h>
  409. int main()
  410. {
  411.     int *p = (int*)malloc(sizeof(int));
  412.     int size ;
  413.     printf("enter size to reallocate:");
  414.     scanf("%d",&size);
  415.     if (size == 0)
  416.     {
  417.         printf("re-enter the size :");
  418.         scanf("%d",&size);
  419.     }
  420.     else
  421.     {
  422.         p = (int*)realloc(p,size);
  423.         printf("%d",p[0]);
  424.     }
  425.     return 0 ;
  426. }
  427. TRUNCATION VULNERABILITY:
  428. // storing a large data type in a short data type leads to loss of data
  429. #include <stdio.h>
  430. #include<stdlib.h>
  431. #include<limits.h>
  432. int main()
  433. {
  434.     unsigned int *p = (int*)malloc(sizeof( unsigned int));
  435.     *p = UINT_MAX;
  436.     printf("UINT_MAX = %u\n",*p);
  437.     unsigned int *c = (int*)malloc(sizeof(unsigned int));
  438.     *c = UINT_MAX+1;
  439.     printf("UINT_MAX =  = %u\n",*c);
  440.     unsigned short d = *p + *c;
  441.     printf("value out unsigned integer: %u\n", *p +*c);
  442.     printf("value out unsigned short integer: %u", d);
  443.  
  444.    return 0;
  445. }
  446. COVERSION OF INTERGERS
  447. // CHANGE IN SIGN WHEN CONVERTING FROM UNSIGNED TO SIGNED OR SIGNED TO UNSIGNED leads to loss of sign(conversion).
  448. #include <stdio.h>
  449. #include<stdlib.h>
  450. #include<limits.h>
  451. int main()
  452. {
  453.     unsigned int *a = (int*)malloc(sizeof(unsigned int));
  454.     *a = UINT_MAX;
  455.     int *b = (int*)malloc(sizeof(int));
  456.     *b = *a;
  457.      printf("signed range: %d <= x <= %d \n", INT_MIN,INT_MAX);
  458.      printf("unsigned range: %u <= x <= %u\n ", 0,UINT_MAX);
  459.      printf("unsigned int value = %u\n",*a);
  460.      printf(" signed  int value = %d",*b);
  461.  
  462.    return 0;
  463. }
  464. MITIGATION:
  465. #include <stdio.h>
  466. #include <stdlib.h>
  467. #include<string.h>
  468. int main(){
  469. unsigned int size=-1;
  470. char *input_str="ksjdbfks";
  471. int *array;
  472. if (size < 3) {
  473. char *c_str = (char *)malloc(size);
  474. memcpy(c_str, input_str, 30);
  475. printf("%s",c_str);}
  476. else{
  477. printf("cant pass negetive values into malloc");
  478. }
  479. return(0);
  480. }
  481. To execute and implement formatted outputs.
  482. frpintf():
  483. CODE:
  484. #include<stdio.h>
  485. int main(int argc, char** argv)
  486. {
  487. printf(argv[1]);
  488. return0;
  489. }
  490. MITIGATION:
  491. #include<stdio.h>
  492. int main(int argc, char** argv)
  493. {
  494. printf("%s",argv[1]);
  495. return 0;
  496. }
  497. sprintf():
  498. CODE:
  499. #include<stdio.h>
  500. int main()
  501. {
  502. char buffer[50];
  503. int a = 10, b = 20, c;
  504. c = a + b;
  505. sprintf(buffer, "sum of two numbersssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
  506. is %d", c);
  507. printf("%s", buffer);
  508. return 0;
  509. }
  510. MITIGATION:
  511. #include<stdio.h>
  512. int main()
  513. {
  514. char buffer[50];
  515. int a = 10, b = 20, c;
  516. c = a + b;
  517. snprintf(buffer,25, "Sum of %d and %d is %d k", a, b, c);
  518. printf("%d", (int)sizeof(buffer));
  519. return 0;
  520. }
  521. fprintf():
  522. CODE:
  523. #include<stdio.h>
  524. int main()
  525. {
  526. int i, n=2;
  527. char str[50];
  528. FILE *fptr = fopen("sample.txt", "w");
  529. if (fptr == NULL)
  530. {
  531. printf("Could not open file");
  532. return 0;
  533. }
  534. for (i=0; i<n; i++)
  535. {
  536. puts("Enter a name");
  537. gets(str);
  538. fprintf(fptr,"%d.%s\n", i, str);
  539. }
  540. fclose(fptr);
  541. return 0;
  542. }
  543.  
  544.  
Advertisement
Comments
  • cyberpunk7925
    2 years
    # text 0.17 KB | 0 0
    1. //MSC00-C.
    2. // mitigation
    3. #include <stdio.h>
    4. #include<string.h>
    5. int main () {
    6. size_t len;
    7. char cstr[] = "char string";
    8. len = strlen(cstr);
    9. printf("%ld",len);
    10. return(0);
    11. }
Add Comment
Please, Sign In to add comment
Advertisement