Friday, June 2, 2023

C++ Std::String Buffer Overflow And Integer Overflow

Interators are usually implemented using signed integers like the typical "for (int i=0; ..." and in fact is the type used indexing "cstr[i]", most of methods use the signed int, int by default is signed.
Nevertheless, the "std::string::operator[]" index is size_t which is unsigned, and so does size(), and same happens with vectors.
Besides the operator[] lack of negative index control, I will explain this later.

Do the compilers doesn't warn about this?


If his code got a large input it would index a negative numer, let see g++ and clang++ warnings:



No warnings so many bugs out there...

In order to reproduce the crash we can load a big string or vector from file, for example:


I've implemented a loading function, getting the file size with tellg() and malloc to allocate the buffer, then in this case used as a string.
Let see how the compiler write asm code based on this c++ code.



So the string constructor, getting size and adding -2 is clear. Then come the operator<< to concat the strings.
Then we see the operator[] when it will crash with the negative index.
In assembly is more clear, it will call operator[] to get the value, and there will hapen the magic dereference happens. The operator[] will end up returning an invalid address that will crash at [RAX]



In gdb the operator[] is a  allq  0x555555555180 <_znst7__cxx1112basic_stringicst11char_traitsicesaiceeixem plt="">

(gdb) i r rsi
rsi            0xfffffffffffefffe  -65538


The implmementation of operator ins in those functions below:

(gdb) bt
#0  0x00007ffff7feebf3 in strcmp () from /lib64/ld-linux-x86-64.so.2
#1  0x00007ffff7fdc9a5 in check_match () from /lib64/ld-linux-x86-64.so.2
#2  0x00007ffff7fdce7b in do_lookup_x () from /lib64/ld-linux-x86-64.so.2
#3  0x00007ffff7fdd739 in _dl_lookup_symbol_x () from /lib64/ld-linux-x86-64.so.2
#4  0x00007ffff7fe1eb7 in _dl_fixup () from /lib64/ld-linux-x86-64.so.2
#5  0x00007ffff7fe88ee in _dl_runtime_resolve_xsavec () from /lib64/ld-linux-x86-64.so.2
#6  0x00005555555554b3 in main (argc=2, argv=0x7fffffffe118) at main.cpp:29

Then crashes on the MOVZX EAX, byte ptr [RAX]

Program received signal SIGSEGV, Segmentation fault.
0x00005555555554b3 in main (argc=2, argv=0x7fffffffe118) at main.cpp:29
29     cout << "penultimate byte is " << hex << s[i] << endl;
(gdb)


What about negative indexing in std::string::operator[] ?
It's exploitable!

In a C char array is known that having control of the index, we can address memory.
Let's see what happens with C++ strings:






The operator[] function call returns the address of string plus 10, and yes, we can do abitrary writes.



Note that gdb displays by default with at&t asm format wich the operands are in oposite order:


And having a string that is in the stack, controlling the index we can perform a write on the stack.



To make sure we are writing outside the string, I'm gonna do 3 writes:


 See below the command "i r rax" to view the address where the write will be performed.


The beginning of the std::string object is 0x7fffffffde50.
Write -10 writes before the string 0x7fffffffde46.
And write -100 segfaults because is writting in non paged address.



So, C++ std::string probably is not vulnerable to buffer overflow based in concatenation, but the std::string::operator[] lack of negative indexing control and this could create vulnerable and exploitable situations, some times caused by a signed used of the unsigned std::string.size()










Read more

  1. Hacking Tools Hardware
  2. Hacking Tools 2019
  3. Pentest Tools Kali Linux
  4. Hacker Tools 2020
  5. Usb Pentest Tools
  6. What Are Hacking Tools
  7. Tools Used For Hacking
  8. Hacker
  9. Hacker Tools
  10. Hack And Tools
  11. Hacking Tools Name
  12. Nsa Hack Tools
  13. Hack Tools For Ubuntu
  14. Hacker Tools Hardware
  15. Termux Hacking Tools 2019
  16. Hacker Tools Free Download
  17. Hacking Tools Kit
  18. Hack Tools For Windows
  19. Pentest Tools Tcp Port Scanner
  20. Hack App
  21. Pentest Tools For Windows
  22. Pentest Tools Android
  23. Hacking Apps
  24. Best Hacking Tools 2019
  25. Hacker Tools Linux
  26. Termux Hacking Tools 2019
  27. Hacker Tools Windows
  28. Hacker Tools Linux
  29. Nsa Hack Tools
  30. Hacks And Tools
  31. Hacking Tools And Software
  32. Blackhat Hacker Tools
  33. Termux Hacking Tools 2019
  34. Hack Tools
  35. New Hack Tools
  36. Hacking Tools For Windows 7
  37. Hacking Tools 2019
  38. Best Hacking Tools 2019
  39. Pentest Tools Linux
  40. What Is Hacking Tools
  41. How To Hack
  42. Pentest Tools Online
  43. Tools Used For Hacking
  44. Pentest Tools Bluekeep
  45. Hacker Hardware Tools
  46. Hacking Tools For Kali Linux
  47. Hacking Apps
  48. Hack App
  49. Hacker Tools Hardware
  50. Hacking App
  51. Hacker Tools Mac
  52. Pentest Tools Url Fuzzer
  53. Pentest Tools Android
  54. Pentest Tools Framework
  55. Hacker Tools Mac
  56. Hacker Tools Hardware
  57. Install Pentest Tools Ubuntu
  58. Pentest Reporting Tools
  59. Pentest Tools Nmap
  60. Hacking Tools Usb
  61. Pentest Tools Subdomain
  62. Hacking Tools Windows
  63. Pentest Tools Online
  64. Github Hacking Tools
  65. Beginner Hacker Tools
  66. Install Pentest Tools Ubuntu
  67. Hacking Tools Windows
  68. Hack And Tools
  69. Hack Tools
  70. Black Hat Hacker Tools
  71. Hack Rom Tools
  72. Tools 4 Hack
  73. Hacking Tools Pc
  74. Hacker Tools Free
  75. Hacking Tools For Games
  76. Hacker Tools Linux
  77. Hacker Tools Hardware
  78. Kik Hack Tools
  79. Pentest Tools For Mac
  80. Pentest Box Tools Download
  81. Hacking Tools Free Download
  82. Pentest Tools Bluekeep
  83. Hacker Tools For Mac
  84. Hacking Tools For Kali Linux
  85. World No 1 Hacker Software
  86. Hacking Tools
  87. Hack Tools For Pc
  88. Hack Tools For Games
  89. Hacking Tools Windows 10
  90. Pentest Tools Linux
  91. Hacker Tools 2019
  92. Computer Hacker
  93. Hacking Apps
  94. Hacker Tools For Ios
  95. Best Hacking Tools 2020
  96. Hacking Tools 2019
  97. Tools For Hacker
  98. Computer Hacker
  99. Pentest Tools Open Source
  100. Hack Tools 2019
  101. Hacker Tools For Ios
  102. Hacks And Tools
  103. Termux Hacking Tools 2019
  104. Nsa Hack Tools
  105. Hacks And Tools
  106. Pentest Tools Url Fuzzer
  107. Top Pentest Tools
  108. Hacking App
  109. Pentest Tools For Android
  110. Pentest Tools Port Scanner
  111. Hacking Tools Download
  112. Hacker Tools 2019
  113. Hack Tools Pc
  114. Hackers Toolbox
  115. Hack Tools Download
  116. Tools For Hacker
  117. Github Hacking Tools
  118. Hacking Tools For Windows Free Download
  119. Pentest Automation Tools
  120. Pentest Tools Open Source
  121. Hacker Tools For Ios
  122. Hacking Tools Software
  123. Underground Hacker Sites
  124. Underground Hacker Sites
  125. Hacking Tools 2020
  126. Hacker Tools Apk Download
  127. Pentest Tools Subdomain
  128. Hack Tools Pc
  129. Hack Tools 2019
  130. Hacking Tools For Windows Free Download
  131. New Hack Tools
  132. Ethical Hacker Tools
  133. Free Pentest Tools For Windows
  134. Best Hacking Tools 2019
  135. Pentest Tools
  136. Pentest Tools For Mac
  137. Hacking Tools Name
  138. Pentest Tools For Android
  139. Hacking Tools Free Download
  140. Best Hacking Tools 2020
  141. Hacker Security Tools
  142. Hacking Tools And Software
  143. Best Hacking Tools 2020
  144. Hacker Tools For Pc
  145. Hacker Tools 2019
  146. Best Hacking Tools 2019
  147. Hacker Tool Kit
  148. Pentest Tools Review
  149. World No 1 Hacker Software
  150. Hacker Tools Hardware
  151. Hacker Tools Mac
  152. Pentest Tools Url Fuzzer
  153. New Hacker Tools
  154. Pentest Tools Nmap

No comments: