Challenge for SlashRoot CTF Final at STIKOM Bali Jimbaran hosted by UKM KSL
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.3 KiB

6 년 전
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <signal.h>
  5. #include <setjmp.h>
  6. /*
  7. * SIGFPE bisa nempel di CPU, perlu dicari solusi
  8. * Test openssl lewat socat
  9. */
  10. void signal_handler(int sig)
  11. {
  12. signal(SIGFPE, signal_handler);
  13. system("/bin/sh");
  14. exit(1);
  15. }
  16. int main(int argc, char const *argv[])
  17. {
  18. int i_a = 0;
  19. int i_b = 0;
  20. int total = 0;
  21. setvbuf(stdin, NULL, _IONBF, 0);
  22. setvbuf(stdout, NULL, _IONBF, 0);
  23. setvbuf(stderr, NULL, _IONBF, 0);
  24. signal(SIGFPE, signal_handler);
  25. puts("Welcome to Online Calculator v3.1");
  26. puts("");
  27. puts("Operator supported: + - * /");
  28. puts("ex: 2 + 2");
  29. puts("ex: 2 + a //'a' to use last results");
  30. puts("");
  31. puts("------------------------------------");
  32. while (1)
  33. {
  34. char c_a[11] = "";
  35. char c_b[11] = "";
  36. char opt = 0;
  37. printf(">");
  38. if (scanf("%10s %c %10s", c_a, &opt, c_b) < 3)
  39. {
  40. puts("Failed to parse expression!");
  41. return 0;
  42. }
  43. else
  44. {
  45. int r1_a = sscanf(c_a, "%d", &i_a);
  46. int r2_a = strcmp("a", c_a) == 0;
  47. int r1_b = sscanf(c_b, "%d", &i_b);
  48. int r2_b = strcmp("a", c_b) == 0;
  49. if ((i_b == 0 && r2_a) && opt == '/')
  50. {
  51. puts("Floating point exception (core dumped)");
  52. return 0;
  53. }
  54. if (r2_a)
  55. {
  56. i_a = total;
  57. }
  58. if (r2_b)
  59. {
  60. i_b = total;
  61. }
  62. if ((r1_a || r2_a) && (r1_b || r2_b))
  63. {
  64. switch (opt)
  65. {
  66. case '+':
  67. total = i_a + i_b;
  68. break;
  69. case '-':
  70. total = i_a - i_b;
  71. break;
  72. case '*':
  73. total = i_a * i_b;
  74. break;
  75. case '/':
  76. total = i_a / i_b;
  77. break;
  78. default:
  79. puts("Unknown operator!");
  80. return 0;
  81. }
  82. printf("=%d\n", total);
  83. }
  84. else
  85. {
  86. puts("Failed to parse operand!");
  87. return 0;
  88. }
  89. }
  90. }
  91. return 0;
  92. }