jueves, 27 de agosto de 2020

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()










Continue reading


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

No hay comentarios:

Publicar un comentario