Skip to content

Commit 0afbd2e

Browse files
Added ParseCommand (#404)
* added test and parsecommand
1 parent 30a7ff9 commit 0afbd2e

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

transform/parsing.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package transform
2+
3+
import (
4+
"strings"
5+
6+
"github.com/vulncheck-oss/go-exploit/output"
7+
)
8+
9+
// Provided a command param that includes args such as "cmd.exe /c whoami" as one might receive from a a user provided flag like
10+
// -command , this function would return "cmd.exe", "/c whoami", true.
11+
//
12+
// program, args, ok := transform.ParseCommand(command)
13+
func ParseCommand(command string) (string, string, bool) {
14+
sCommand := strings.SplitN(command, " ", 2)
15+
if sCommand == nil {
16+
output.PrintFrameworkError("Invalid command provided, should be space-separated with program and then args in front. examples: cmd.exe /c someargs or bash -c someargs")
17+
18+
return "", "", false
19+
}
20+
21+
if len(sCommand) != 2 {
22+
output.PrintFrameworkError("Invalid command provided, should be space-separated with program and then args in front. examples: cmd.exe /c someargs or bash -c someargs")
23+
24+
return "", "", false
25+
}
26+
27+
return sCommand[0], sCommand[1], true
28+
}

transform/parsing_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package transform
2+
3+
import "testing"
4+
5+
func TestParseCommand(t *testing.T) {
6+
program, args, ok := ParseCommand("cmd.exe /c calc")
7+
if !ok {
8+
t.Fatal("Failed to parse command")
9+
}
10+
11+
if program != "cmd.exe" || args != "/c calc" {
12+
t.Fatalf("Program or args returned unexpected values: %s %s", program, args)
13+
}
14+
15+
t.Log(program + " " + args)
16+
}

0 commit comments

Comments
 (0)