tags:

views:

246

answers:

5

Is it possible to call into COM objects via x86 assembly language? If so, how?

Why would I want to do this? Let's say I've got two programs that I don't have source for - all I've got are the binaries. One of them implements a COM interface, the other doesn't. I want to inject code into the first program to call into the second using this COM interface, and this requires me to use x86 assembly.

+4  A: 

Of course it's possible - in effect that's what the C/C++ compiler does.

But why in God's name would you want to do this? If it were for educational value, then surely doing the COM stuff by hand in straight C would do the trick.


Given the updated question, I'd suggest that you write the COM stuff in a DLL and inject that DLL into the program you want to patch, then patch in simple x86 code into the program to call your DLL that does the heavy lifting. I don't recall the techniques for injecting a DLL into the address space of a process, but there are at least a couple. The AppInit registry setting (or something like that) is one.

However, I think that most of the avenues for injecting code into a process are considered security flaws (and have often been used by malware), so I suspect that Microsoft may have removed many if not most (or all) from more recent service packs or OS versions.

Michael Burr
OpenProcess/WriteProcessMemory still work. They kind of have to as the loader is implemented in userspace.
Joshua
A: 

Yes -- COM is designed primarily as a binary API albeit one that has a very easy implementation from C/C++. However, the specification is not in terms of C or C++.

You can refer to the original COM spec, there is a copy here and it must be buried somewhere on the Microsoft site as well.

Update: your use case doesn't seem to require the use of assembly directly, any code injection technique can be used. How you compile the code to be injected is a different matter to how you inject it.

Rob Walker
+1  A: 

The easiest way to do what you want is probably to create a DLL that does the COM communication, and then inject code that will force the other application to load the DLL. You can either use a DLL injectino technique or just edit the assembly of the application itself to call LoadLibrary/GetProcAddress, etc.

Edit: BTW, I've found that OllyDbg is a great tool for editing the assembly of an existing application.

Gerald
+1  A: 

I have to agree with Mike B. This sounds like something you don't want to do, but...

To expand on what Mike B said about it being possible, at the lowest level COM is an ABI (application binary interface); that is, it defines the layout of COM objects in memory, and all COM objects are required to follow this layout. COM objects (on an x86 platform) happen to be laid out in memory the same way that the Visual C++ compiler (and most other compilers) lays out objects in memory. This makes C++ (and C, with some additional effort) a convenient choice for working with COM. That said, I've never seen anyone try this in assembly, but there is a tutorial about doing COM in C available on CodeProject. The concepts there should be fairly easy to translate into assembly:

CodeProject: COM in Plain C

Mike Spross
A: 

You could go through the hard way or you could look for a tested and true solution.
Neither of them require assembly language.

Leonardo Constantino
Both of them are overkill for extremely simple functionality that I need to add. Thanks though - I have used DLL Injection, and I have played around with Detours, though not extensively.
Erik Forbes