Module: TurboRex::Windows::COM::Utils

Included in:
InProcFinder, OutOfProcFinder
Defined in:
lib/turborex/windows/com/utils.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clsid_to_raw(clsid) ⇒ Object



105
106
107
108
109
110
# File 'lib/turborex/windows/com/utils.rb', line 105

def self.clsid_to_raw(clsid)
  pstr_clsid = INTERNAL_APIPROXY.alloc_c_ary('OLECHAR', "{#{clsid}}".chars.push(0).map{|c|c.ord})
  pclsid = INTERNAL_APIPROXY.alloc_c_ptr('CLSID')
  return unless INTERNAL_APIPROXY.clsidfromstring(pstr_clsid, pclsid).nil?
  pclsid
end

.create_istorage(name, mode = STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/turborex/windows/com/utils.rb', line 95

def self.create_istorage(name, mode=STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE)
  wname = Win32API.alloc_c_ary('WCHAR', name.chars.map{|c|c.unpack('C').first}.push(0))
  ppstgOpen = Win32API.alloc_c_ptr('LPSTORAGE')
  hr = Win32API.stgcreatedocfile(wname, mode, 0, ppstgOpen)
  return false unless TinySDK.nt_success?(hr)
  istorage = Interface::IStorage.new
  istorage.this = ppstgOpen[0]
  istorage
end

.create_istreamObject



86
87
88
89
90
91
92
93
# File 'lib/turborex/windows/com/utils.rb', line 86

def self.create_istream
  ppstm = Win32API.alloc_c_ptr('LPSTREAM')
  hr = Win32API.createstreamonhglobal(0, 1, ppstm)
  return false unless TinySDK.nt_success?(hr)
  istream = Interface::IStream.new
  istream.this = ppstm[0]
  istream
end

.dll_get_class_object(rclsid, dll, interface = Interface::IClassFactory.new) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/turborex/windows/com/utils.rb', line 112

def self.dll_get_class_object(rclsid, dll, interface=Interface::IClassFactory.new)
  _api_proxy = TurboRex::Windows::COM::INTERNAL_APIPROXY.dup
  _api_proxy.new_api_c <<-EOS, dll
    HRESULT DllGetClassObject(
      REFCLSID rclsid,
      REFIID   riid,
      LPVOID   *ppv
    );
  EOS

  rclsid = clsid_to_raw(rclsid)    
  riid = clsid_to_raw(interface.iid)
  ppv = _api_proxy.alloc_c_ptr('PVOID')
  
  unless hr = _api_proxy.dllgetclassobject(rclsid, riid, ppv)
    interface.this = ppv[0]
    return interface
  end

  raise "Failed to call DllGetClassObject(): #{TinySDK.format_hex_ntstatus(hr, hex_str: true)}"
end

.marshal_interface(object, mshctx = MSHCTX_DIFFERENTMACHINE, mshlflags = MSHLFLAGS_NORMAL) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/turborex/windows/com/utils.rb', line 5

def self.marshal_interface(object, mshctx=MSHCTX_DIFFERENTMACHINE, mshlflags=MSHLFLAGS_NORMAL)
  iid = object.iid
  _iid = "{#{iid}}"
  pstr_iid = Win32API.alloc_c_ary('OLECHAR', _iid.chars.push(0).map{|c|c.ord})
  iid = Win32API.alloc_c_struct('CLSID')
  Win32API.clsidfromstring(pstr_iid, iid)

  istream = create_istream
  hr = Win32API.comarshalinterface(istream.this, iid, object.this, mshctx, 0, mshlflags)
  return false unless TinySDK.nt_success?(hr)
  istream
end

.marshal_interface_to_string(object, mshctx = MSHCTX_DIFFERENTMACHINE, mshlflags = MSHLFLAGS_NORMAL) ⇒ Object



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
# File 'lib/turborex/windows/com/utils.rb', line 18

def self.marshal_interface_to_string(object, mshctx=MSHCTX_DIFFERENTMACHINE, mshlflags=MSHLFLAGS_NORMAL)
  # https://thrysoee.dk/InsideCOM+/ch15c.htm
  istream = marshal_interface(object, mshctx, mshlflags)
  return false unless istream
  phglobal = Win32API.alloc_c_ptr('HGLOBAL') 
  size = Win32API.alloc_c_ptr('ULONG')

  iid = object.iid
  _iid = "{#{iid}}"
  pstr_iid = Win32API.alloc_c_ary('OLECHAR', _iid.chars.push(0).map{|c|c.ord})
  iid = Win32API.alloc_c_struct('CLSID')
  Win32API.clsidfromstring(pstr_iid, iid) 

  hr = Win32API.cogetmarshalsizemax(size, iid, object.this, mshctx, 0, mshlflags)
  return false unless TinySDK.nt_success?(hr)

  Win32API.gethglobalfromstream(istream.this, phglobal)
  addr = Win32API.globallock(phglobal[0])

  objref = Win32API.memory_read(addr, size[0])
  Win32API.globalunlock(phglobal[0])
  istream.Release

  objref
end

.unmarshal_interface(stream, riid, interface = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/turborex/windows/com/utils.rb', line 44

def self.unmarshal_interface(stream, riid, interface=nil)
  riid =~ /\{.+\}/ ? _iid = riid : _iid = "{#{riid}}"

  pstr_iid = Win32API.alloc_c_ary('OLECHAR', _iid.chars.push(0).map{|c|c.ord})
  iid = Win32API.alloc_c_struct('CLSID')
  ppv = Win32API.alloc_c_ptr('PVOID')
  Win32API.clsidfromstring(pstr_iid, iid)

  hr = Win32API.counmarshalinterface(stream.this, iid, ppv)
  raise hr.to_s unless TinySDK.nt_success?(hr)

  pthis = ppv[0]
  return pthis unless interface

  interface.this = pthis
  interface
end

.unmarshal_interface_from_string(objref, riid, interface = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/turborex/windows/com/utils.rb', line 62

def self.unmarshal_interface_from_string(objref, riid, interface=nil)
  istream = create_istream
  return false unless istream

  buf = Win32API.alloc_c_ary('BYTE', objref.bytesize)
  buf.str = objref
  hr = istream.Write(buf, objref.bytesize, 0)
  return false unless TinySDK.nt_success?(hr)

  # WTF? Why dlibMove won't work?
  # dlibMove = Win32API.alloc_c_struct('LARGE_INTEGER')
  # dlibMove.QuadPart = 0
  # istream.Seek(dlibMove, 0, 0)

  istream.Seek(0, 0, 0)

  phglobal = Win32API.alloc_c_ptr('HGLOBAL') 
  Win32API.gethglobalfromstream(istream.this, phglobal)
  addr = Win32API.globallock(phglobal[0])
  objref = Win32API.memory_read(addr, 292)

  unmarshal_interface(istream, riid, interface)
end

Instance Method Details

#get_disptbl_count(proxy_file_info) ⇒ Object



134
135
136
137
138
139
# File 'lib/turborex/windows/com/utils.rb', line 134

def get_disptbl_count(proxy_file_info)
  pcif_stub_vtbl_list = to_ptr(@memory.get_page(proxy_file_info.pStubVtblList, @ptr_len))
  if_stub_vtbl = TurboRex::Windows::COM::INTERNAL_APIPROXY.alloc_c_struct('CInterfaceStubVtbl')
  if_stub_vtbl.str = @memory.get_page(pcif_stub_vtbl_list, if_stub_vtbl.sizeof)
  return if_stub_vtbl.header.DispatchTableCount
end

#get_pid_by_std_objref(interface) ⇒ Object

Note: The interface stub should be standard.



159
160
161
162
# File 'lib/turborex/windows/com/utils.rb', line 159

def get_pid_by_std_objref(interface)
  objref = TurboRex::Windows::Win32API.decode_c_struct('OBJREF', interface.marshal_to_string)
  objref.u_standard.std.ipid.Data2
end

#get_proxy_file_info(iid) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/turborex/windows/com/utils.rb', line 141

def get_proxy_file_info(iid)
  # TODO: Replace to call CoGetPSClsid()
  require 'win32/registry'
  Win32::Registry::HKEY_CLASSES_ROOT.open("Interface\\{#{iid}}") do |reg|
    psclsid32_key = reg.open('ProxyStubClsid32')
    ps_clsid = psclsid32_key.read('').last
    psclsid32_key.close

    Win32::Registry::HKEY_CLASSES_ROOT.open("CLSID\\#{ps_clsid}") do |reg_clsid|
      inproc32_key = reg_clsid.open('InprocServer32') 
      dll_path = inproc32_key.read_s_expand('')
      inproc32_key.close
      return internal_get_proxyfile(dll_path, ps_clsid.delete('{}'))
    end
  end
end

#internal_get_proxyfile(path, clsid) ⇒ Object



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
# File 'lib/turborex/windows/com/utils.rb', line 164

def internal_get_proxyfile(path, clsid)
  _api_proxy = TurboRex::Windows::COM::INTERNAL_APIPROXY.dup
  _api_proxy.new_api_c <<-EOS, path
    void RPC_ENTRY GetProxyDllInfo( const ProxyFileInfo*** pInfo, const CLSID ** pId );
    HRESULT DllGetClassObject(
      REFCLSID rclsid,
      REFIID   riid,
      LPVOID   *ppv
    );
  EOS

  begin
    ppproxy_fileinfo = _api_proxy.alloc_c_ptr('PVOID')
    proxy_file_info = _api_proxy.alloc_c_struct('ProxyFileInfo')
    ppclsid = _api_proxy.alloc_c_ptr('PVOID')
    _api_proxy.getproxydllinfo(ppproxy_fileinfo, ppclsid)
    pproxy_file_info = to_ptr(@memory.get_page(to_ptr(ppproxy_fileinfo.str), @ptr_len))
    proxy_file_info.str = @memory.get_page(pproxy_file_info, proxy_file_info.sizeof)
  rescue NoMethodError # GetProxyDllInfo() is not exported
    ipsfactory = Interface::IPSFactoryBuffer.new
    Utils.dll_get_class_object(clsid, path, ipsfactory)
    cstd_psfactory = _api_proxy.alloc_c_struct('CStdPSFactoryBuffer')
    cstd_psfactory.str =  @memory.get_page(ipsfactory.this, cstd_psfactory.sizeof)

    pproxy_file_info = to_ptr(@memory.get_page(cstd_psfactory.pProxyFileList, @ptr_len))
    proxy_file_info = _api_proxy.alloc_c_struct('ProxyFileInfo')
    proxy_file_info.str = @memory.get_page(pproxy_file_info, proxy_file_info.sizeof)
    ipsfactory.Release
  end

  proxy_file_info
end

#to_ptr(raw) ⇒ Object



197
198
199
200
201
202
203
204
205
206
# File 'lib/turborex/windows/com/utils.rb', line 197

def to_ptr(raw)
  format = case @ptr_len
  when 8
    'Q'
  when 4
    'L'
  end

  raw.unpack(format).first
end