diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 557cbf4..ee044e5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,5 +20,8 @@ jobs: - name: Vet run: go vet ./... + - name: Verify module + run: go mod verify + - name: Test run: go test ./... diff --git a/examples/main.go b/examples/main.go index dfdf68e..e7885df 100644 --- a/examples/main.go +++ b/examples/main.go @@ -1,22 +1,35 @@ package main -// running some tests on the lib import ( "context" "fmt" "log" + "os" "github.com/arisinghackers/goxploit/pkg/metasploit" "github.com/arisinghackers/goxploit/pkg/msfrpc" ) +func env(key, fallback string) string { + if v := os.Getenv(key); v != "" { + return v + } + return fallback +} + func main() { + host := env("MSF_HOST", "127.0.0.1") + port := 3000 + ssl := env("MSF_SSL", "false") + webURI := env("MSF_WEB_URI", "/api") + user := env("MSF_USER", "msf") + pass := env("MSF_PASS", "msf") - client := msfrpc.NewMsfRpcClient("killodds", "false", "veemweaver", "127.0.0.1", 3000, "/api") + client := msfrpc.NewMsfRpcClient(pass, ssl, user, host, port, webURI) typedClient := metasploit.NewClient(client) ctx := context.Background() - login, err := typedClient.Auth.LoginContext(ctx, "veemweaver", "killodds") + login, err := typedClient.Auth.LoginContext(ctx, user, pass) if err != nil { log.Fatalf("Error authenticating: %v", err) } @@ -27,5 +40,4 @@ func main() { log.Fatal(err) } fmt.Printf("Metasploit Core Version: %s (ruby=%s api=%s)\n", version.Version, version.Ruby, version.API) - } diff --git a/internal/generator/library.go b/internal/generator/library.go index 7c4e53c..30eb834 100644 --- a/internal/generator/library.go +++ b/internal/generator/library.go @@ -50,8 +50,9 @@ func (g *LibraryGenerator) GenerateLibrary() error { builder := &strings.Builder{} builder.WriteString("// Code generated by goxploit msfrpc LibraryGenerator. DO NOT EDIT.\n\n") - builder.WriteString("package metasploit\n\n") + builder.WriteString("package generated\n\n") builder.WriteString("import (") + builder.WriteString("\n\t\"context\"") builder.WriteString("\n\t\"errors\"") builder.WriteString("\n\t\"fmt\"") builder.WriteString("\n\t\"github.com/arisinghackers/goxploit/pkg/msfrpc\"") @@ -81,6 +82,7 @@ func (g *LibraryGenerator) GenerateLibrary() error { funcName := toCamelCase(rawMethod) params := []string{} + paramNames := []string{} args := []string{fmt.Sprintf(`"%s"`, method[0])} requiresAuth := false @@ -93,6 +95,7 @@ func (g *LibraryGenerator) GenerateLibrary() error { } args = append(args, param) params = append(params, fmt.Sprintf("%s string", param)) + paramNames = append(paramNames, param) continue } @@ -102,13 +105,21 @@ func (g *LibraryGenerator) GenerateLibrary() error { args = append(args, val) } params = append(params, fmt.Sprintf("%s string", val)) + paramNames = append(paramNames, val) } + // Non-context wrapper delegates to context version. builder.WriteString(fmt.Sprintf("func (c *%s) %s(%s) (map[string]interface{}, error) {\n", className, funcName, strings.Join(params, ", "))) + builder.WriteString(fmt.Sprintf("\treturn c.%sContext(context.Background(), %s)\n", funcName, strings.Join(paramNames, ", "))) + builder.WriteString("}\n\n") + + // Context version with the actual implementation. + contextParams := append([]string{"ctx context.Context"}, params...) + builder.WriteString(fmt.Sprintf("func (c *%s) %sContext(ctx context.Context, %s) (map[string]interface{}, error) {\n", className, funcName, strings.Join(contextParams, ", "))) if requiresAuth { - builder.WriteString(fmt.Sprintf("\tresp, err := c.AuthenticatedRequest(%s)\n", buildArgsString(args))) + builder.WriteString(fmt.Sprintf("\tresp, err := c.AuthenticatedRequestContext(ctx, %s)\n", buildArgsString(args))) } else { - builder.WriteString(fmt.Sprintf("\tresp, err := c.MsfRequest(%s)\n", buildArgsString(args))) + builder.WriteString(fmt.Sprintf("\tresp, err := c.MsfRequestContext(ctx, %s)\n", buildArgsString(args))) } builder.WriteString("\tif err != nil {\n\t\treturn nil, err\n\t}\n") builder.WriteString("\tif result, ok := resp[\"result\"]; ok && result == \"failure\" {\n") diff --git a/pkg/metasploit/response.go b/pkg/metasploit/response.go index 4e2bf44..dd7c1ac 100644 --- a/pkg/metasploit/response.go +++ b/pkg/metasploit/response.go @@ -78,15 +78,27 @@ func readInt(resp map[string]interface{}, key string, required bool) (int64, err } } +var errRPCFailed = fmt.Errorf("rpc call returned failure") + func checkRPCFailure(resp map[string]interface{}) error { - if result, ok := resp["result"]; ok && fmt.Sprintf("%v", result) == "failure" { - if msg, exists := resp["error_message"]; exists { - return fmt.Errorf("%v", msg) + v, ok := resp["result"] + if !ok { + return nil + } + switch s := v.(type) { + case string: + if s != "failure" { + return nil } - return fmt.Errorf("rpc call returned failure") + case []byte: + if string(s) != "failure" { + return nil + } + default: + return nil } if msg, ok := resp["error_message"]; ok { return fmt.Errorf("%v", msg) } - return nil + return errRPCFailed } diff --git a/pkg/msfrpc/client.go b/pkg/msfrpc/client.go index 1470b4b..e9f2765 100644 --- a/pkg/msfrpc/client.go +++ b/pkg/msfrpc/client.go @@ -15,7 +15,7 @@ import ( type MsfRpcClient struct { UserPassword string Token *string - SSL string + UseSSL bool UserName string IP string Port int @@ -25,15 +25,16 @@ type MsfRpcClient struct { } func NewMsfRpcClient(userPassword, ssl, userName, ip string, port int, webServerURI string) *MsfRpcClient { + useSSL := ssl == "true" protocol := "http://" - if ssl == "true" { + if useSSL { protocol = "https://" } baseURL := fmt.Sprintf("%s%s:%d%s", protocol, ip, port, webServerURI) return &MsfRpcClient{ UserPassword: userPassword, - SSL: protocol, + UseSSL: useSSL, UserName: userName, IP: ip, Port: port, diff --git a/pkg/msfrpc/generated/auth.go b/pkg/msfrpc/generated/auth.go index de43e8e..bf41848 100644 --- a/pkg/msfrpc/generated/auth.go +++ b/pkg/msfrpc/generated/auth.go @@ -1,8 +1,9 @@ // Code generated by goxploit msfrpc LibraryGenerator. DO NOT EDIT. -package metasploit +package generated import ( + "context" "errors" "fmt" "github.com/arisinghackers/goxploit/pkg/msfrpc" @@ -17,7 +18,11 @@ func NewAuth(client *msfrpc.MsfRpcClient) *Auth { } func (c *Auth) Login(userName string, userPassword string) (map[string]interface{}, error) { - resp, err := c.MsfRequest([]interface{}{"auth.login", userName, userPassword}) + return c.LoginContext(context.Background(), userName, userPassword) +} + +func (c *Auth) LoginContext(ctx context.Context, userName string, userPassword string) (map[string]interface{}, error) { + resp, err := c.MsfRequestContext(ctx, []interface{}{"auth.login", userName, userPassword}) if err != nil { return nil, err } @@ -31,7 +36,11 @@ func (c *Auth) Login(userName string, userPassword string) (map[string]interface } func (c *Auth) Logout(LogoutToken string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"auth.logout", LogoutToken}) + return c.LogoutContext(context.Background(), LogoutToken) +} + +func (c *Auth) LogoutContext(ctx context.Context, LogoutToken string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"auth.logout", LogoutToken}) if err != nil { return nil, err } @@ -45,7 +54,11 @@ func (c *Auth) Logout(LogoutToken string) (map[string]interface{}, error) { } func (c *Auth) TokenAdd(NewToken string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"auth.token_add", NewToken}) + return c.TokenAddContext(context.Background(), NewToken) +} + +func (c *Auth) TokenAddContext(ctx context.Context, NewToken string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"auth.token_add", NewToken}) if err != nil { return nil, err } @@ -59,7 +72,11 @@ func (c *Auth) TokenAdd(NewToken string) (map[string]interface{}, error) { } func (c *Auth) TokenGenerate() (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"auth.token_generate"}) + return c.TokenGenerateContext(context.Background()) +} + +func (c *Auth) TokenGenerateContext(ctx context.Context) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"auth.token_generate"}) if err != nil { return nil, err } @@ -73,7 +90,11 @@ func (c *Auth) TokenGenerate() (map[string]interface{}, error) { } func (c *Auth) TokenList() (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"auth.token_list"}) + return c.TokenListContext(context.Background()) +} + +func (c *Auth) TokenListContext(ctx context.Context) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"auth.token_list"}) if err != nil { return nil, err } @@ -87,7 +108,11 @@ func (c *Auth) TokenList() (map[string]interface{}, error) { } func (c *Auth) TokenRemove(TokenToBeRemoved string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"auth.token_remove", TokenToBeRemoved}) + return c.TokenRemoveContext(context.Background(), TokenToBeRemoved) +} + +func (c *Auth) TokenRemoveContext(ctx context.Context, TokenToBeRemoved string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"auth.token_remove", TokenToBeRemoved}) if err != nil { return nil, err } diff --git a/pkg/msfrpc/generated/console.go b/pkg/msfrpc/generated/console.go index b22c226..61bca77 100644 --- a/pkg/msfrpc/generated/console.go +++ b/pkg/msfrpc/generated/console.go @@ -1,8 +1,9 @@ // Code generated by goxploit msfrpc LibraryGenerator. DO NOT EDIT. -package metasploit +package generated import ( + "context" "errors" "fmt" "github.com/arisinghackers/goxploit/pkg/msfrpc" @@ -17,7 +18,11 @@ func NewConsole(client *msfrpc.MsfRpcClient) *Console { } func (c *Console) Create() (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"console.create"}) + return c.CreateContext(context.Background()) +} + +func (c *Console) CreateContext(ctx context.Context) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"console.create"}) if err != nil { return nil, err } @@ -31,7 +36,11 @@ func (c *Console) Create() (map[string]interface{}, error) { } func (c *Console) Destroy(consoleId string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"console.destroy", consoleId}) + return c.DestroyContext(context.Background(), consoleId) +} + +func (c *Console) DestroyContext(ctx context.Context, consoleId string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"console.destroy", consoleId}) if err != nil { return nil, err } @@ -45,7 +54,11 @@ func (c *Console) Destroy(consoleId string) (map[string]interface{}, error) { } func (c *Console) List() (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"console.list"}) + return c.ListContext(context.Background()) +} + +func (c *Console) ListContext(ctx context.Context) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"console.list"}) if err != nil { return nil, err } @@ -59,7 +72,11 @@ func (c *Console) List() (map[string]interface{}, error) { } func (c *Console) Write(ConsoleId string, InputCommand string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"console.write", ConsoleId, InputCommand}) + return c.WriteContext(context.Background(), ConsoleId, InputCommand) +} + +func (c *Console) WriteContext(ctx context.Context, ConsoleId string, InputCommand string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"console.write", ConsoleId, InputCommand}) if err != nil { return nil, err } @@ -73,7 +90,11 @@ func (c *Console) Write(ConsoleId string, InputCommand string) (map[string]inter } func (c *Console) Read(ConsoleId string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"console.read", ConsoleId}) + return c.ReadContext(context.Background(), ConsoleId) +} + +func (c *Console) ReadContext(ctx context.Context, ConsoleId string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"console.read", ConsoleId}) if err != nil { return nil, err } @@ -87,7 +108,11 @@ func (c *Console) Read(ConsoleId string) (map[string]interface{}, error) { } func (c *Console) SessionDetach(consoleId string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"console.session_detach", consoleId}) + return c.SessionDetachContext(context.Background(), consoleId) +} + +func (c *Console) SessionDetachContext(ctx context.Context, consoleId string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"console.session_detach", consoleId}) if err != nil { return nil, err } @@ -101,7 +126,11 @@ func (c *Console) SessionDetach(consoleId string) (map[string]interface{}, error } func (c *Console) SessionKill(consoleId string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"console.session_kill", consoleId}) + return c.SessionKillContext(context.Background(), consoleId) +} + +func (c *Console) SessionKillContext(ctx context.Context, consoleId string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"console.session_kill", consoleId}) if err != nil { return nil, err } @@ -115,7 +144,11 @@ func (c *Console) SessionKill(consoleId string) (map[string]interface{}, error) } func (c *Console) Tabs(consoleId string, InputLine string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"console.tabs", consoleId, InputLine}) + return c.TabsContext(context.Background(), consoleId, InputLine) +} + +func (c *Console) TabsContext(ctx context.Context, consoleId string, InputLine string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"console.tabs", consoleId, InputLine}) if err != nil { return nil, err } diff --git a/pkg/msfrpc/generated/core.go b/pkg/msfrpc/generated/core.go index eb5299f..a9fbac5 100644 --- a/pkg/msfrpc/generated/core.go +++ b/pkg/msfrpc/generated/core.go @@ -1,8 +1,9 @@ // Code generated by goxploit msfrpc LibraryGenerator. DO NOT EDIT. -package metasploit +package generated import ( + "context" "errors" "fmt" "github.com/arisinghackers/goxploit/pkg/msfrpc" @@ -17,7 +18,11 @@ func NewCore(client *msfrpc.MsfRpcClient) *Core { } func (c *Core) AddModulePath(Path string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"core.add_module_path", Path}) + return c.AddModulePathContext(context.Background(), Path) +} + +func (c *Core) AddModulePathContext(ctx context.Context, Path string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"core.add_module_path", Path}) if err != nil { return nil, err } @@ -31,7 +36,11 @@ func (c *Core) AddModulePath(Path string) (map[string]interface{}, error) { } func (c *Core) ModuleStats() (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"core.module_stats"}) + return c.ModuleStatsContext(context.Background()) +} + +func (c *Core) ModuleStatsContext(ctx context.Context) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"core.module_stats"}) if err != nil { return nil, err } @@ -45,7 +54,11 @@ func (c *Core) ModuleStats() (map[string]interface{}, error) { } func (c *Core) ReloadModules() (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"core.reload_modules"}) + return c.ReloadModulesContext(context.Background()) +} + +func (c *Core) ReloadModulesContext(ctx context.Context) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"core.reload_modules"}) if err != nil { return nil, err } @@ -59,7 +72,11 @@ func (c *Core) ReloadModules() (map[string]interface{}, error) { } func (c *Core) Save() (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"core.save"}) + return c.SaveContext(context.Background()) +} + +func (c *Core) SaveContext(ctx context.Context) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"core.save"}) if err != nil { return nil, err } @@ -73,7 +90,11 @@ func (c *Core) Save() (map[string]interface{}, error) { } func (c *Core) Setg(OptionName string, OptionValue string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"core.setg", OptionName, OptionValue}) + return c.SetgContext(context.Background(), OptionName, OptionValue) +} + +func (c *Core) SetgContext(ctx context.Context, OptionName string, OptionValue string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"core.setg", OptionName, OptionValue}) if err != nil { return nil, err } @@ -87,7 +108,11 @@ func (c *Core) Setg(OptionName string, OptionValue string) (map[string]interface } func (c *Core) Unsetg(OptionName string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"core.unsetg", OptionName}) + return c.UnsetgContext(context.Background(), OptionName) +} + +func (c *Core) UnsetgContext(ctx context.Context, OptionName string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"core.unsetg", OptionName}) if err != nil { return nil, err } @@ -101,7 +126,11 @@ func (c *Core) Unsetg(OptionName string) (map[string]interface{}, error) { } func (c *Core) ThreadList() (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"core.thread_list"}) + return c.ThreadListContext(context.Background()) +} + +func (c *Core) ThreadListContext(ctx context.Context) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"core.thread_list"}) if err != nil { return nil, err } @@ -115,7 +144,11 @@ func (c *Core) ThreadList() (map[string]interface{}, error) { } func (c *Core) ThreadKill(ThreadID string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"core.thread_kill", ThreadID}) + return c.ThreadKillContext(context.Background(), ThreadID) +} + +func (c *Core) ThreadKillContext(ctx context.Context, ThreadID string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"core.thread_kill", ThreadID}) if err != nil { return nil, err } @@ -129,7 +162,11 @@ func (c *Core) ThreadKill(ThreadID string) (map[string]interface{}, error) { } func (c *Core) Version() (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"core.version"}) + return c.VersionContext(context.Background()) +} + +func (c *Core) VersionContext(ctx context.Context) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"core.version"}) if err != nil { return nil, err } @@ -143,7 +180,11 @@ func (c *Core) Version() (map[string]interface{}, error) { } func (c *Core) Stop() (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"core.stop"}) + return c.StopContext(context.Background()) +} + +func (c *Core) StopContext(ctx context.Context) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"core.stop"}) if err != nil { return nil, err } diff --git a/pkg/msfrpc/generated/job.go b/pkg/msfrpc/generated/job.go index 34a277f..3108f79 100644 --- a/pkg/msfrpc/generated/job.go +++ b/pkg/msfrpc/generated/job.go @@ -1,8 +1,9 @@ // Code generated by goxploit msfrpc LibraryGenerator. DO NOT EDIT. -package metasploit +package generated import ( + "context" "errors" "fmt" "github.com/arisinghackers/goxploit/pkg/msfrpc" @@ -17,7 +18,11 @@ func NewJob(client *msfrpc.MsfRpcClient) *Job { } func (c *Job) List() (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"job.list"}) + return c.ListContext(context.Background()) +} + +func (c *Job) ListContext(ctx context.Context) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"job.list"}) if err != nil { return nil, err } @@ -31,7 +36,11 @@ func (c *Job) List() (map[string]interface{}, error) { } func (c *Job) Info(jobId string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"job.info", jobId}) + return c.InfoContext(context.Background(), jobId) +} + +func (c *Job) InfoContext(ctx context.Context, jobId string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"job.info", jobId}) if err != nil { return nil, err } @@ -45,7 +54,11 @@ func (c *Job) Info(jobId string) (map[string]interface{}, error) { } func (c *Job) Stop(jobId string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"job.stop", jobId}) + return c.StopContext(context.Background(), jobId) +} + +func (c *Job) StopContext(ctx context.Context, jobId string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"job.stop", jobId}) if err != nil { return nil, err } diff --git a/pkg/msfrpc/generated/module.go b/pkg/msfrpc/generated/module.go index 0975e6f..1083a07 100644 --- a/pkg/msfrpc/generated/module.go +++ b/pkg/msfrpc/generated/module.go @@ -1,8 +1,9 @@ // Code generated by goxploit msfrpc LibraryGenerator. DO NOT EDIT. -package metasploit +package generated import ( + "context" "errors" "fmt" "github.com/arisinghackers/goxploit/pkg/msfrpc" @@ -17,7 +18,11 @@ func NewModule(client *msfrpc.MsfRpcClient) *Module { } func (c *Module) Exploits() (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"module.exploits"}) + return c.ExploitsContext(context.Background()) +} + +func (c *Module) ExploitsContext(ctx context.Context) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"module.exploits"}) if err != nil { return nil, err } @@ -31,7 +36,11 @@ func (c *Module) Exploits() (map[string]interface{}, error) { } func (c *Module) Auxiliary() (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"module.auxiliary"}) + return c.AuxiliaryContext(context.Background()) +} + +func (c *Module) AuxiliaryContext(ctx context.Context) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"module.auxiliary"}) if err != nil { return nil, err } @@ -45,7 +54,11 @@ func (c *Module) Auxiliary() (map[string]interface{}, error) { } func (c *Module) Post() (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"module.post"}) + return c.PostContext(context.Background()) +} + +func (c *Module) PostContext(ctx context.Context) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"module.post"}) if err != nil { return nil, err } @@ -59,7 +72,11 @@ func (c *Module) Post() (map[string]interface{}, error) { } func (c *Module) Payloads() (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"module.payloads"}) + return c.PayloadsContext(context.Background()) +} + +func (c *Module) PayloadsContext(ctx context.Context) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"module.payloads"}) if err != nil { return nil, err } @@ -73,7 +90,11 @@ func (c *Module) Payloads() (map[string]interface{}, error) { } func (c *Module) Encoders() (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"module.encoders"}) + return c.EncodersContext(context.Background()) +} + +func (c *Module) EncodersContext(ctx context.Context) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"module.encoders"}) if err != nil { return nil, err } @@ -87,7 +108,11 @@ func (c *Module) Encoders() (map[string]interface{}, error) { } func (c *Module) Nops() (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"module.nops"}) + return c.NopsContext(context.Background()) +} + +func (c *Module) NopsContext(ctx context.Context) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"module.nops"}) if err != nil { return nil, err } @@ -101,7 +126,11 @@ func (c *Module) Nops() (map[string]interface{}, error) { } func (c *Module) Info(ModuleType string, ModuleName string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"module.info", ModuleType, ModuleName}) + return c.InfoContext(context.Background(), ModuleType, ModuleName) +} + +func (c *Module) InfoContext(ctx context.Context, ModuleType string, ModuleName string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"module.info", ModuleType, ModuleName}) if err != nil { return nil, err } @@ -115,7 +144,11 @@ func (c *Module) Info(ModuleType string, ModuleName string) (map[string]interfac } func (c *Module) Options(ModuleType string, ModuleName string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"module.options", ModuleType, ModuleName}) + return c.OptionsContext(context.Background(), ModuleType, ModuleName) +} + +func (c *Module) OptionsContext(ctx context.Context, ModuleType string, ModuleName string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"module.options", ModuleType, ModuleName}) if err != nil { return nil, err } @@ -129,7 +162,11 @@ func (c *Module) Options(ModuleType string, ModuleName string) (map[string]inter } func (c *Module) CompatiblePayloads(ModuleName string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"module.compatible_payloads", ModuleName}) + return c.CompatiblePayloadsContext(context.Background(), ModuleName) +} + +func (c *Module) CompatiblePayloadsContext(ctx context.Context, ModuleName string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"module.compatible_payloads", ModuleName}) if err != nil { return nil, err } @@ -143,7 +180,11 @@ func (c *Module) CompatiblePayloads(ModuleName string) (map[string]interface{}, } func (c *Module) TargetCompatiblePayloads(ModuleName string, SessionId string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"module.target_compatible_payloads", ModuleName, SessionId}) + return c.TargetCompatiblePayloadsContext(context.Background(), ModuleName, SessionId) +} + +func (c *Module) TargetCompatiblePayloadsContext(ctx context.Context, ModuleName string, SessionId string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"module.target_compatible_payloads", ModuleName, SessionId}) if err != nil { return nil, err } @@ -157,7 +198,11 @@ func (c *Module) TargetCompatiblePayloads(ModuleName string, SessionId string) ( } func (c *Module) CompatibleSessions(ModuleName string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"module.compatible_sessions", ModuleName}) + return c.CompatibleSessionsContext(context.Background(), ModuleName) +} + +func (c *Module) CompatibleSessionsContext(ctx context.Context, ModuleName string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"module.compatible_sessions", ModuleName}) if err != nil { return nil, err } @@ -171,7 +216,11 @@ func (c *Module) CompatibleSessions(ModuleName string) (map[string]interface{}, } func (c *Module) Encode(Data string, EncoderModule string, Options string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"module.encode", Data, EncoderModule, Options}) + return c.EncodeContext(context.Background(), Data, EncoderModule, Options) +} + +func (c *Module) EncodeContext(ctx context.Context, Data string, EncoderModule string, Options string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"module.encode", Data, EncoderModule, Options}) if err != nil { return nil, err } @@ -185,7 +234,11 @@ func (c *Module) Encode(Data string, EncoderModule string, Options string) (map[ } func (c *Module) Execute(ModuleType string, ModuleName string, Options string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"module.execute", ModuleType, ModuleName, Options}) + return c.ExecuteContext(context.Background(), ModuleType, ModuleName, Options) +} + +func (c *Module) ExecuteContext(ctx context.Context, ModuleType string, ModuleName string, Options string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"module.execute", ModuleType, ModuleName, Options}) if err != nil { return nil, err } diff --git a/pkg/msfrpc/generated/plugin.go b/pkg/msfrpc/generated/plugin.go index 32095bf..fcec874 100644 --- a/pkg/msfrpc/generated/plugin.go +++ b/pkg/msfrpc/generated/plugin.go @@ -1,8 +1,9 @@ // Code generated by goxploit msfrpc LibraryGenerator. DO NOT EDIT. -package metasploit +package generated import ( + "context" "errors" "fmt" "github.com/arisinghackers/goxploit/pkg/msfrpc" @@ -17,7 +18,11 @@ func NewPlugin(client *msfrpc.MsfRpcClient) *Plugin { } func (c *Plugin) Load(PluginName string, Options string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"plugin.load", PluginName, Options}) + return c.LoadContext(context.Background(), PluginName, Options) +} + +func (c *Plugin) LoadContext(ctx context.Context, PluginName string, Options string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"plugin.load", PluginName, Options}) if err != nil { return nil, err } @@ -31,7 +36,11 @@ func (c *Plugin) Load(PluginName string, Options string) (map[string]interface{} } func (c *Plugin) Unload(PluginName string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"plugin.unload", PluginName}) + return c.UnloadContext(context.Background(), PluginName) +} + +func (c *Plugin) UnloadContext(ctx context.Context, PluginName string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"plugin.unload", PluginName}) if err != nil { return nil, err } @@ -45,7 +54,11 @@ func (c *Plugin) Unload(PluginName string) (map[string]interface{}, error) { } func (c *Plugin) Loaded() (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"plugin.loaded"}) + return c.LoadedContext(context.Background()) +} + +func (c *Plugin) LoadedContext(ctx context.Context) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"plugin.loaded"}) if err != nil { return nil, err } diff --git a/pkg/msfrpc/generated/session.go b/pkg/msfrpc/generated/session.go index 91d5cb3..a556789 100644 --- a/pkg/msfrpc/generated/session.go +++ b/pkg/msfrpc/generated/session.go @@ -1,8 +1,9 @@ // Code generated by goxploit msfrpc LibraryGenerator. DO NOT EDIT. -package metasploit +package generated import ( + "context" "errors" "fmt" "github.com/arisinghackers/goxploit/pkg/msfrpc" @@ -17,7 +18,11 @@ func NewSession(client *msfrpc.MsfRpcClient) *Session { } func (c *Session) List() (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"session.list"}) + return c.ListContext(context.Background()) +} + +func (c *Session) ListContext(ctx context.Context) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"session.list"}) if err != nil { return nil, err } @@ -31,7 +36,11 @@ func (c *Session) List() (map[string]interface{}, error) { } func (c *Session) Stop(sessionId string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"session.stop", sessionId}) + return c.StopContext(context.Background(), sessionId) +} + +func (c *Session) StopContext(ctx context.Context, sessionId string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"session.stop", sessionId}) if err != nil { return nil, err } @@ -45,7 +54,11 @@ func (c *Session) Stop(sessionId string) (map[string]interface{}, error) { } func (c *Session) ShellRead(sessionId string, ReadPointer string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"session.shell_read", sessionId, ReadPointer}) + return c.ShellReadContext(context.Background(), sessionId, ReadPointer) +} + +func (c *Session) ShellReadContext(ctx context.Context, sessionId string, ReadPointer string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"session.shell_read", sessionId, ReadPointer}) if err != nil { return nil, err } @@ -59,7 +72,11 @@ func (c *Session) ShellRead(sessionId string, ReadPointer string) (map[string]in } func (c *Session) ShellWrite(sessionId string, InputCommand string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"session.shell_write", sessionId, InputCommand}) + return c.ShellWriteContext(context.Background(), sessionId, InputCommand) +} + +func (c *Session) ShellWriteContext(ctx context.Context, sessionId string, InputCommand string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"session.shell_write", sessionId, InputCommand}) if err != nil { return nil, err } @@ -73,7 +90,11 @@ func (c *Session) ShellWrite(sessionId string, InputCommand string) (map[string] } func (c *Session) MeterpreterWrite(sessionId string, ps string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"session.meterpreter_write", sessionId, ps}) + return c.MeterpreterWriteContext(context.Background(), sessionId, ps) +} + +func (c *Session) MeterpreterWriteContext(ctx context.Context, sessionId string, ps string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"session.meterpreter_write", sessionId, ps}) if err != nil { return nil, err } @@ -87,7 +108,11 @@ func (c *Session) MeterpreterWrite(sessionId string, ps string) (map[string]inte } func (c *Session) MeterpreterRead(sessionId string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"session.meterpreter_read", sessionId}) + return c.MeterpreterReadContext(context.Background(), sessionId) +} + +func (c *Session) MeterpreterReadContext(ctx context.Context, sessionId string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"session.meterpreter_read", sessionId}) if err != nil { return nil, err } @@ -101,7 +126,11 @@ func (c *Session) MeterpreterRead(sessionId string) (map[string]interface{}, err } func (c *Session) MeterpreterRunSingle(sessionId string, ps string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"session.meterpreter_run_single", sessionId, ps}) + return c.MeterpreterRunSingleContext(context.Background(), sessionId, ps) +} + +func (c *Session) MeterpreterRunSingleContext(ctx context.Context, sessionId string, ps string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"session.meterpreter_run_single", sessionId, ps}) if err != nil { return nil, err } @@ -115,7 +144,11 @@ func (c *Session) MeterpreterRunSingle(sessionId string, ps string) (map[string] } func (c *Session) MeterpreterScript(sessionId string, scriptName string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"session.meterpreter_script", sessionId, scriptName}) + return c.MeterpreterScriptContext(context.Background(), sessionId, scriptName) +} + +func (c *Session) MeterpreterScriptContext(ctx context.Context, sessionId string, scriptName string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"session.meterpreter_script", sessionId, scriptName}) if err != nil { return nil, err } @@ -129,7 +162,11 @@ func (c *Session) MeterpreterScript(sessionId string, scriptName string) (map[st } func (c *Session) MeterpreterSessionDetach(sessionId string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"session.meterpreter_session_detach", sessionId}) + return c.MeterpreterSessionDetachContext(context.Background(), sessionId) +} + +func (c *Session) MeterpreterSessionDetachContext(ctx context.Context, sessionId string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"session.meterpreter_session_detach", sessionId}) if err != nil { return nil, err } @@ -143,7 +180,11 @@ func (c *Session) MeterpreterSessionDetach(sessionId string) (map[string]interfa } func (c *Session) MeterpreterTabs(sessionId string, InputLine string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"session.meterpreter_tabs", sessionId, InputLine}) + return c.MeterpreterTabsContext(context.Background(), sessionId, InputLine) +} + +func (c *Session) MeterpreterTabsContext(ctx context.Context, sessionId string, InputLine string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"session.meterpreter_tabs", sessionId, InputLine}) if err != nil { return nil, err } @@ -157,7 +198,11 @@ func (c *Session) MeterpreterTabs(sessionId string, InputLine string) (map[strin } func (c *Session) CompatibleModules(sessionId string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"session.compatible_modules", sessionId}) + return c.CompatibleModulesContext(context.Background(), sessionId) +} + +func (c *Session) CompatibleModulesContext(ctx context.Context, sessionId string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"session.compatible_modules", sessionId}) if err != nil { return nil, err } @@ -171,7 +216,11 @@ func (c *Session) CompatibleModules(sessionId string) (map[string]interface{}, e } func (c *Session) ShellUpgrade(sessionId string, IpAddress string, Port string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"session.shell_upgrade", sessionId, IpAddress, Port}) + return c.ShellUpgradeContext(context.Background(), sessionId, IpAddress, Port) +} + +func (c *Session) ShellUpgradeContext(ctx context.Context, sessionId string, IpAddress string, Port string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"session.shell_upgrade", sessionId, IpAddress, Port}) if err != nil { return nil, err } @@ -185,7 +234,11 @@ func (c *Session) ShellUpgrade(sessionId string, IpAddress string, Port string) } func (c *Session) RingClear(sessionId string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"session.ring_clear", sessionId}) + return c.RingClearContext(context.Background(), sessionId) +} + +func (c *Session) RingClearContext(ctx context.Context, sessionId string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"session.ring_clear", sessionId}) if err != nil { return nil, err } @@ -199,7 +252,11 @@ func (c *Session) RingClear(sessionId string) (map[string]interface{}, error) { } func (c *Session) RingLast(sessionId string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"session.ring_last", sessionId}) + return c.RingLastContext(context.Background(), sessionId) +} + +func (c *Session) RingLastContext(ctx context.Context, sessionId string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"session.ring_last", sessionId}) if err != nil { return nil, err } @@ -213,7 +270,11 @@ func (c *Session) RingLast(sessionId string) (map[string]interface{}, error) { } func (c *Session) RingPut(sessionId string, InputCommand string) (map[string]interface{}, error) { - resp, err := c.AuthenticatedRequest([]interface{}{"session.ring_put", sessionId, InputCommand}) + return c.RingPutContext(context.Background(), sessionId, InputCommand) +} + +func (c *Session) RingPutContext(ctx context.Context, sessionId string, InputCommand string) (map[string]interface{}, error) { + resp, err := c.AuthenticatedRequestContext(ctx, []interface{}{"session.ring_put", sessionId, InputCommand}) if err != nil { return nil, err }