hubertf

snyk CTF 2025 - Math For Me

Feb 28th, 2025
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. Writeup: Math For Me - Reverse Engineering
  2. [email protected], 2025-02-28
  3.  
  4.  
  5. % file math4me
  6. math4me: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=662371bb4df3dc3e85b895f2e3f0f1c880c63471, for GNU/Linux 3.2.0, not stripped
  7.  
  8. % strings math4me
  9. %d: %d
  10. Welcome to the Math Challenge!
  11. Find the special number:
  12. Congratulations! Here's your flag: %s
  13. That's not the special number. Try again!
  14.  
  15. Decompile with IDA-Free
  16.  
  17. Right magic number that makes check_number() true: 20
  18.  
  19. % cat m4m.c
  20. #include <stdio.h>
  21. #include <string.h>
  22.  
  23. int check_number(int n)
  24. {
  25. return (5 * n + 4) / 2 == 52; // true for input 20
  26. }
  27.  
  28. void compute_flag_char(char *target, int i, int input_num) // i=5, 6, ... 36
  29. {
  30. int funnyoffset; // [rsp+1Ch] [rbp-A4h]
  31. int plusminus[20]; // [rsp+20h] [rbp-A0h]
  32. char longnumberstring[72]; // [rsp+70h] [rbp-50h] BYREF
  33.  
  34. strcpy(longnumberstring, "5552ef842ecab9be1b15536653855555552ef842ecab9be1b1553665385555");
  35. plusminus[0] = 1;
  36. plusminus[1] = 3;
  37. plusminus[2] = -2;
  38. plusminus[3] = 4;
  39. plusminus[4] = -1;
  40. plusminus[5] = 2;
  41. plusminus[6] = -3;
  42. plusminus[7] = 1;
  43. plusminus[8] = 4;
  44. plusminus[9] = -2;
  45. plusminus[10] = 3;
  46. plusminus[11] = -1;
  47. plusminus[12] = 2;
  48. plusminus[13] = -4;
  49. plusminus[14] = 1;
  50. plusminus[15] = -2;
  51. plusminus[16] = 3;
  52. plusminus[17] = -1;
  53. plusminus[18] = 2;
  54. funnyoffset = i * input_num % 5 + plusminus[i % 10];
  55. printf("%d: %d\n", i, funnyoffset);
  56. *(char *)(i + target) = longnumberstring[i] + funnyoffset;
  57. }
  58.  
  59. int main(void)
  60. {
  61. int input_num; // [rsp+8h] [rbp-48h] BYREF
  62. int i; // [rsp+Ch] [rbp-44h]
  63. char v6[37]; // [rsp+10h] [rbp-40h] BYREF
  64. char v7[19]; // [rsp+35h] [rbp-1Bh] BYREF
  65.  
  66. puts("Welcome to the Math Challenge!");
  67. printf("Find the special number: ");
  68. scanf("%d", &input_num);
  69. memcpy(v6, "flag{", 5);
  70. if ( check_number(input_num) )
  71. {
  72. for ( i = 5; i <= 36; ++i )
  73. compute_flag_char(v6, (unsigned int)i, input_num);
  74. strcpy(v7, "}");
  75. printf("Congratulations! Here's your flag: %s\n", v6);
  76. }
  77. else
  78. {
  79. puts("That's not the special number. Try again!");
  80. }
  81. return 0;
  82. }
  83. % gcc m4m.c -o m4m
  84. % echo 20 | ./m4m
  85. Welcome to the Math Challenge!
  86. Find the special number: 20
  87. 5: 2
  88. 6: -3
  89. 7: 1
  90. 8: 4
  91. 9: -2
  92. 10: 1
  93. 11: 3
  94. 12: -2
  95. 13: 4
  96. 14: -1
  97. 15: 2
  98. 16: -3
  99. 17: 1
  100. 18: 4
  101. 19: -2
  102. 20: 1
  103. 21: 3
  104. 22: -2
  105. 23: 4
  106. 24: -1
  107. 25: 2
  108. 26: -3
  109. 27: 1
  110. 28: 4
  111. 29: -2
  112. 30: 1
  113. 31: 3
  114. 32: -2
  115. 33: 4
  116. 34: -1
  117. 35: 2
  118. 36: -3
  119. Congratulations! Here's your flag: flag{h556cd-NOTGONNATELLYOU-368391gc
  120.  
  121. Note: add trailing '}':
  122. flag{h556cdd`=ag.c53664:45569368391gc}
  123.  
Advertisement
Add Comment
Please, Sign In to add comment