/// <summary> /// Constructor /// </summary> /// <param name="result">Result</param> /// <param name="config">Configuration</param> public NeoDebugger(ReverseResult result, NeoConfig config) : base(result, config) { // Set config if (config == null) { config = new NeoConfig(); } // Create script byte[] script; using (MemoryStream ms = new MemoryStream()) { //uint offset = 0; foreach (Instruction i in Instructions) { //i.Offset = offset; /*offset +=*/ i.Write(ms); } script = ms.ToArray(); } // Prepare engine Engine = config.CreateEngine(); // Load script Engine.LoadScript(script, false); //Engine.LoadScript(verifiable.Scripts[i].InvocationScript, true); // VerifyScripts if (Config == null || Engine == null) { State |= DebuggerState.Error; } }
/// <summary> /// 支付交易返回失败或支付系统超时,调用该接口撤销交易。如果此订单用户支付失败,微信支付系统会将此订单关闭;如果用户支付成功,微信支付系统会将此订单资金退还给用户。 /// 注意:7天以内的交易单可调用撤销,其他正常支付的单如需实现相同功能请调用申请退款API。提交支付交易后调用【查询订单API】,没有明确的支付结果再调用【撤销订单API】。 /// 调用支付接口后请勿立即调用撤销订单API,建议支付后至少15s后再调用撤销订单接口。 /// </summary> /// <param name="model"></param> /// <returns></returns> public ReverseResult Reverse(ReverseRequest model) { var url = "https://api.mch.weixin.qq.com/secapi/pay/reverse"; ReverseResult result = null; try { var wechatConfig = WeChatConfig; model.Appid = wechatConfig.AppId; model.Mch_id = PayConfig.MchId; model.Nonce_str = PayUtil.GetNoncestr(); //本地或者服务器的证书位置(证书在微信支付申请成功发来的通知邮件中) var cert = PayConfig.PayCertPath; //私钥(在安装证书时设置) var password = PayConfig.CertPassword; //调用证书 var cer = new X509Certificate2(cert, password, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet); var dictionary = PayUtil.GetAuthors(model); model.Sign = PayUtil.CreateMd5Sign(dictionary, PayConfig.TenPayKey); //生成Sign result = PostXML <ReverseResult>(url, model, cer); } catch (Exception ex) { WeChatHelper.LoggerAction?.Invoke(nameof(TenPayV3), ex.ToString()); } return(result); }
void LoadFiles() { if (Config == null) { return; } tsProgressBar.Visible = true; Enabled = false; GridOpCode.DataSource = null; LastSaveFile = null; // Delete tabs except Instructions for (int x = tabControl1.TabCount - 1; x >= 2; x--) { TabPage t = tabControl1.TabPages[x]; tabControl1.TabPages.Remove(t); t.Dispose(); } if (Result != null) { Result.Instructions.CollectionChanged -= Instructions_CollectionChanged; Result.Instructions.Clear(); Result = null; } new Task(() => { ALoadFiles(); }).Start(); }
private ModelNode ReverseModel(ModelNode deployedModel, IEnumerable <Type> reverseHandlers, ReverseOptions options) { ReverseResult reverseResut = null; WithCSOMContext(context => { var reverseService = new StandardCSOMReverseService(); if (reverseHandlers != null) { reverseService.Handlers.Clear(); foreach (var reverseHandler in reverseHandlers) { var reverseHandlerInstance = Activator.CreateInstance(reverseHandler) as CSOMReverseHandlerBase; reverseService.Handlers.Add(reverseHandlerInstance); } } if (deployedModel.Value.GetType() == typeof(FarmDefinition)) { throw new SPMeta2NotImplementedException( string.Format("Runner does not support model of type: [{0}]", deployedModel.Value.GetType())); } else if (deployedModel.Value.GetType() == typeof(WebApplicationDefinition)) { throw new SPMeta2NotImplementedException( string.Format("Runner does not support model of type: [{0}]", deployedModel.Value.GetType())); } else if (deployedModel.Value.GetType() == typeof(SiteDefinition)) { reverseResut = reverseService.ReverseSiteModel(context, options); } else if (deployedModel.Value.GetType() == typeof(WebDefinition)) { reverseResut = reverseService.ReverseWebModel(context, options); } else if (deployedModel.Value.GetType() == typeof(ListDefinition)) { throw new SPMeta2NotImplementedException( string.Format("Runner does not support model of type: [{0}]", deployedModel.Value.GetType())); } else { throw new SPMeta2NotImplementedException( string.Format("Runner does not support model of type: [{0}]", deployedModel.Value.GetType())); } }); return(reverseResut.Model); }
protected virtual ReverseResult ReverseInternal(object modelHost, ReverseOptions options) { var result = new ReverseResult(); var context = new ReverseContext { ReverseOptions = options, ReverseHost = modelHost as ReverseHostBase }; ReverseModel(context); result.Model = context.RootModelNode; return(result); }
void openToolStripMenuItem_Click(object sender, EventArgs e) { IReverseTemplate tag = null; if (sender != null && sender is ToolStripItem ti) { tag = (IReverseTemplate)ti.Tag; } if (tag == null) { return; } if (Reverser != null) { Reverser = null; } CleanDebugger(); if (Result != null) { Result.Instructions.Clear(); Result = null; } Template = tag; Reverser = Template.CreateReverser(); Reverser.OnParseProgress += OnReverseProgress; if (Config == null || Template.ConfigType == null || Config.GetType() != Template.ConfigType) { if (Config != null && Config is IDisposable d) { d.Dispose(); } //preferencesToolStripMenuItem_Click(sender, e); Config = null; } EnableDisableConfig(); EnableDisableDebugger(); }
void ALoadFiles() { try { ReverseResult rs = new ReverseResult(); if (Config != null && !Reverser.TryParse(Config, ref rs)) { throw (new Exception("Error parsing the file")); } EndLoad(rs); } catch (Exception ex) { EndLoad(null); Error(ex); } }
/// <summary> /// Constructor /// </summary> /// <param name="result">Reverse Result</param> /// <param name="debugConfig">Debugger config</param> protected DebuggerBase(ReverseResult result, T debugConfig) { if (result != null) { Modules.CopyFrom(result.Modules); Instructions.CopyFrom(result.Instructions); } Config = debugConfig; State = DebuggerState.None; InvocationStackCount = 0; CurrentInstructionIndex = 0; Instruction ins = Instructions[CurrentInstructionIndex]; if (ins != null) { Module m = Modules.GetModuleOf(ins.Location); if (m != null) { _CurrentMethod = m.Methods.GetMethodOf(ins.Location); } } }
/// <summary> /// Get instructions from stream /// </summary> /// <param name="initClass">Init class</param> /// <param name="result">Result</param> public virtual bool TryParse(object initClass, ref ReverseResult result) { uint insNumber = 0; uint offset = 0; if (result == null) { result = new ReverseResult() { } } ; if (initClass == null || !(initClass is IInitClassStream ics)) { return(false); } byte[] all; OffsetRelationCache offsetCache = new OffsetRelationCache(); List <Instruction> recallJump = new List <Instruction>(); List <Instruction> calls = new List <Instruction>(); PrapareResultForOcurrences(result); using (MemoryStream ms = new MemoryStream()) { foreach (StreamModule module in ics.GetStream()) { Types.Module mAdd = new Types.Module() { Name = module.Name, Start = new IndexOffset() { Offset = (uint)ms.Position, Index = (uint)result.Instructions.Count }, Color = module.Color }; Method mEntryPoint = new Method() { Start = mAdd.Start, Name = "EntryPoint of " + module.Name, }; Instruction lastIns = null; try { if (module.Stream is FileStream fi && Path.GetExtension(fi.Name).ToLowerInvariant() == ".json") { #region load file if are json FileStream long originalPos = module.Stream.Position; all = new byte[module.Stream.Length - originalPos]; module.Stream.Read(all, 0, all.Length); string json = Encoding.UTF8.GetString(all, 0, all.Length); result = JsonHelper.Deserialize <ReverseResult>(json, true); if (result != null) { // Prepare ocurrences PrapareResultForOcurrences(result); // Fill cache offsetCache.FillWith(result.Instructions); // Process instructions (Jumps) using (MemoryStream msX = new MemoryStream()) { foreach (Instruction i in result.Instructions) { ProcessInstruction(result.Instructions, i, offsetCache); // Recall jumps if (i.Jump != null && i.Jump.To != null && offsetCache.TryGetValue(i.Jump.To.Offset, out uint index, OffsetIndexRelation.OffsetToIndex)) { i.Jump.To.Index = index; } i.Write(msX); } result.Bytes = msX.ToArray(); } // Regenerate borders result.StyleMethodBorders(); // Regenerate ocurrences result.GenerateOcurrences(); return(result.Instructions.Count > 0); } module.Stream.Seek(originalPos, SeekOrigin.Begin); #endregion } long max = module.Stream.Length; int percent = 0, newPercent = 0; while (true) { byte[] opCode = new byte[OpCodeSize]; if (module.Stream.Read(opCode, 0, OpCodeSize) != OpCodeSize) { break; } string key = opCode.ToHexString(); if (!OpCodeCache.TryGetValue(key, out OpCodeArgumentAttribute read) || read == null) { throw (new OpCodeNotFoundException() { Offset = offset, OpCode = opCode, }); } OpCodeEmptyArgument arg = read.Create(); uint rBytes = arg.Read(module.Stream); lastIns = new Instruction() { OpCode = new OpCode() { RawValue = opCode, Name = read.OpCode, Description = read.Description, Flags = read.Flags }, Argument = arg.GetType() == typeof(OpCodeEmptyArgument) ? null : arg, Comment = arg.ASCIIValue, Color = mAdd.Color, }; lastIns.Location.Index = insNumber; lastIns.Location.Offset = offset; offsetCache.Add(lastIns.Location); ProcessInstruction(result.Instructions, lastIns, offsetCache); if (lastIns.OpCode.Flags.HasFlag(OpCodeFlag.IsCall)) { calls.Add(lastIns); } else { if (mEntryPoint != null && lastIns.OpCode.Flags.HasFlag(OpCodeFlag.IsRet)) { mEntryPoint.End = lastIns.Location; mAdd.Methods.Add(mEntryPoint); mEntryPoint = null; } } // Recall jumps if (lastIns.Jump != null && !lastIns.Jump.IsDynamic && lastIns.Jump.To.Index == uint.MaxValue) { recallJump.Add(lastIns); } result.Instructions.Add(lastIns); offset += (uint)(rBytes + OpCodeSize); insNumber++; newPercent = (int)((module.Stream.Position * 100) / max); if (percent != newPercent) { percent = newPercent; OnParseProgress?.Invoke(this, percent); } lastIns.Write(ms); } if (mEntryPoint != null) { mEntryPoint.End = lastIns.Location; mAdd.Methods.Add(mEntryPoint); mEntryPoint = null; } } catch (Exception er) { result = null; throw (er); } finally { if (module != null) { module.Dispose(); } } long pos = ms.Position; mAdd.End = lastIns.Location; mAdd.Size = (uint)(pos - mAdd.Start.Offset); ms.Seek(mAdd.Start.Offset, SeekOrigin.Begin); all = new byte[mAdd.Size]; ms.Read(all, 0, all.Length); using (SHA1 sha = SHA1.Create()) mAdd.Hash = sha.ComputeHash(all, 0, all.Length).ToHexString(); result.Modules.Add(mAdd); } result.Bytes = ms.ToArray(); } // Recall jumps foreach (Instruction j in recallJump) { if (offsetCache.TryGetValue(j.Jump.To.Offset, out uint index, OffsetIndexRelation.OffsetToIndex)) { j.Jump.To.Index = index; } else { // If enter here, there will be an error j.Jump = null; } }
/// <summary> /// Prepare result /// </summary> /// <param name="result">Result</param> public virtual void PrapareResultForOcurrences(ReverseResult result) { if (result == null) { return; } if (!result.Ocurrences.ContainsKey("Unusable Code")) { result.Ocurrences["Unusable Code"] = new OcurrenceCollection() { Checker = UnusableCodeCheckOcurrence, ControlParams = new UnusableCodeChartParams(result) } } ; if (!result.Ocurrences.ContainsKey("Strings")) { result.Ocurrences["Strings"] = new OcurrenceCollection() { Checker = StringCheckOcurrence } } ; if (!result.Ocurrences.ContainsKey("OpCodes")) { result.Ocurrences["OpCodes"] = new OcurrenceCollection() { Checker = OpCodesCheckOcurrence } } ; } /// <summary> /// Check if instruction are unusable /// </summary> /// <param name="i">Instruction</param> /// <param name="name">OpCode name</param> bool UnusableCodeCheckOcurrence(Instruction i, out string name) { if (i.Flags.HasFlag(InstructionFlag.UnusableCode) && i.OpCode != null) { name = i.OpCode.Name; return(true); } name = null; return(false); } /// <summary> /// Check if instruction have OpCode /// </summary> /// <param name="i">Instruction</param> /// <param name="name">OpCode name</param> bool OpCodesCheckOcurrence(Instruction i, out string name) { name = i.OpCode.Name; return(!string.IsNullOrEmpty(name)); } /// <summary> /// Check if instruction have ASCII value /// </summary> /// <param name="i">Instruction</param> /// <param name="name">ASCII value</param> bool StringCheckOcurrence(Instruction i, out string name) { name = i.Argument == null ? "" : i.Argument.ASCIIValue; return(!string.IsNullOrEmpty(name)); }
public virtual IDebugger CreateDebugger(ReverseResult result, object debugConfig) { throw new NotImplementedException(); }
void EndLoad(ReverseResult result) { if (InvokeRequired) { Invoke(new Action <ReverseResult>(EndLoad), result); return; } Result = result; if (result != null) { Hex.ByteProvider = new DynamicByteProvider(result == null || result.Bytes == null ? new byte[] { } : result.Bytes); foreach (string sk in result.Ocurrences.Keys) { TabPage t = new TabPage(sk) { Tag = "Ocurrence" }; t.Controls.Add(new UCOcurrence(result.Ocurrences[sk], sk) { Dock = DockStyle.Fill }); tabControl1.TabPages.Add(t); } TreeModules.BeginUpdate(); TreeModules.Nodes.Clear(); foreach (Module md in result.Modules) { TreeNode tm = new TreeNode(md.Name) { Tag = md, ImageKey = "Module", SelectedImageKey = "Module" }; foreach (Method mt in md.Methods) { TreeNode tmt = new TreeNode(mt.Name) { Tag = mt, ImageKey = "Method", SelectedImageKey = "Method" }; tmt.Nodes.Add(new TreeNode("From " + mt.Start.OffsetHex) { Tag = mt.Start, ImageKey = "OffsetFrom", SelectedImageKey = "OffsetFrom" }); tmt.Nodes.Add(new TreeNode("To " + mt.End.OffsetHex) { Tag = mt.End, ImageKey = "OffsetTo", SelectedImageKey = "OffsetTo" }); tm.Nodes.Add(tmt); } TreeModules.Nodes.Add(tm); } TreeModules.ExpandAll(); TreeModules.EndUpdate(); } else { TreeModules.Nodes.Clear(); Hex.ByteProvider = new DynamicByteProvider(result == null || result.Bytes == null ? new byte[] { } : result.Bytes); } GridOpCode.DataSource = result?.Instructions; // Create debugger stopToolStripMenuItem_Click(null, null); if (result != null && result.Instructions != null) { result.Instructions.CollectionChanged += Instructions_CollectionChanged; Instructions_CollectionChanged(result.Instructions, null); } tsProgressBar.Visible = false; Enabled = true; }
/// <summary> /// Prepare result /// </summary> /// <param name="result">Resut</param> public override void PrapareResultForOcurrences(ReverseResult result) { base.PrapareResultForOcurrences(result); if (result == null) { return; } // Append syscall if (!result.Ocurrences.ContainsKey("SysCalls")) { result.Ocurrences["SysCalls"] = new OcurrenceCollection() { Checker = SysCallCheckOcurrence } } ; // Append UInt160 if (!result.Ocurrences.ContainsKey("UInt160-Addresses")) { result.Ocurrences["UInt160-Addresses"] = new OcurrenceCollection() { Checker = UInt160AddressesCheckOcurrence } } ; // Append UInt256 if (!result.Ocurrences.ContainsKey("UInt256-Addresses")) { result.Ocurrences["UInt256-Addresses"] = new OcurrenceCollection() { Checker = UInt256AddressesCheckOcurrence } } ; } /// <summary> /// Check if Instruction have any UInt256 Addresses /// </summary> /// <param name="instruction">Instruction</param> /// <param name="name">Name</param> bool UInt256AddressesCheckOcurrence(Instruction instruction, out string name) { if (instruction == null || instruction.Argument == null) { name = null; return(false); } byte[] d = instruction.Argument.RawValue; if (d != null && d.Length == 32) { UInt256 r = new UInt256(d); name = r.ToString(); return(true); } name = null; return(false); } /// <summary> /// Check if Instruction have any UInt160 Addresses /// </summary> /// <param name="instruction">Instruction</param> /// <param name="name">Name</param> bool UInt160AddressesCheckOcurrence(Instruction instruction, out string name) { if (instruction == null || instruction.Argument == null) { name = null; return(false); } byte[] d = instruction.Argument.RawValue; if (d != null && d.Length == 20) { UInt160 r = new UInt160(d); name = r.ToString(); return(true); } name = null; return(false); } /// <summary> /// Check if Instruction are SysCall /// </summary> /// <param name="instruction">Instruction</param> /// <param name="name">Name</param> bool SysCallCheckOcurrence(Instruction instruction, out string name) { if (instruction.Argument != null && instruction.OpCode != null && instruction.OpCode.Name == "SYSCALL") { name = instruction.Argument.ASCIIValue; return(!string.IsNullOrEmpty(name)); } name = null; return(false); }
IDebugger IReverseTemplate.CreateDebugger(ReverseResult result, object debugConfig) { DebuggerT t = CreateDebugger(result, debugConfig); return(t); }
/// <summary> /// Create debugger /// </summary> /// <param name="result">Reverse result</param> /// <param name="debugConfig">Debugger config</param> public virtual DebuggerT CreateDebugger(ReverseResult result, object debugConfig) { return((DebuggerT)Activator.CreateInstance(typeof(DebuggerT), new object[] { result, debugConfig })); }