Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide command prompt window when using Exec in Golang?

say i have the following code, using syscall to hide command line window

process := exec.Command(name, args...)
process.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
err := process.Start()
if err != nil {
    log.Print(err)
}

but when i compiled it and tried to run it in Windows, command line window showed up again

what can i do to prevent command line window from appearing?

PS i already know how to compile golang source into a Windows GUI executable using go build -ldflags -H=windowsgui, but doing so only ensures the program itself doesn't bring up a command line window, Exec will show those windows anyway

like image 955
jm33_m0 Avatar asked Feb 28 '17 05:02

jm33_m0


People also ask

How do I run a shell program in Golang?

Go exec command tutorial shows how to execute shell commands and programs in Golang. The Run function starts the specified command and waits for it to complete, while the Start starts the specified command but does not wait for it to complete; we need to use Wait with Start . The os/exec package runs external commands.

What is exec and exec hide command in PowerShell?

The exec and hide commands are used to execute the script and hide any console windows from opening. Include elevatecmd to request administrator privileges for the batch file although it’s only needed if you know commands in your script require elevation. nircmd elevatecmd exec hide [path to.bat file]

What is exec command in Go language?

Using exec in GoLang Exec is a subpackage in os package. It can be used to run external commands using Go. This post will provide some examples of how to get started using it.

How do I hide the console window in a batch script?

When using a method to hide the console window make sure the batch script contains no commands that are likely to stop the script before it exits, such as pause or requiring user input like a Yes/No response.


1 Answers

There is a better solution, which can run exec.Command() without spawn a visible window, ( ͡° ͜ʖ ͡°).

Here is my code:

Firstly import "syscall"

cmd_path := "C:\\Windows\\system32\\cmd.exe"
cmd_instance := exec.Command(cmd_path, "/c", "notepad")
cmd_instance.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
cmd_output, err := cmd_instance.Output()

Origin: https://www.reddit.com/r/golang/comments/2c1g3x/build_golang_app_reverse_shell_to_run_in_windows/

like image 71
Yu Chen Avatar answered Oct 02 '22 14:10

Yu Chen