<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是一個檢測應用程序是否存在安全漏洞的工具,更多細節請訪問http://www.veracode.com

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

    首先,先看看VeraCode對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對如何解決這個問題的建議:
    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.
    通過對現有系統的實踐證明,對于這類OS Command Injection Issue,消除時主要遵循以下幾個原則:

    1)重構代碼,保證只有1個函數最終執行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調用操作系統命令時,優先使用數組作為參數
    Process exec(String[] cmdarray, String[] envp, File dir)
    而不是字符串作為參數
    Process exec(String cmd, String[] envp, File dir)

    3)Veracode會檢測傳入exec()的變量是否存在隱患(比如文件中讀取出來的,或者注冊表里讀取出來的),這種情況就需要對原有變量做驗證,然后重新定義變量,傳入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)另外,還可以定義一個全局的可執行命令的列表(White List),對每次要執行的命令,都驗證它是否在允許的可執行命令列表里。

    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;
        }

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

    實際上,我們在做第三方安全檢測時,使用上面提到的4點,Veracode 已經可以通過檢測了,但是Fortify不行,所以最后只能在Fortify的系統里標記"Not an issue", 忽略這個最終的Runtime.exec調用。

    posted on 2011-09-06 10:28 想飛就飛 閱讀(3664) 評論(0)  編輯  收藏 所屬分類: J2EE 、開發工具&環境

    公告


    導航

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

    統計

    常用鏈接

    留言簿(13)

    我參與的團隊

    隨筆分類(69)

    隨筆檔案(68)

    最新隨筆

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲码一区二区三区| 亚洲精品无码中文久久字幕| 曰批全过程免费视频免费看| 女性自慰aⅴ片高清免费| 激情五月亚洲色图| 国产成在线观看免费视频| 亚洲人成黄网在线观看| 无遮挡国产高潮视频免费观看 | 污视频网站在线观看免费| 国产一级淫片视频免费看| 爱爱帝国亚洲一区二区三区| 国产成人高清精品免费软件 | 亚洲国产a∨无码中文777| 免费观看男人吊女人视频| 亚洲欧洲日产国码久在线观看| 久久久国产亚洲精品| 色www永久免费视频| 特级无码毛片免费视频| 亚洲线精品一区二区三区| 97久久免费视频| 亚洲综合精品伊人久久| 国产一区二区三区免费看| 亚洲一区二区三区免费| 亚洲av日韩av高潮潮喷无码| 免费成人激情视频| 青草青草视频2免费观看| 亚洲va中文字幕无码久久不卡| 亚洲成av人片在www鸭子| 无码区日韩特区永久免费系列| 亚洲人成人网站18禁| 亚洲欧洲精品成人久久奇米网 | 久久亚洲AV成人无码软件| 中文字幕不卡免费视频| 日本免费一区尤物| 久草免费福利在线| 浮力影院亚洲国产第一页| 91九色老熟女免费资源站| 日韩亚洲人成网站| 亚洲国产一区二区三区青草影视| 在线播放免费人成视频在线观看| 国产精品小视频免费无限app|