my_cobalt_strike/console.go
2022-07-18 18:38:06 +08:00

212 lines
4.1 KiB
Go

package main
import (
"bufio"
"errors"
"fmt"
"io"
"net"
"os"
"os/exec"
"strconv"
"strings"
)
func main() {
//print banner
fmt.Println(" ____ ____ ____ _ ")
fmt.Println(" / ___/ ___| / ___|___ _ __ ___ ___ | | ___ ")
fmt.Println("| | \\___ \\| | / _ \\| _ \\/ __|/ _ \\| |/ _ \\")
fmt.Println("| |___ ___) | |__| (_) | | | \\__ \\ (_) | | __/")
fmt.Println(" \\____|____/ \\____\\___/|_| |_|___/\\___/|_|\\___|")
console()
//listener("tcp", 4444)
//dial()
}
type env struct {
lport int
rhost int
rport int
}
var env1 env
// listener function
func listener(network string, port int) {
// Create a listener
strport := strconv.Itoa(port)
listener, err := net.Listen("tcp", ":"+strport)
if err != nil {
fmt.Println("err = ", err)
return
}
defer listener.Close()
//阻塞等待用户链接
conn, err := listener.Accept()
if err != nil {
fmt.Println("err = ", err)
return
}
defer conn.Close() //关闭当前用户链接
//接收用户的请求
// for {
// buf := make([]byte, 1024) //1024大小的缓冲区
// n, err1 := conn.Read(buf)
// if err1 != nil {
// fmt.Println("err1 = ", err1)
// return
// }
// fmt.Println("buf = ", string(buf[:n]))
// }
go func() {
buf := make([]byte, 1024)
for {
n, err := conn.Read(buf)
if err != nil {
if err == io.EOF {
return
}
panic(err)
}
fmt.Printf("received: %v", string(buf[:n]))
}
}()
// 客户端可以输入消息并发送到服务端
for {
//var inp string
//fmt.Scanln(&inp)
//conn.Write([]byte(inp + "\n"))
}
}
// 控制台函数
func console() {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("CSConsole > ")
// Read the keyboad input.
input, err := reader.ReadString('\n')
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
// Handle the execution of the input.
if err = execInput(input); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}
// ErrNoPath is returned when 'cd' was called without a second argument.
var ErrNoPath = errors.New("path required")
var ErrNoSet = errors.New("variaty name required")
var ErrNoVar = errors.New("variaty name wrong")
func execInput(input string) error {
// Remove the newline character.
input = strings.TrimSuffix(input, "\n")
// Split the input separate the command and the arguments.
args := strings.Split(input, " ")
// Check for built-in commands.
switch args[0] {
case "help":
fmt.Print("use show to show options")
fmt.Print("use set to set varieties")
fmt.Print("use ")
return nil
case "cd":
// 'cd' to home with empty path not yet supported.
if len(args) < 2 {
return ErrNoPath
}
// Change the directory and return the error.
return os.Chdir(args[1])
case "set":
if len(args) < 3 {
return ErrNoSet
}
switch args[1] {
case "lport":
env1.lport, _ = strconv.Atoi(args[2])
return nil
case "rhost":
env1.rhost, _ = strconv.Atoi(args[2])
return nil
case "rport":
env1.rport, _ = strconv.Atoi(args[2])
return nil
default:
return ErrNoVar
}
case "show":
fmt.Printf("Local listening port (lport): %d\n", env1.lport)
fmt.Printf("Remote listening host (rhost): %d\n", env1.rhost)
fmt.Printf("Remote listening port (rport): %d\n", env1.rport)
return nil
case "listen":
listener("tcp", env1.lport)
case "exit":
os.Exit(0)
}
// Prepare the command to execute.
cmd := exec.Command(args[0], args[1:]...)
// Set the correct output device.
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
// Execute the command and return the error.
return cmd.Run()
}
func dial(host string, port int) {
dialaddr := host + ":" + strconv.Itoa(port)
conn, err := net.Dial("tcp", dialaddr)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
defer conn.Close()
// 将读取部分放入到子协程中,不阻塞主协程运行
go func() {
buf := make([]byte, 1024)
for {
n, err := conn.Read(buf)
if err != nil {
if err == io.EOF {
return
}
panic(err)
}
fmt.Printf("received: %v", string(buf[:n]))
}
}()
// 客户端可以输入消息并发送到服务端
for {
var inp string
fmt.Scanln(&inp)
conn.Write([]byte(inp + "\n"))
}
}