Saturday, August 29, 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()










More articles


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

No comments:

Post a Comment