<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    如何消除VeraCode檢測中的OS Command Injection Issue(CWE ID 78)

    Veracode是一個(gè)檢測應(yīng)用程序是否存在安全漏洞的工具,更多細(xì)節(jié)請(qǐng)?jiān)L問http://www.veracode.com

    這里主要總結(jié)一下如何消除Veracode檢測結(jié)果中的OS Command Injection issue(CWE ID 78)。

    首先,先看看VeraCode對(duì)OS Command Injection的定義:
    OS command injection vulnerabilities occur when data enters an application from an untrusted source and is used to
    dynamically construct and execute a system command.  This allows an attacker to either alter the command executed by the application or append additional commands.  The command is typically executed with the privileges of the executing process and gives an attacker a privilege or capability that he would not otherwise have.

    再看卡VeraCode對(duì)如何解決這個(gè)問題的建議:
    Careful handling of all untrusted data is critical in preventing OS command injection attacks.   Using one or more of the following techniques provides defense-in-depth and minimizes the likelihood of an vulnerability.
    * If possible, use library calls rather than external processes to recreate the desired functionality.
    * Validate user-supplied input using positive filters (white lists) to ensure that it conforms to the expected format, using centralized data validation routines when possible.
    * Select safe API routines.  Some APIs that execute system commands take an array of strings as input rather than a
    single string, which protects against some forms of command injection by ensuring that a user-supplied argument
    cannot be interpreted as part of the command.
    通過對(duì)現(xiàn)有系統(tǒng)的實(shí)踐證明,對(duì)于這類OS Command Injection Issue,消除時(shí)主要遵循以下幾個(gè)原則:

    1)重構(gòu)代碼,保證只有1個(gè)函數(shù)最終執(zhí)行Runtime.exec(xxxx,xxx)
        public static Process execAndReturnProcess(String cmd[]) throws Exception
        {
            
    return execAndReturnProcess(cmd, null);
        }
       
        public static Process execAndReturnProcess(String cmds[], String envps[]) throws Exception
        {
            String[] validatedCmdArray 
    = ASEUtils.validateCommandArray(cmds);
            String[] validatedEnvArray 
    = ASEUtils.validateEnvArray(envps);
            
            
    if (null == validatedCmdArray)
            {
                
    throw new Exception("No permission to execute the command:" + cmds[0]);
            }
            
            Runtime runtime 
    = Runtime.getRuntime();
            Process process 
    = runtime.exec(validatedCmdArray, validatedEnvArray);
            
    return process;
        }

    2)用Runtime.exec調(diào)用操作系統(tǒng)命令時(shí),優(yōu)先使用數(shù)組作為參數(shù)
    Process exec(String[] cmdarray, String[] envp, File dir)
    而不是字符串作為參數(shù)
    Process exec(String cmd, String[] envp, File dir)

    3)Veracode會(huì)檢測傳入exec()的變量是否存在隱患(比如文件中讀取出來的,或者注冊(cè)表里讀取出來的),這種情況就需要對(duì)原有變量做驗(yàn)證,然后重新定義變量,傳入Runtime.exec中,如下面代碼所示:
    public static Process execAndReturnProcess(String cmds[], String envps[]) throws Exception
        {
            String[] validatedCmdArray 
    = validateCommandArray(cmds);
            String[] validatedEnvArray 
    = validateEnvArray(envps);
            
            
    if (null == validatedCmdArray)
            {
                
    throw new Exception("No permission to execute the command:" + cmds[0]);
            }
            
            Runtime runtime 
    = Runtime.getRuntime();
            Process process 
    = runtime.exec(validatedCmdArray, validatedEnvArray);
            
    return process;
        }    
        
        
    public static String[] validateCommandArray(String[] cmdArray)
        {
            String[] validatedCmdArray = new String[cmdArray.length];
            
            
    for (int i=0; i<cmdArray.length; i++)
            {
                
    if ( null != cmdArray[i] && cmdArray[i].trim().length()>0)
                {
                    validatedCmdArray[i] 
    = removeControlCharacter(cmdArray[i]);
                }
            }
            
    return validatedCmdArray;
        }

    4)另外,還可以定義一個(gè)全局的可執(zhí)行命令的列表(White List),對(duì)每次要執(zhí)行的命令,都驗(yàn)證它是否在允許的可執(zhí)行命令列表里。

    public static final String [] ALLOWED_COMMAND_ROUTINES =
        {
            
    "cmd",        
            
    "command",    
            
    "sh",         
            
    "env",
        };
        
        
    private static boolean isValidateCommandRoutine(String command)
        {
            Boolean isValidRoutine 
    = false;
            
    for (int i=0; i<ALLOWED_COMMAND_ROUTINES.length ;i++)
            {
                
    if (command.equals(ALLOWED_COMMAND_ROUTINES[i]) != -1)
                {
                    isValidRoutine 
    = true;
                }
            }
            
    return isValidRoutine;
        }
        
        
    public static String[] validateCommandArray(String cmds[])
        {
            Boolean isValidRoutine 
    = isValidateCommandRoutine(cmds[0]);
            
    if (isValidRoutine)
            {
                String[] validatedCmdArray 
    = new String[cmds.length];
                
    for (int i=0; i<cmds.length; i++)
                {
                    
    if ( null != cmds[i] && cmds[i].trim().length()>0)
                    {
                        validatedCmdArray[i] 
    = removeControlCharacter(cmds[i]);
                    }
                }
                
    return validatedCmdArray;
            }
            
    return null;
        }

    當(dāng)然,如果第三方檢測程序始終認(rèn)為最后的調(diào)用 Runtime.exec(xxx,xx)存在隱患,則可以采用它們提供的注釋或者標(biāo)記等其他方法消除最終的調(diào)用入口。

    實(shí)際上,我們?cè)谧龅谌桨踩珯z測時(shí),使用上面提到的4點(diǎn),Veracode 已經(jīng)可以通過檢測了,但是Fortify不行,所以最后只能在Fortify的系統(tǒng)里標(biāo)記"Not an issue", 忽略這個(gè)最終的Runtime.exec調(diào)用。

    posted on 2011-09-06 10:28 想飛就飛 閱讀(3682) 評(píng)論(0)  編輯  收藏 所屬分類: J2EE 、開發(fā)工具&環(huán)境

    公告


    導(dǎo)航

    <2011年9月>
    28293031123
    45678910
    11121314151617
    18192021222324
    2526272829301
    2345678

    統(tǒng)計(jì)

    常用鏈接

    留言簿(13)

    我參與的團(tuán)隊(duì)

    隨筆分類(69)

    隨筆檔案(68)

    最新隨筆

    搜索

    積分與排名

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 久久精品国产亚洲AV麻豆~| 中文字幕在线视频免费观看| 婷婷久久久亚洲欧洲日产国码AV | 亚洲成AV人网址| 免费看h片的网站| 免费的全黄一级录像带| 日韩在线观看视频免费| 亚洲av色香蕉一区二区三区| 亚洲无成人网77777| 亚洲AV无码乱码在线观看富二代 | 亚洲熟妇无码AV不卡在线播放| 久久久久久久亚洲Av无码| 国产亚洲综合色就色| 亚洲国产精品一区二区第四页 | 亚洲大码熟女在线观看| 久久精品国产99国产精品亚洲| 亚洲人成网站影音先锋播放| 亚洲午夜久久久影院伊人| 亚洲乱码国产一区网址| 免费成人av电影| 四虎国产精品免费久久影院| 黄网址在线永久免费观看| 成年女人18级毛片毛片免费| 中文字幕影片免费在线观看| 99久久99热精品免费观看国产| 国产无遮挡无码视频免费软件| 久久久久久噜噜精品免费直播 | 在线观看亚洲精品福利片| 亚洲欧洲日产国码高潮αv| 亚洲国产精品日韩| 亚洲一级Av无码毛片久久精品| 大胆亚洲人体视频| 亚洲综合最新无码专区| 亚洲性日韩精品一区二区三区| 中文字幕第13亚洲另类| 国产亚洲情侣一区二区无码AV| 中文字幕专区在线亚洲| 国产AV无码专区亚洲AV手机麻豆 | 182tv免费视频在线观看| 久久免费福利视频| 国产精品色拉拉免费看|