Skip to content
Merged
Show file tree
Hide file tree
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
23 changes: 12 additions & 11 deletions cmd/cmd2app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"context"
"fmt"
"go/ast"
"go/parser"
Expand All @@ -13,7 +14,7 @@ import (
"strings"
"text/template"

"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

// SubcommandInfo holds the extracted details for a single subcommand.
Expand Down Expand Up @@ -288,7 +289,7 @@ func buildSubcommandApp(info SubcommandInfo, outputDir, sourceModulePath string)
}

func main() {
app := &cli.App{
app := &cli.Command{
Name: "cmd2app",
Usage: `Generates separate, size-optimized executables for each subcommand of a given urfave/cli.Command, e.g.:
cmd2app github.com/ctrsploit/ctrsploit/cmd/ctrsploit/vul.Command
Expand All @@ -301,26 +302,26 @@ cmd2app github.com/ctrsploit/ctrsploit/cmd/ctrsploit/vul.Command
Usage: "Specify the output directory for the binaries.",
},
},
Action: func(c *cli.Context) error {
if c.Args().Len() != 1 {
Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Len() != 1 {
return cli.Exit("Error: requires one argument.\nUsage: cmd2app <package.VarName>", 1)
}
fullCmdPath := c.Args().First()
fullCmdPath := cmd.Args().First()
lastDotIndex := strings.LastIndex(fullCmdPath, ".")
if lastDotIndex == -1 {
return cli.Exit(fmt.Sprintf("Error: invalid command path '%s'. Format should be <package.VarName>", fullCmdPath), 1)
}
pkgPath := fullCmdPath[:lastDotIndex]
varName := fullCmdPath[lastDotIndex+1:]

outputDir := c.String("output-dir")
outputDir := cmd.String("output-dir")

// Get the directory of the source package to use as the module path for replace directive
cmd := exec.Command("go", "list", "-f", "{{.Module.Dir}}", pkgPath)
goListCmd := exec.Command("go", "list", "-f", "{{.Module.Dir}}", pkgPath)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
goListCmd.Stdout = &stdout
goListCmd.Stderr = &stderr
if err := goListCmd.Run(); err != nil {
return cli.Exit(fmt.Sprintf("Error: failed to get module directory for package '%s': %v\n%s", pkgPath, err, stderr.String()), 1)
}
sourceModulePath := strings.TrimSpace(stdout.String())
Expand Down Expand Up @@ -361,7 +362,7 @@ cmd2app github.com/ctrsploit/ctrsploit/cmd/ctrsploit/vul.Command
return nil
},
}
if err := app.Run(os.Args); err != nil {
if err := app.Run(context.Background(), os.Args); err != nil {
log.Fatal(err)
}
}
6 changes: 3 additions & 3 deletions cmd/cmd2app/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import (
"os"

subcmd_pkg "{{.PkgPath}}"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

func main() {
// The target variable (e.g., auto.Command) is already a pointer to cli.Command.
targetCommand := subcmd_pkg.{{.VarName}}

app := &cli.App{
app := &cli.Command{
Name: targetCommand.Name,
Usage: targetCommand.Usage,
Flags: targetCommand.Flags,
Action: targetCommand.Action,
Commands: targetCommand.Subcommands, // Also include sub-subcommands if any
Commands: targetCommand.Commands, // Also include sub-subcommands if any
}

if err := app.Run(os.Args); err != nil {
Expand Down
16 changes: 9 additions & 7 deletions env/upload.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
package env

import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/ctrsploit/sploit-spec/pkg/env/linux"
"github.com/ctrsploit/sploit-spec/pkg/upload"
"github.com/urfave/cli/v2"
"time"
"github.com/urfave/cli/v3"
)

var LinuxEnv linux.Env

func UploadAction(context *cli.Context) (err error) {
func UploadAction(context context.Context, cmd *cli.Command) (err error) {
//eg. ECS
servicename := context.Args().Get(0)
servicename := cmd.Args().Get(0)
// region_tag.json eg. cn-north4_linux.json
filename := context.Args().Get(1)
filename := cmd.Args().Get(1)
// obsurl
obsurl := context.Args().Get(2)
obsurl := cmd.Args().Get(2)
// obshost (if want to hide obs upload behavior), put your real obsurl in here, put the fake url in obsurl
obshost := context.Args().Get(3)
obshost := cmd.Args().Get(3)
if servicename == "" {
return
}
Expand Down
2 changes: 1 addition & 1 deletion example/xsploit/Dockerfile_dev
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# syntax=docker/dockerfile:1

ARG GO_VERSION=1.21.4
ARG GO_VERSION=1.22
ARG BASE_DEBIAN_DISTRO="bullseye"
ARG GOLANG_IMAGE="golang:${GO_VERSION}-${BASE_DEBIAN_DISTRO}"

Expand Down
3 changes: 2 additions & 1 deletion example/xsploit/cmd/checksec/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"os"
"xsploit/cmd/xsploit/checksec"

Expand All @@ -16,7 +17,7 @@ func main() {
sploit := app.Command2App(checksec.Command)
sploit.Name = name
app.InstallGlobalFlags(sploit)
err := sploit.Run(os.Args)
err := sploit.Run(context.Background(), os.Args)
if err != nil {
awesome_error.CheckFatal(err)
}
Expand Down
6 changes: 4 additions & 2 deletions example/xsploit/cmd/xsploit/auto/auto.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package auto

import (
"context"
"fmt"
"github.com/urfave/cli/v2"

"github.com/urfave/cli/v3"
)

const (
Expand All @@ -14,7 +16,7 @@ var (
Name: CommandNameAuto,
Usage: "auto gathering information, detect vulnerabilities and run exploits",
Aliases: []string{"a"},
Action: func(context *cli.Context) (err error) {
Action: func(context.Context, *cli.Command) (err error) {
fmt.Println("TODO")
return
},
Expand Down
9 changes: 5 additions & 4 deletions example/xsploit/cmd/xsploit/checksec/auto.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package checksec

import (
"xsploit/vul/cve-2099-9999"
"context"
cve_2099_9999 "xsploit/vul/cve-2099-9999"

"github.com/ctrsploit/sploit-spec/pkg/vul"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

const (
Expand All @@ -16,11 +17,11 @@ var (
Name: CommandNameAuto,
Usage: "auto",
Aliases: []string{"a"},
Action: func(context *cli.Context) (err error) {
Action: func(ctx context.Context, cmd *cli.Command) (err error) {
vulnerabilities := vul.Vulnerabilities{
&cve_2099_9999.Vul,
}
err = vulnerabilities.Check(context)
err = vulnerabilities.Check(cmd)
if err != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions example/xsploit/cmd/xsploit/checksec/checksec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package checksec
import (
"xsploit/vul/cve-2099-9999"

"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

var Command = &cli.Command{
Name: "checksec",
Aliases: []string{"c"},
Usage: "check security inside a container",
Subcommands: []*cli.Command{
Commands: []*cli.Command{
Auto,
cve_2099_9999.CheckSecCmd,
},
Expand Down
6 changes: 4 additions & 2 deletions example/xsploit/cmd/xsploit/env/auto.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package env

import (
"github.com/urfave/cli/v2"
"context"
"xsploit/env/auto"

"github.com/urfave/cli/v3"
)

const (
Expand All @@ -13,7 +15,7 @@ var (
Auto = &cli.Command{
Name: CommandNameAuto,
Usage: "auto",
Action: func(context *cli.Context) (err error) {
Action: func(context.Context, *cli.Command) (err error) {
auto.Print()
return
},
Expand Down
4 changes: 2 additions & 2 deletions example/xsploit/cmd/xsploit/env/env.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package env

import "github.com/urfave/cli/v2"
import "github.com/urfave/cli/v3"

var Command = &cli.Command{
Name: "env",
Aliases: []string{"e"},
Usage: "Collect information",
Subcommands: []*cli.Command{
Commands: []*cli.Command{
Auto,
Second,
Minute,
Expand Down
8 changes: 5 additions & 3 deletions example/xsploit/cmd/xsploit/env/minute.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package env

import (
"github.com/ctrsploit/sploit-spec/pkg/log"
"github.com/urfave/cli/v2"
"context"
"xsploit/env/minute"

"github.com/ctrsploit/sploit-spec/pkg/log"
"github.com/urfave/cli/v3"
)

var Minute = &cli.Command{
Name: "minute",
Aliases: []string{"m"},
Usage: "show the minute info",
Action: func(context *cli.Context) (err error) {
Action: func(context.Context, *cli.Command) (err error) {
log.Logger.Debug("")
minute.Print()
return
Expand Down
8 changes: 5 additions & 3 deletions example/xsploit/cmd/xsploit/env/second.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package env

import (
"github.com/ctrsploit/sploit-spec/pkg/log"
"github.com/urfave/cli/v2"
"context"
"xsploit/env/second"

"github.com/ctrsploit/sploit-spec/pkg/log"
"github.com/urfave/cli/v3"
)

var Second = &cli.Command{
Name: "second",
Aliases: []string{"s"},
Usage: "show the second info",
Action: func(context *cli.Context) (err error) {
Action: func(context.Context, *cli.Command) (err error) {
log.Logger.Debug("")
second.Print()
return
Expand Down
3 changes: 2 additions & 1 deletion example/xsploit/cmd/xsploit/env/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package env

import (
"encoding/json"
"github.com/ctrsploit/sploit-spec/pkg/upload"
"xsploit/env/auto"

"github.com/ctrsploit/sploit-spec/pkg/upload"
)

var (
Expand Down
6 changes: 4 additions & 2 deletions example/xsploit/cmd/xsploit/exploit/auto.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package exploit

import (
"context"
"fmt"
"github.com/urfave/cli/v2"

"github.com/urfave/cli/v3"
)

const (
Expand All @@ -13,7 +15,7 @@ var (
Auto = &cli.Command{
Name: CommandNameAuto,
Usage: "auto",
Action: func(context *cli.Context) (err error) {
Action: func(context.Context, *cli.Command) (err error) {
fmt.Println("TODO")
return
},
Expand Down
4 changes: 2 additions & 2 deletions example/xsploit/cmd/xsploit/exploit/exploit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package exploit
import (
"xsploit/vul/cve-2099-9999"

"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

var Command = &cli.Command{
Name: "exploit",
Aliases: []string{"x"},
Usage: "run a exploit",
Subcommands: []*cli.Command{
Commands: []*cli.Command{
Auto,
cve_2099_9999.ExploitCmd,
},
Expand Down
4 changes: 2 additions & 2 deletions example/xsploit/cmd/xsploit/vul/vul.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ package vul
import (
cve_2099_9999 "xsploit/vul/cve-2099-9999"

"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

var Command = &cli.Command{
Name: "vul",
Aliases: []string{"v"},
Usage: "list vulnerabilities",
Subcommands: []*cli.Command{
Commands: []*cli.Command{
cve_2099_9999.VulCmd,
},
}
7 changes: 4 additions & 3 deletions example/xsploit/cmd/xsploit/xsploit.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"os"
"xsploit/cmd/xsploit/auto"
"xsploit/cmd/xsploit/checksec"
Expand All @@ -11,7 +12,7 @@ import (
"github.com/ctrsploit/sploit-spec/pkg/app"
spec_version "github.com/ctrsploit/sploit-spec/pkg/spec-version"
"github.com/ctrsploit/sploit-spec/pkg/version"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

const usage = `An example sploit tool follows sploit-spec`
Expand All @@ -21,7 +22,7 @@ func init() {
}

func main() {
sploit := &cli.App{
sploit := &cli.Command{
Name: "xsploit",
Usage: usage,
Commands: []*cli.Command{
Expand All @@ -35,5 +36,5 @@ func main() {
},
}
app.InstallGlobalFlags(sploit)
_ = sploit.Run(os.Args)
_ = sploit.Run(context.Background(), os.Args)
}
Loading