|
| 1 | +During REing you will notice certain patterns. |
| 2 | + |
| 3 | +## Object ctor and dtor |
| 4 | + |
| 5 | +These patterns will help with noticing certain structures when REing code. |
| 6 | + |
| 7 | +### std::vector |
| 8 | + |
| 9 | +In ctor you won't notice it at first glance. However if you look closely: |
| 10 | + |
| 11 | +```cpp |
| 12 | +... |
| 13 | +*(_DWORD *)(a + 1) = 0; |
| 14 | +*(_DWORD *)(a + 2) = 0; |
| 15 | +*(_DWORD *)(a + 3) = 0; |
| 16 | +... |
| 17 | +``` |
| 18 | +You'll notice that `*a` wasn't set to anything, and if you find dtor: |
| 19 | +```cpp |
| 20 | +... |
| 21 | +if(*(a + 1)) |
| 22 | +{ |
| 23 | + ... |
| 24 | + free(*(_DWORD *)(a + 1)); // or operator delete(*(a + 1)); |
| 25 | +} |
| 26 | +*(_DWORD *)(a + 1) = 0; |
| 27 | +*(_DWORD *)(a + 2) = 0; |
| 28 | +*(_DWORD *)(a + 3) = 0; |
| 29 | +... |
| 30 | +``` |
| 31 | +This is a definitely std::vector and function before free (if it presents) may suggest you the type of vector. |
| 32 | + |
| 33 | +```cpp |
| 34 | +... |
| 35 | +if(a->v.begin) |
| 36 | +{ |
| 37 | + ... |
| 38 | + free(a->v.begin); // or operator delete(*(a + 1)); |
| 39 | +} |
| 40 | +a->v.begin = 0; |
| 41 | +a->v.end = 0; |
| 42 | +a->v.capacity_end = 0; |
| 43 | +... |
| 44 | +``` |
| 45 | + |
| 46 | +**Note: offsets may be different, but overall idea stays** |
| 47 | + |
| 48 | +An example: |
| 49 | + |
| 50 | +```cpp |
| 51 | + v2 = *(std::string **)(a1 + 8); |
| 52 | + if ( v2 ) |
| 53 | + { |
| 54 | + func_FreeStringsRange(v2, *(std::string **)(a1 + 12)); |
| 55 | + operator delete(*(void **)(a1 + 8)); |
| 56 | + } |
| 57 | + *(_DWORD *)(a1 + 8) = 0; |
| 58 | + *(_DWORD *)(a1 + 12) = 0; |
| 59 | + *(_DWORD *)(a1 + 16) = 0; |
| 60 | +``` |
| 61 | +
|
| 62 | +This is a vector of std::string. |
| 63 | +
|
| 64 | +### linked list |
| 65 | +
|
| 66 | +ctor |
| 67 | +```cpp |
| 68 | +*(_DWORD *)(a1 + 4) = a1 + 4; |
| 69 | +*(_DWORD *)(a1 + 8) = a1 + 4; |
| 70 | +``` |
| 71 | + |
| 72 | +dtor |
| 73 | +```cpp |
| 74 | +*(_DWORD *)(*(_DWORD *)(a1 + 4) + 4) = *(_DWORD *)(a1 + 8); |
| 75 | +**(_DWORD **)(a1 + 8) = *(_DWORD *)(a1 + 4); |
| 76 | +*(_DWORD *)(a1 + 4) = a1 + 4; |
| 77 | +*(_DWORD *)(a1 + 8) = a1 + 4; |
| 78 | +``` |
| 79 | + |
| 80 | +This is a linked list |
| 81 | + |
| 82 | +```cpp |
| 83 | +a1->l.prev = &a1->l; |
| 84 | +a1->l.next = &a1->l; |
| 85 | +``` |
| 86 | + |
| 87 | +```cpp |
| 88 | + a1->l.next->prev = a1->l.prev; |
| 89 | + a1->l.prev->next = a1->l.next; |
| 90 | + a1->l.prev = &a1->l; |
| 91 | + a1->l.next = &a1->l; |
| 92 | +``` |
| 93 | + |
| 94 | +### std::map (binary tree) |
| 95 | + |
| 96 | +```cpp |
| 97 | +v1 = sub_465480(); // this function has weird stuff and call to *new* with size we'll use later |
| 98 | + // no first field set |
| 99 | +*((_DWORD *)this + 1) = v1; // doing some stuff with the second field |
| 100 | +*(_BYTE *)(v1 + 45/*any offset*/) = 1; // setting some value to 1 |
| 101 | +*(_DWORD *)(*((_DWORD *)this + 1) + 4) = *((_DWORD *)this + 1); |
| 102 | +**((_DWORD **)this + 1) = *((_DWORD *)this + 1); |
| 103 | +*(_DWORD *)(*((_DWORD *)this + 1) + 8) = *((_DWORD *)this + 1); |
| 104 | +*((_DWORD *)this + 2) = 0; // setting third field to zero |
| 105 | +``` |
| 106 | + |
| 107 | +This is a map based on binary tree. |
| 108 | + |
| 109 | +```cpp |
| 110 | +node = create_node(); |
| 111 | +this->m.root = node; |
| 112 | +node->is_leaf = 1; |
| 113 | +this->m.root->parent = this->m.root; |
| 114 | +this->m.root->left = this->m.root; |
| 115 | +this->m.root->right = this->m.root; |
| 116 | +this->m.size = 0; |
| 117 | +``` |
| 118 | + |
| 119 | +Where dtor will look very fancy and you'll guess it very fast |
| 120 | + |
| 121 | +```cpp |
| 122 | +... // maybe an iteration over map before |
| 123 | +some_function(&this->m, &node, this->m.root->left, this->m.root); |
| 124 | +operator delete(this->m.root); |
| 125 | +this->m.root = 0; |
| 126 | +this->m.size = 0; |
| 127 | +... |
| 128 | +``` |
| 129 | +
|
| 130 | +### std::shared_ptr |
| 131 | +
|
| 132 | +```cpp |
| 133 | +if ( v10 ) |
| 134 | +{ |
| 135 | + if ( !_InterlockedExchangeAdd(v10 + 1, 0xFFFFFFFF) ) |
| 136 | + { |
| 137 | + (*(void (__thiscall **)(volatile signed __int32 *))(*v10 + 4))(v10); |
| 138 | + if ( !_InterlockedExchangeAdd(v10 + 2, 0xFFFFFFFF) ) |
| 139 | + (*(void (__thiscall **)(volatile signed __int32 *))(*v10 + 8))(v10); |
| 140 | + } |
| 141 | +} |
| 142 | +``` |
| 143 | +You'll see this very frequently. It is inlined dtor of shared pointer. So if `v10` is a counter block, then field before it is pointer to data associated with it. |
| 144 | + |
| 145 | +```cpp |
| 146 | +if ( pi ) |
| 147 | + { |
| 148 | + if ( !_InterlockedExchangeAdd(&pi->use_count_, 0xFFFFFFFF) ) |
| 149 | + { |
| 150 | + pi->vtable->dispose(pi); |
| 151 | + if ( !_InterlockedExchangeAdd(&pi->weak_count_, 0xFFFFFFFF) ) |
| 152 | + pi->vtable->destroy(pi); |
| 153 | + } |
| 154 | + } |
| 155 | +``` |
0 commit comments