Created
January 15, 2018 11:06
-
-
Save Liam0205/8e206c87677accfc65aa4291d9f49b6d to your computer and use it in GitHub Desktop.
Crack the access control of C++ class by the use of virtual table.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stddef.h> | |
| #include <iostream> | |
| class Base { | |
| public: | |
| virtual void f() { | |
| std::cout << "Your are calling Base::f (public)." << std::endl; | |
| } | |
| private: | |
| virtual void g() { | |
| std::cout << "Your are calling Base::g (private)." << std::endl; | |
| } | |
| }; | |
| class Derived : public Base{}; | |
| using funcptr_t = void(*)(void); | |
| using ptr_t = uint64_t*; | |
| funcptr_t fuckcxx(Base* const ptr, const ptrdiff_t offset) { | |
| ptr_t pvtbl = reinterpret_cast<ptr_t>(ptr); | |
| ptr_t pfunc = reinterpret_cast<ptr_t>(*pvtbl); | |
| return reinterpret_cast<funcptr_t>(*(pfunc + offset)); | |
| } | |
| int main() { | |
| Derived d; | |
| auto f = fuckcxx(&d, 0); | |
| auto g = fuckcxx(&d, 1); | |
| f(); g(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment