簡體   English   中英

linux如何修補此代碼

[英]linux how to patch this code

#include <WhatHere?>
#include <WhatHere?>
#include <WhatHere?>
int main(int argc, char **argv) {
    char command[50] = "echo ";
    strcat(command,argv[1]); // concatenate the input so that the final command is "echo <input>"
    system(command); // call the system() function to print the input
    return 0; // denote that the program has finished executing successfully
}

我們可以通過運行此代碼獲得遠程訪問嗎? 我知道有可能,但請幫助我進行修補。

假設您擔心潛在的緩沖區溢出,可以像這樣修復它:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (int argc, char **argv) {
    char *command;
    if (argc != 2) {
        fprintf (stderr, "Wrong number of arguments\n");
        return 1;
    }
    if ((command = malloc (strlen (argv[1]) + 6)) == NULL) {
        fprintf (stderr, "Could not allocate memory\n");
        return 1;
    }
    strcpy (command, "echo ");
    strcat(command,argv[1]);
    system(command);
    free (command);
    return 0;
}

這為"echo " (5), argv[1] (字符串長度)和空終止符(1) argv[1]足夠的空間。

允許運行用戶指定的內容仍然很危險,但是至少您不會再有緩沖區溢出了。

Paxdiablo為您的緩沖區溢出問題提供了一個很好的解決方案,但這實際上是您遇到的最少問題。 您的大問題是,您在不首先檢查用戶輸入的情況下就盲目使用用戶輸入。

例如,運行程序如下:

./your_app "\"goodbye data\" && rm -rf /"

即使您的程序沒有緩沖區溢出問題,也將以災難告終。 攻擊者可以很容易地傳入執行各種令人討厭的事情的整個shell腳本,他們所要做的就是將其重新編寫以適合一行。

您需要檢查傳入的用戶輸入,然后再將其傳遞給system()並確保它看起來像您期望的那樣。 更好的是,避免完全將system()與用戶輸入一起使用,而應使用更安全的方法來完成您需要的操作(在您的示例中,可以將對system("echo ...")調用替換為printf() )。 如果您絕對必須將用戶輸入傳遞給system() ,請考慮在受限的環境(例如chroot監獄system()運行您的應用程序,以至少使做討厭的事情變得更加困難。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM