internal UInt32 Call(IntPtr address, CallConvention convention, params object[] parameters) { try { Monitor.Enter(this.Process); Array.Reverse(parameters); foreach (object o in parameters) { Type paramt = o.GetType(); if (paramt == typeof(System.String)) { this.PushString(Convert.ToString(o)); } else if (paramt == typeof(System.Byte) && paramt.IsArray) { this.PushPointer((Byte[])o); } else { this.PushValue(Convert.ToUInt32(o)); } } return(this.Call(address, convention)); } catch (Exception ex) { System.IO.File.AppendAllText("errors-caller.txt", ex.Message + "\n" + ex.StackTrace + "\n\n"); } finally { Monitor.Exit(this.Process); } return(0); }
public RoutineDirectives(int dir = 0) { _callconv = 0; if (dir != 0) { Add(dir); } }
private protected MethodSignature(TypeSignature type, string name, CallConvention callConvention, IEnumerable <Func <ModuleDef, TypeSig> > parameters) { DeclaringType = type; Name = name; CallConvention = callConvention; int index = 0; Parameters = parameters.Select(p => new InternalParameterSignature(this, index++, p)).ToList(); }
public static CallConvention ToPatcher(this CallingConvention convention) { CallConvention result = CallConvention.Standard; switch (convention & CallingConvention.Mask) { case CallingConvention.VarArg: result |= CallConvention.VarArgs; break; } result |= (CallConvention)(convention & ~CallingConvention.Mask); return(result); }
public static bool IsAssignable(this TypeSig src, TypeSig dst, AccessMode mode) => new TypeEqualityComparer(ComparerOptions).Equals(src, dst); // TODO: Improve this check (base types, etc) public static CallingConvention ToDNLib(this CallConvention convention) { CallingConvention result = CallingConvention.Default; switch (convention & (CallConvention)CallingConvention.Mask) { case CallConvention.VarArgs: result |= CallingConvention.VarArg; break; } result |= (CallingConvention)convention & ~CallingConvention.Mask; return(result); }
public virtual void Add(int dir) { if (Enum.IsDefined(typeof(GeneralDirective), dir)) { generaldirs.Add((GeneralDirective)dir); } else if (Enum.IsDefined(typeof(CallConvention), dir)) { Callconv = (CallConvention)dir; } else { Error("Invalid routine diretive"); } }
public virtual void Add(RoutineDirectives dirs) { if (dirs == null) { return; } if (dirs.Callconv != 0) { Callconv = dirs.Callconv; } foreach (GeneralDirective dir in dirs.generaldirs) { generaldirs.Add(dir); } }
/// <summary> /// Checks the immediate coherence between function directives. /// Must be called after all directives are added /// </summary> /// <returns></returns> internal virtual bool CheckDirectives() { bool ret = true; if (_callconv == 0) { _callconv = CallConvention.Register; } if (generaldirs.Contains(GeneralDirective.VarArgs) && Callconv != CallConvention.CDecl) { ret |= Error("Varargs directive can only be used with the Cdecl calling convention"); } return(ret); }
/// <summary> /// Calls the function, return its exit code. /// </summary> private UInt32 Call(IntPtr address, CallConvention convention) { uint lpExitCode = 0; BinaryWriter opcodes = new BinaryWriter(new MemoryStream()); this.Parameters.BaseStream.Position = 0; opcodes.Write(new BinaryReader(this.Parameters.BaseStream).ReadBytes((int)this.Parameters.BaseStream.Length)); opcodes.Write((byte)0xB8); //mov eax, - opcodes.Write((Int32)address); opcodes.Write((UInt16)0xD0FF); //call eax if (convention == CallConvention.Cdecl) { opcodes.Write((UInt16)0xC481); //add esp, - opcodes.Write((UInt32)StackParams * 4); } opcodes.Write((byte)0xC2); opcodes.Write((UInt16)0x04); //retn 4 opcodes.BaseStream.Position = 0; BinaryReader reader = new BinaryReader(opcodes.BaseStream); UInt32 ops = this.GetPointer(reader.ReadBytes((int)opcodes.BaseStream.Length)); uint threadId = 0; IntPtr hThread = CreateRemoteThread(this.Process.Handle, IntPtr.Zero, 0, (IntPtr)ops, IntPtr.Zero, 0, out threadId); if (hThread != null && hThread != IntPtr.Zero) { uint waitCode = WaitForSingleObject(hThread, 2000); switch (waitCode) { case WAIT_ABANDONED: this.OutputError("WaitForSingleObject: unexpected return code (abandoned)", Marshal.GetLastWin32Error()); break; case WAIT_FAILED: this.OutputError("WaitForSingleObject: unexpected return code (failed)", Marshal.GetLastWin32Error()); break; case WAIT_SIGNALED: break; case WAIT_TIMEOUT: this.OutputError("WaitForSingleObject: unexpected return code (timeout)", Marshal.GetLastWin32Error()); break; } bool exitSuccess = GetExitCodeThread(hThread, out lpExitCode); if (!exitSuccess) { this.OutputError("GetExitCodeThread: unexpected return code (false)", Marshal.GetLastWin32Error()); } if (!CloseHandle(hThread)) { this.OutputError("CloseHandle (hThread): unexpected return code (false)", Marshal.GetLastWin32Error()); } //this.Handles.Add(hThread); } else { this.OutputError("CreateRemoteThread: unexpected return code (null/IntPtr.Zero)", Marshal.GetLastWin32Error()); } foreach (IntPtr pointer in this.MemoryToRelease) { if (!VirtualFreeEx(this.Process.Handle, pointer, 0, AllocationType.Release)) { this.OutputError("VirtualFreeEx: unexpected return code (false)", Marshal.GetLastWin32Error()); } } this.MemoryToRelease = new List <IntPtr>(); foreach (IntPtr handle in this.Handles) { if (handle == IntPtr.Zero) { continue; } bool closeHandleSuccess = CloseHandle(handle); if (!closeHandleSuccess) { this.OutputError("CloseHandle: unexpected return code (false), value: " + handle.ToInt32(), Marshal.GetLastWin32Error()); } } this.Handles = new List <IntPtr>(); return(lpExitCode); }
internal InternalMethodSignature(TypeSignature type, string name, CallConvention callConvention, IEnumerable <Func <ModuleDef, TypeSig> > parameters) : base(type, name, callConvention, parameters) { }
public MethodSignature Method(string name, CallConvention callConvention, IEnumerable <Func <ModuleDef, TypeSig> > parameters) => new InternalMethodSignature(this, name, callConvention, parameters.ToArray());
public MethodSignature Method(string name, CallConvention callConvention, params Func <ModuleDef, TypeSig>[] parameters) => Method(name, callConvention, (IEnumerable <Func <ModuleDef, TypeSig> >)parameters);