Skip to main content
1 de 3
byteman
  • 802
  • 1
  • 7
  • 12

É possível sim ser admin, há esse gist de Jeremy Black

Para ver o resultado do commando, adicione .CombinedOutput() ao final de exec.Command(args).CombineOutput()

package main

import (
    "fmt"
    "os"
    "os/exec"
    "strings"
    "syscall"

    "time"

    "golang.org/x/sys/windows"
)

func main() {
    if !amAdmin() {
        runMeElevated()
    } else {
        res, _ := exec.Command(`cmd`, `/c`, `CheckNetIsolation.exe`, `LoopbackExempt`, `-a`, `-n=....`).CombinedOutput()
        fmt.Println(string(res))
        time.Sleep(5 * time.Second)
    }
}

func runMeElevated() {
    verb := "runas"
    exe, _ := os.Executable()
    cwd, _ := os.Getwd()
    args := strings.Join(os.Args[1:], " ")

    verbPtr, _ := syscall.UTF16PtrFromString(verb)
    exePtr, _ := syscall.UTF16PtrFromString(exe)
    cwdPtr, _ := syscall.UTF16PtrFromString(cwd)
    argPtr, _ := syscall.UTF16PtrFromString(args)

    var showCmd int32 = 1 //SW_NORMAL

    err := windows.ShellExecute(0, verbPtr, exePtr, argPtr, cwdPtr, showCmd)
    if err != nil {
        fmt.Println(err)
    }
}

func amAdmin() bool {
    _, err := os.Open("\\\\.\\PHYSICALDRIVE0")
    if err != nil {
        fmt.Println("admin no")
        return false
    }
    fmt.Println("admin yes")
    return true
}
byteman
  • 802
  • 1
  • 7
  • 12