Example #1
0
        public static unsafe DecodedResult Decode(CodeInfo nci, int maxInstructions)
        {
            var             gch = new GCHandle();
            uint            usedInstructionsCount = 0;
            CodeInfoStruct *ci = null;

            try {
                ci = AcquireCodeInfoStruct(nci, out gch);
                var dr = new DecodedResult(maxInstructions);

                distorm_decode64(ci->codeOffset, ci->code, ci->codeLen, ci->dt, dr.InstructionsPointer, (uint)maxInstructions, &usedInstructionsCount);

                dr.UsedInstructionCount = (int)usedInstructionsCount;
                return(dr);
            }
            finally {
                if (gch.IsAllocated)
                {
                    gch.Free();
                }
                if (ci != null)
                {
                    Free(ci);
                }
            }
        }
Example #2
0
    private static unsafe void Main(string[] args)
    {
      var buf = new byte[4];
      buf[0] = (byte) 0xc3;
      buf[1] = (byte) 0x33;
      buf[2] = (byte) 0xc0;
      buf[3] = (byte) 0xc3;
      var ci = new CodeInfo((long) 0x1000, buf, DecodeType.Decode32Bits, 0);
      var dr = new DecodedResult(10);
      diStorm3.Decode(ci, dr);

      foreach (var x in dr.Instructions) {
        var s = String.Format("{0:X} {1} {2}", x.Offset, x.Mnemonic, x.Operands);
        Console.WriteLine(s);
      }

      var dr2 = new DecomposedResult(10);
      diStorm3.Decompose(ci, dr2);

      foreach (var y in dr2.Instructions) {
        if (y.Opcode != Opcode.RET)
        {
          var x = diStorm3.Format(ci, y);
          var s = String.Format("{0:X} {1} {2}", x.Offset, x.Mnemonic, x.Operands);
          Console.WriteLine(s);
        }
      }

    }
Example #3
0
        public static unsafe void Decode(CodeInfo nci, DecodedResult dr)
        {
            _CodeInfo *   ci    = null;
            _DecodedInst *insts = null;
            var           gch   = new GCHandle();
            uint          usedInstructionsCount = 0;

            try
            {
                if ((ci = AcquireCodeInfoStruct(nci, out gch)) == null)
                {
                    throw new OutOfMemoryException();
                }

                var maxInstructions = dr.MaxInstructions;

                if ((insts = (_DecodedInst *)Malloc(maxInstructions * sizeof(_DecodedInst))) == null)
                {
                    throw new OutOfMemoryException();
                }

                distorm_decode64(ci->codeOffset, ci->code, ci->codeLen, ci->dt, insts, (uint)maxInstructions,
                                 &usedInstructionsCount);

                var dinsts = new DecodedInst[usedInstructionsCount];

                for (var i = 0; i < usedInstructionsCount; i++)
                {
                    dinsts[i] = CreateDecodedInstObj(&insts[i]);
                }
                dr.Instructions = dinsts;
            }
            finally {
                /* In case of an error, jInsts will get cleaned automatically. */
                if (gch.IsAllocated)
                {
                    gch.Free();
                }
                if (ci != null)
                {
                    Free(ci);
                }
                if (insts != null)
                {
                    Free(insts);
                }
            }
        }
Example #4
0
        public static unsafe void Decode(CodeInfo nci, DecodedResult dr)
        {
            _CodeInfo* ci = null;
              _DecodedInst* insts = null;
              var gch = new GCHandle();
              uint usedInstructionsCount = 0;

              try
              {
            if ((ci = AcquireCodeInfoStruct(nci, out gch)) == null)
              throw new OutOfMemoryException();

            var maxInstructions = dr.MaxInstructions;

            if ((insts = (_DecodedInst*) Malloc(maxInstructions*sizeof (_DecodedInst))) == null)
              throw new OutOfMemoryException();

            distorm_decode64(ci->codeOffset, ci->code, ci->codeLen, ci->dt, insts, (uint) maxInstructions,
                         &usedInstructionsCount);

            var dinsts = new DecodedInst[usedInstructionsCount];

            for (var i = 0; i < usedInstructionsCount; i++)
              dinsts[i] = CreateDecodedInstObj(&insts[i]);
            dr.Instructions = dinsts;
              }
              finally {
            /* In case of an error, jInsts will get cleaned automatically. */
            if (gch.IsAllocated)
              gch.Free();
            if (ci != null)
              Free(ci);
            if (insts != null)
              Free(insts);
              }
        }
Example #5
0
        public static unsafe DecodedResult Decode(CodeInfo nci, int maxInstructions)
        {
            var gch = new GCHandle();
              uint usedInstructionsCount = 0;
              CodeInfoStruct* ci = null;
              try {

            ci = AcquireCodeInfoStruct(nci, out gch);
            var dr = new DecodedResult(maxInstructions);

            distorm_decode64(ci->codeOffset, ci->code, ci->codeLen, ci->dt, dr.InstructionsPointer, (uint) maxInstructions, &usedInstructionsCount);

            dr.UsedInstructionCount = (int) usedInstructionsCount;
            return dr;
              }
              finally {
            if (gch.IsAllocated)
              gch.Free();
            if (ci != null)
              Free(ci);
              }
        }