Thursday, June 1, 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()










Related links


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

No comments: