File size: 8,183 Bytes
8739cbb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | --patches a dotnet method. Prerequisite: Must not be inlined or generic, or anything complex
function ParseScriptTokens(script,values)
--parses the script for <> entries and looks up the value in the values table
if script==nil then
print(debug.traceback())
error('ParseScriptTokens: script is nil')
end
if values==nil then
print(debug.traceback())
error('ParseScriptTokens: values is nil')
end
return string.gsub(script,"<(.-)>",function(v)
local r=values[v]
if r then return r else return x end
end)
end
function dotnetpatch_getAllReferences()
--gets a list of all assemblies
--todo: if they are in-memory only, export them to a file first (create the mz/pe manually, just the metadata)
local r={}
local sysfile
if monopipe then
mono_enumImages(function(img)
local n=mono_image_get_filename(img)
local ln=extractFileName(n:lower())
--not removing default core assembly from referrences
table.insert(r,n)
if ln=='mscorlib.dll' or ln=='netstandard.dll' or ln=='system.runtime.dll' then
if sysfile==nil then
sysfile=n
end
end
end)
return r,sysfile
end
local dc=getDotNetDataCollector()
local d=dc.enumDomains()
local i
for i=1,#d do
local ml=dc.enumModuleList(d[i].DomainHandle)
local j
for j=1,#ml do
local ln=extractFileName(ml[j].Name):lower()
--not removing default core assembly from referrences
r[#r+1]=ml[j].Name
printf("%d %s", j, ml[j].Name)
if ln=='mscorlib.dll' or ln=='netstandard.dll' or ln=='system.runtime.dll' then
if sysfile==nil then
sysfile=ml[j].Name
end
end
end
end
return r,sysfile
end
function SplitDotNetName(dotnetname)
local r=mono_splitSymbol(dotnetname) --can handle both
return r.namespace, r.classname, r.methodname
end
function findDotNetMethodAddress(name, modulename)
--print(string.format("findDotNetMethodAddress('%s','%s')", name, modulename));
local result
local namespace, classname, methodname=SplitDotNetName(name)
if namespace==nil then namespace='' end
if classname==nil or classname=='' then return getAddressSafe(name) end --hexadecimal ?
if methodname==nil or methodname=='' then return getAddressSafe(name) end
if monopipe then
--mono
local dllmodulelower=modulename:lower()
--first try to get it manually using the .net interface currently used, if that fails, go for the symbolhandler method
--find the image
local assemblies=mono_enumAssemblies()
for i=1,#assemblies do
local img=mono_getImageFromAssembly(assemblies[i])
local imagename=mono_image_get_filename(img):lower()
local ln=extractFileName(imagename)
if dllmodulelower==ln or dllmodulelower==imagename then
--find the class and method
local class=mono_image_findClass(img, namespace, classname)
if class then
local method=mono_class_findMethod(class, methodname)
if method then
return mono_compile_method(method)
end
end
break
end
end
--still here
result=getAddressSafe(name) --monoscript's symbolhandler will cause this method to get compiled
if result==nil then
return nil,name..' could not be resolved'
end
else
--ms dotnet
local dnformat=''
if namespace~='' then dnformat=namespace..'.' end
dnformat=dnformat..classname..'::'..methodname
result=getAddressSafe(dnformat) --the .net module injector tries to prelink all types in the module and reload the symbolhandler for that module
if result==nil then
--try to find the stub
result=dotnet_findDotNetMethodAddress(namespace, classname, methodname, modulename)
if result==nil then
return nil,name..' could not be resolved'
end
end
end
return result
end
function detourdotnet(oldmethodaddress,newmethodaddress,oldmethodcalleraddress)
--write jmp newmethod at the compiled address of oldmethod and if oldmethodcaller is provided write a jmp <trampolinetoold> at oldmethodcaller
--print(string.format("detourdotnet(%08x,%08x,%08x)",oldmethodaddress, newmethodaddress, oldmethodcalleraddress))
local ahe,ahd=generateAPIHookScript(string.format("%.8x",oldmethodaddress), string.format("%.8x",newmethodaddress))
script=ahe..string.format([[
%.8x:
jmp originalcall
]],oldmethodcalleraddress)
--print('-------ENABLE-------')
--print(script)
--print('--------------------')
--print('');
--print('------DISABLE------')
--print(ahd)
--print('-------------------')
local aaresult,disableinfo=autoAssemble(script)
if aaresult then
return aaresult,disableinfo,ahd
else
return aaresult, disableinfo
end
end
function InjectDotNetDetour(dllmodule, oldmethodname, newmethodname, oldmethodcaller)
--load the given dll, find and compile the methods, and call detourdotnet
if dllmodule==nil then
print(debug.traceback())
print('InjectDotNetDetour: dllmodule is nil')
error('InjectDotNetDetour: dllmodule is nil')
end
--print(string.format("InjectDotNetDetour(%s, %s, %s, %s)", dllmodule, oldmethodname, newmethodname, oldmethodcaller))
if monopipe then
if mono_loadAssemblyFromFile(dllmodule)==nil then
return false,'loading '+dllmodule+' failed'
end
--get the address of oldmethodname, newmethodname, and optionally oldmethodcaller
local oldmethodaddress=getAddressSafe(oldmethodname)
local oldmethodcalleraddress=getAddressSafe(oldmethodcaller)
local newmethodaddress=findDotNetMethodAddress(newmethodname, extractFileName(dllmodule)) --get that from the injected dll, not the symhandler (in case there's a previously injected dll)
if oldmethodcaller and newmethodaddress and oldmethodcalleraddress then
return detourdotnet(oldmethodaddress,newmethodaddress,oldmethodcalleraddress)
else
error("not all addresses found")
end
else
LaunchDotNetInterface()
--print("injecting module")
if dotnet_loadModule(dllmodule)==false then
return false,'loading '..dllmodule..' failed'
end
--print("Getting oldmethod address "..oldmethodname);
local oldmethodAddress=getAddressSafe(oldmethodname)
if oldmethodAddress==nil then
--print(oldmethodname.." not perfect")
oldmethodaddress=findDotNetMethodAddress(oldmethodname)
if oldmethodaddress==nil then error('Failure getting '..oldmethodname) end
end
--printf("oldmethodaddress=%.8x",oldmethodaddress)
--print("--------------")
--print("Getting newmethod address "..newmethodname);
--local newmethodaddress=getAddressSafe(newmethodname)
--if newmethodaddress==nil then
--print(newmethodname.." not perfect")
local newmethodaddress=findDotNetMethodAddress(newmethodname, extractFileName(dllmodule))
-- if newmethodaddress==nil then error('Failure getting '..newmethodname) end
--end
--printf("newmethodaddress=%.8x",newmethodaddress)
--print("--------------")
--print("Getting oldmethodcaller address "..oldmethodcaller);
local oldmethodcalleraddress=getAddressSafe(oldmethodcaller)
if oldmethodcalleraddress==nil then
--print(oldmethodcaller.." not perfect")
oldmethodcalleraddress=findDotNetMethodAddress(oldmethodcaller, extractFileName(dllmodule))
if oldmethodcalleraddress==nil then error('Failure getting '..oldmethodcalleraddress) end
end
--printf("oldmethodcalleraddress=%.8x",oldmethodcalleraddress)
-- print("--------------")
if oldmethodaddress and newmethodaddress and oldmethodcalleraddress then
return detourdotnet(oldmethodaddress,newmethodaddress,oldmethodcalleraddress)
else
error("not all addresses found")
end
end
end
|