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