Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. patching operation code known as opcodes

patching operation code known as opcodes

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nah1337
    wrote on last edited by
    #1

    Hello i searched on the web but didnt find any good info how to do it. Could anyone post a example or refer me somewhere where i can read up on them i would like to hook a function using opcodes. Thnx

    A B 2 Replies Last reply
    0
    • N nah1337

      Hello i searched on the web but didnt find any good info how to do it. Could anyone post a example or refer me somewhere where i can read up on them i would like to hook a function using opcodes. Thnx

      A Offline
      A Offline
      Adam Roderick J
      wrote on last edited by
      #2

      NT based API hooking is also good is it really necessary for you to hook with the functions using op codes. otherwise my suggestion is NT based :- API hooking revealed[^] kernel based:- API Hooking (LoadLibrary)[^]

      Величие не Бога может быть недооценена.

      N 1 Reply Last reply
      0
      • A Adam Roderick J

        NT based API hooking is also good is it really necessary for you to hook with the functions using op codes. otherwise my suggestion is NT based :- API hooking revealed[^] kernel based:- API Hooking (LoadLibrary)[^]

        Величие не Бога может быть недооценена.

        N Offline
        N Offline
        nah1337
        wrote on last edited by
        #3

        great sugestions but i would like to really know about more how to hook functions using opcodes...

        1 Reply Last reply
        0
        • N nah1337

          Hello i searched on the web but didnt find any good info how to do it. Could anyone post a example or refer me somewhere where i can read up on them i would like to hook a function using opcodes. Thnx

          B Offline
          B Offline
          Bacon Ultimate Cheeseburger
          wrote on last edited by
          #4

          In order to properly perform the type of hook operation you are describing you will need to implement some type of code relocation functionality. This is typically done by relocating one opcode at a time until enough memory is available to insert the patched code (or hook). This requires that each opcode and any operands be decoded and then encoded at a new location. Simply copying the opcode may not be sufficient if it uses relative addressing. From a top level view you would typically do the following: 1. Allocate enough memory for the original code to be copied to. See VirtualAllocEx() in the Windows SDK. 2. Decode a single opcode. 3. Encode/assemble the opcode at it's new location. 4. Repeat steps 2 and 3 until there is enough memory available to insert the hook. 5. Insert a jmp operation at the end of the relocated opcodes. This typically points to the end of the decode stream (or pointer if you prefer). 6. Insert the hook code (typically a jmp to your code). You end up with something like this:

          Before code relocation
          user code -> | LoadLibrary (movable) | LoadLibrary main code

          After code relocation
          user code -> * jmp to your code * LoadLibrary main code
          | |
          | |
          your code -> LoadLibrary (moved)

          Before you begin you will probably want to familiarize yourself with the target CPU. Assuming that you are targeting Intel platforms visit "Intel® 64 and IA-32 Architectures Software Developer's Manuals[^]" for a list of references. The reference manuals will tell you how each instruction is composed and what if any operands need to be processed. It will also tell you how each operand is composed, which addressing modes apply to each opcode and give you a list of all opcode modifiers (prefix bytes) and what they mean. For an example of how to decode an opcode check out http://udis86.sourceforge.net/[^]

          1300 calories of pure beef goodness can't be wrong!

          N 1 Reply Last reply
          0
          • B Bacon Ultimate Cheeseburger

            In order to properly perform the type of hook operation you are describing you will need to implement some type of code relocation functionality. This is typically done by relocating one opcode at a time until enough memory is available to insert the patched code (or hook). This requires that each opcode and any operands be decoded and then encoded at a new location. Simply copying the opcode may not be sufficient if it uses relative addressing. From a top level view you would typically do the following: 1. Allocate enough memory for the original code to be copied to. See VirtualAllocEx() in the Windows SDK. 2. Decode a single opcode. 3. Encode/assemble the opcode at it's new location. 4. Repeat steps 2 and 3 until there is enough memory available to insert the hook. 5. Insert a jmp operation at the end of the relocated opcodes. This typically points to the end of the decode stream (or pointer if you prefer). 6. Insert the hook code (typically a jmp to your code). You end up with something like this:

            Before code relocation
            user code -> | LoadLibrary (movable) | LoadLibrary main code

            After code relocation
            user code -> * jmp to your code * LoadLibrary main code
            | |
            | |
            your code -> LoadLibrary (moved)

            Before you begin you will probably want to familiarize yourself with the target CPU. Assuming that you are targeting Intel platforms visit "Intel® 64 and IA-32 Architectures Software Developer's Manuals[^]" for a list of references. The reference manuals will tell you how each instruction is composed and what if any operands need to be processed. It will also tell you how each operand is composed, which addressing modes apply to each opcode and give you a list of all opcode modifiers (prefix bytes) and what they mean. For an example of how to decode an opcode check out http://udis86.sourceforge.net/[^]

            1300 calories of pure beef goodness can't be wrong!

            N Offline
            N Offline
            nah1337
            wrote on last edited by
            #5

            thnx very informative il start following you tips, thnx much !!!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups