using Iced.Intel; internal sealed record ApiMethodReference(string Path,string Method,int NativeMethod,string LiteralRva,string ConstructorCallRva,string FunctionRva); // Constant arguments only. Tracks callee-saved integer constants across calls, // including the compiler's common LEA EDX,[zero+method] spelling. Incoming branch // paths merge by intersection, so conflicting branch values become unknown. No // branch outcome, memory read or caller argument is guessed. Static evidence only. internal static class NativeApiMethodScanner { private sealed record Value(ulong Number,ApiPathLiteral[]? Paths=null); internal static IEnumerable Scan(IReadOnlyList instructions,ulong imageBase, IReadOnlyDictionary literals,uint function) { if(instructions.Count==0)yield break; var byAddress=instructions.Select((instruction,index)=>(instruction.IP,index)).ToDictionary(value=>value.IP,value=>value.index); var starts=new SortedSet{0}; for(var index=0;index();var queued=new HashSet{entry};queue.Enqueue(entry); while(queue.TryDequeue(out var block)) { queued.Remove(block);var state=states[block]!.Copy();var end=block+1(); if(last.FlowControl is FlowControl.ConditionalBranch or FlowControl.UnconditionalBranch &&byAddress.TryGetValue(last.NearBranchTarget,out var destination))successors.Add(blockForInstruction[destination]); if(last.FlowControl is FlowControl.Next or FlowControl.Call or FlowControl.IndirectCall or FlowControl.ConditionalBranch &&end history,ulong imageBase, IReadOnlyDictionary literals,uint function) { var state=new Arguments(imageBase,literals);var targets=Targets(history); foreach(var instruction in history) { if(targets.Contains(instruction.IP))state.Clear();state.Observe(in instruction); } return state.Read(function); } private static HashSet Targets(IReadOnlyList instructions)=>instructions .Where(value=>value.FlowControl is FlowControl.ConditionalBranch or FlowControl.UnconditionalBranch) .Select(value=>value.NearBranchTarget).ToHashSet(); private sealed class Arguments(ulong imageBase,IReadOnlyDictionary literals) { private readonly Dictionary registers=[]; private readonly InstructionInfoFactory factory=new(); internal void Clear()=>registers.Clear(); internal Arguments Copy() {var copy=new Arguments(imageBase,literals);foreach(var pair in registers)copy.registers.Add(pair.Key,pair.Value);return copy;} internal bool Intersect(Arguments other) { var changed=false;foreach(var key in registers.Keys.ToArray()) { var prior=registers[key]; if(other.registers.TryGetValue(key,out var value)) { if(prior.Paths is {} left&&value.Paths is {} right) { var merged=left.Concat(right).Distinct().OrderBy(path=>path.Rva,StringComparer.Ordinal).ToArray(); if(merged.Length<=32) {if(!left.SequenceEqual(merged)){registers[key]=new(0,merged);changed=true;}continue;} } else if(prior.Paths is null&&value.Paths is null&&prior.Number==value.Number)continue; } registers.Remove(key);changed=true; } return changed; } internal ApiMethodReference? Read(uint function)=>ReadAll(function).SingleOrDefault(); internal IEnumerable ReadAll(uint function) { if(!registers.TryGetValue(Register.R8,out var path)||path.Paths is null ||!registers.TryGetValue(Register.RDX,out var method)||method.Paths is not null||method.Number>int.MaxValue)yield break; var number=(int)method.Number; // 5C166A..5C168A dispatches enum 4 to5C1843, which sets // CURLOPT_CUSTOMREQUEST(10036) to the literal DELETE at50BA6A0. foreach(var literal in path.Paths) yield return new(literal.Path,number switch{1=>"GET",2=>"POST",3=>"PUT",4=>"DELETE",_=>"UNRESOLVED"},number,literal.Rva,"",$"0x{function:X}"); } internal void Observe(in Instruction instruction,bool retainBranchFacts=false) { if(instruction.IsInvalid){Clear();return;} Value? result=null;var destination=instruction.Op0Kind==OpKind.Register?instruction.Op0Register:Register.None; var wide=destination.GetSize() is 4 or 8; if(wide) { if(instruction.Mnemonic==Mnemonic.Mov) { if(instruction.Op1Kind==OpKind.Register)registers.TryGetValue(instruction.Op1Register.GetFullRegister(),out result); else if(IsImmediate(instruction.Op1Kind))result=new(instruction.GetImmediate(1)); } else if(instruction.Mnemonic==Mnemonic.Xor&&instruction.Op1Kind==OpKind.Register&&instruction.Op1Register==destination)result=new(0); else if(instruction.Mnemonic==Mnemonic.Lea) { if(instruction.IsIPRelativeMemoryOperand) { var address=instruction.IPRelativeMemoryAddress; if(address>=imageBase&&address-imageBase<=uint.MaxValue&&literals.TryGetValue((uint)(address-imageBase),out var path))result=new(0,[path]); } else if(Number(instruction.MemoryBase,out var basis)&&Number(instruction.MemoryIndex,out var index)) result=new(unchecked(basis+index*(ulong)instruction.MemoryIndexScale+instruction.MemoryDisplacement64)); } else if(instruction.Mnemonic is Mnemonic.Inc or Mnemonic.Dec&&Number(destination,out var operand)) result=new(unchecked(instruction.Mnemonic==Mnemonic.Inc?operand+1:operand-1)); else if(instruction.Mnemonic is Mnemonic.Add or Mnemonic.Sub or Mnemonic.And or Mnemonic.Or&&IsImmediate(instruction.Op1Kind)&&Number(destination,out var operand2)) { var number=instruction.GetImmediate(1);result=new(instruction.Mnemonic switch {Mnemonic.Add=>unchecked(operand2+number),Mnemonic.Sub=>unchecked(operand2-number),Mnemonic.And=>operand2&number,_=>operand2|number}); } if(result is not null&&destination.GetSize()==4)result=result.Paths is null?new((uint)result.Number):null; } foreach(var used in factory.GetInfo(in instruction).GetUsedRegisters()) if(used.Access is OpAccess.Write or OpAccess.CondWrite or OpAccess.ReadWrite or OpAccess.ReadCondWrite) registers.Remove(used.Register.GetFullRegister()); if(result is not null)registers[destination.GetFullRegister()]=result; if(instruction.FlowControl is FlowControl.Call or FlowControl.IndirectCall) { foreach(var register in new[]{Register.RAX,Register.RCX,Register.RDX,Register.R8,Register.R9,Register.R10,Register.R11})registers.Remove(register); } else if(instruction.FlowControl!=FlowControl.Next&&!retainBranchFacts)Clear(); } private bool Number(Register register,out ulong value) { value=0;if(register==Register.None)return true; if(!registers.TryGetValue(register.GetFullRegister(),out var found)||found.Paths is not null)return false; value=found.Number;return true; } private static bool IsImmediate(OpKind kind)=>kind is OpKind.Immediate8 or OpKind.Immediate8to16 or OpKind.Immediate8to32 or OpKind.Immediate8to64 or OpKind.Immediate16 or OpKind.Immediate32 or OpKind.Immediate32to64 or OpKind.Immediate64; } }