清理/转义进入 SSH 命令的参数

发布于 2024-12-09 05:21:40 字数 947 浏览 0 评论 0原文

我需要做什么才能正确清理/转义正在输入到编程 SSH 命令中的参数?

例如,路径参数 -

public boolean exists(String path) {

    try {
        ChannelExec c = (ChannelExec) session.openChannel("exec");

        //Here *** would like to be sure that the path is completely valid
        c.setCommand("[ -f " + path + " ] && echo \"File exists\" || echo \"File does not exists\"");

        InputStream in = c.getInputStream();

        c.connect();

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        IOUtils.copy(in, out);

        in.close();
        out.close();

        System.out.println(out.toString("UTF-8"));
        c.disconnect();

    } catch (JSchException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // TODO Auto-generated method stub
    return false;
}

不安全的原因是路径参数可能来自用户上传的文件。从技术上讲,恶意用户可以上传带有无效文件名的文件。虽然我可以事先检查这一点(我正在这样做),但我也想在这里检查一下。

What do I need to do to properly sanitize/escape a parameter that is being entered into a programmatic SSH command?

For example, the path parameter -

public boolean exists(String path) {

    try {
        ChannelExec c = (ChannelExec) session.openChannel("exec");

        //Here *** would like to be sure that the path is completely valid
        c.setCommand("[ -f " + path + " ] && echo \"File exists\" || echo \"File does not exists\"");

        InputStream in = c.getInputStream();

        c.connect();

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        IOUtils.copy(in, out);

        in.close();
        out.close();

        System.out.println(out.toString("UTF-8"));
        c.disconnect();

    } catch (JSchException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // TODO Auto-generated method stub
    return false;
}

The reason it is unsafe is that the path parameter can come from a user uploaded file. A malicious user could technically upload a file w/ an invalid filename. Although I can check for this beforehand (which I'm doing) I'd also like to check for it here too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

预谋 2024-12-16 05:21:40

我认为这里的一个好主意是确保它作为单个参数传递给 [,而不是多个参数(甚至多个命令)。因此,只需将其包装在 ' 中,并将字符串内的任何 ' 替换为 '\'' 即可。

private String escape(String s) {
    return "'" + s.replace("'", "'\\''") + "'";
}

您还可以使用 ' 而不是 \" 作为命令的 echo 部分,只要您不需要变量扩展即可服务器端(并且这些字符串中没有变量):(

c.setCommand("[ -f " + escape(path) + " ] && " +
              "echo 'File exists' || echo 'File does not exist'");

请注意,我还做了一个小小的语法修复。)

I think a good idea here would be to make sure it is passed as a single parameter to [, and not multiple ones (or even multiple commands). So simply wrap it in ', and replace any ' inside the string by '\''.

private String escape(String s) {
    return "'" + s.replace("'", "'\\''") + "'";
}

You also can use ' instead of \" for the echo part of the command, as long as you don't need variable expansion on the server side (and there are no variables in these strings):

c.setCommand("[ -f " + escape(path) + " ] && " +
              "echo 'File exists' || echo 'File does not exist'");

(Note that I also did a tiny grammar fix.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文