Last updated at Fri, 22 Sep 2017 19:41:54 GMT

I recently checked into github a C# library that helps allow easy communication and integration from your Mono/.NET applications.

The library follows the same Session/Manager pattern as the Nexpose library I mentioned previously in the Nexpose blog. It has support for both the core Metasploit RPC and for the Metasploit Pro RPC.

Getting started is easy. To understand a bit more the classes you have at your disposal, here are a few quick examples. First off, within the metasploitsharp namespace, you will have a MetasploitSession class, and two managers (MetasploitManager and MetasploitProManager). MetasploitManager implements core RPC methods, while MetasploitProManager inherits from MetasploitManager and implements the Pro features. You may use all three of these classes within the context of a using statement. MetasploitSession automagically logs out your session when the object is disposed at the end of its context.

using (MetasploitSession session = new MetasploitSession("metasploit", "password", "https://192.168.1.123:3790/api/1.1"))
{
    using (MetasploitManager manager = new MetasploitManager(session))
    {
        Dictionary<object, object> response = manager.GetCoreModuleStats();
        
        foreach (var pair in response)
            Console.WriteLine(pair.Key ": " pair.Value);
    }
} //session is logged out here at the end of its context, no need to manually log out.

You may also call methods directly off of the session object, and ignore the MetasploitManager completely.

using (MetasploitSession session = new MetasploitSession("metasploit", "password", "http://192.168.1.123:3790/api/1.1"))
{
    Dictionary<object, object> response = session.Execute("core.stats");
    
    foreach (var pair in response)
        Console.WriteLine(pair.Key ": " pair.Value);
} //session is logged out here

Due to C# being a strongly-typed language, and Ruby being a duck-typed language, you are at the mercy of Dictionaries of objects that can be any type. I have done my best to do most of the typing behind the scenes in the MetasploitSession class, but the types in the Dictionaries that are returned vary from method call to method call, so the programmer must know what he is expecting and type accordingly on his end.

There are plenty of examples in the github repo, going over both Core and Pro API features. This library is released under a BSD license, so feel free to fork and do what you will.