Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

COMHunter update #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 65 additions & 13 deletions COMHunter/Program.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management;
using System.Runtime;
using System.Reflection;
using System.Runtime.InteropServices;

namespace COMHunter
{
class Program
{
public static string usage = "Usage: COMHunter.exe <-inproc|-localserver>";
public static string usage = "Usage: COMHunter.exe [-inproc | -localserver] [-includesystem]";

public struct COMServer
{
Expand All @@ -17,22 +20,60 @@ public struct COMServer
public string Type;
}

// Trim function is not present on .NET versions < 4.6
public static string TrimEnd(string source, string value)
{
if (!source.EndsWith(value)) {
return source;
}
return source.Remove(source.LastIndexOf(value));
}

static void Main(string[] args)
{
List<COMServer> servers = new List<COMServer>();

if (args.Length == 0)
HashSet<string> exclusionSet = new HashSet<string>();
exclusionSet.Add("ToString");
exclusionSet.Add("GetLifetimeService");
exclusionSet.Add("InitializeLifetimeService");
exclusionSet.Add("CreateObjRef");
exclusionSet.Add("Equals");
exclusionSet.Add("GetHashCode");
exclusionSet.Add("GetType");

bool includeSystem = false;
bool inproc = true;
bool localserver = true;
foreach(string arg in args) {
if (arg.Equals("-includesystem")) {
includeSystem = true;
} else if (arg.Equals("-inproc")) {
localserver = false;
} else if (arg.Equals("-localserver")) {
inproc = false;
} else {
Console.WriteLine(usage);
return;
}
}

if (!inproc && !localserver) {
inproc = localserver = true;
}

if (inproc && localserver)
{
servers = WMICollection("InprocServer32");
servers.AddRange(WMICollection("LocalServer32"));
servers = WMICollection("InprocServer32", includeSystem);
servers.AddRange(WMICollection("LocalServer32", includeSystem));
}
else if(args[0] == "-inproc")
else if (inproc)
{
servers = WMICollection("InprocServer32");
servers = WMICollection("InprocServer32", includeSystem);
}
else if (args[0] == "-localserver")
else if (localserver)
{
servers = WMICollection("LocalServer32");
servers = WMICollection("LocalServer32", includeSystem);
}
else
{
Expand All @@ -43,11 +84,19 @@ static void Main(string[] args)
foreach (COMServer server in servers)
{
Console.WriteLine("{0} {1} ({2})", server.CLSID, server.ServerPath, server.Type);
Type t = Type.GetTypeFromCLSID(new Guid(server.CLSID));
MethodInfo[] methods = t.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
foreach (MethodInfo m in methods) {
if(exclusionSet.Contains(m.Name)) {
continue;
}
Console.WriteLine("\t- " + m.Name);
}
}
return;
}

static List<COMServer> WMICollection(string type)
static List<COMServer> WMICollection(string type, bool includeSystem)
{
List<COMServer> comServers = new List<COMServer>();
try
Expand All @@ -57,13 +106,16 @@ static List<COMServer> WMICollection(string type)
{
// Collect InProcServer32 values
string svrObj = Convert.ToString(queryObj[type]);
string svr = Environment.ExpandEnvironmentVariables(svrObj).Trim('"');
string svr = TrimEnd(Environment.ExpandEnvironmentVariables(svrObj), "\"");

if (!string.IsNullOrEmpty(svr)
&& svr.ToLower().StartsWith(@"c:\") // Filter out things like combase.dll and ole32.dll
&& !svr.ToLower().Contains(@"c:\windows\") // Ignore OS components
&& svr.ToLower().StartsWith(@"c:\")
&& File.Exists(svr)) // Make sure the file exists
{
//Ignore OS components, if flag isn't set
if(!includeSystem && svr.ToLower().Contains(@"c:\windows\")) {
continue;
}
comServers.Add(new COMServer
{
CLSID = queryObj["ComponentId"].ToString(),
Expand Down