Our Interactive Travel Map and Blog

South America

South America
Choices, choices, choices

Saturday, 20 January 2024

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

No comments: