天天看點

java安全編碼指南之:輸入校驗

簡介

為了保證java程式的安全,任何外部使用者的輸入我們都認為是可能有惡意攻擊意圖,我們需要對所有的使用者輸入都進行一定程度的校驗。

本文将帶領大家探讨一下使用者輸入校驗的一些場景。一起來看看吧。

在字元串标準化之後進行校驗

通常我們在進行字元串校驗的時候需要對一些特殊字元進行過濾,過濾之後再進行字元串的校驗。

我們知道在java中字元是基于Unicode進行編碼的。但是在Unicode中,同一個字元可能有不同的表示形式。是以我們需要對字元進行标準化。

java中有一個專門的類Normalizer來負責處理,字元标準化的問題。

我們看下面一個例子:

    public void testNormalizer(){
        System.out.println(Normalizer.normalize("\u00C1", Normalizer.Form.NFKC));
        System.out.println(Normalizer.normalize("\u0041\u0301", Normalizer.Form.NFKC));
    }      

輸出結果:

Á
Á      

我們可以看到,雖然兩者的Unicode不一樣,但是最終表示的字元是一樣的。是以我們在進行字元驗證的時候,一定要先進行normalize處理。

考慮下面的例子:

    public void falseNormalize(){
        String s = "\uFE64" + "script" + "\uFE65";
        Pattern pattern = Pattern.compile("[<>]"); // 檢查是否有尖括号
        Matcher matcher = pattern.matcher(s);
        if (matcher.find()) {
            throw new IllegalStateException();
        }
        s = Normalizer.normalize(s, Normalizer.Form.NFKC);
    }      

其中\uFE64表示的是

我們需要對代碼進行下面的改動:

    public void trueNormalize(){
        String s = "\uFE64" + "script" + "\uFE65";
        s = Normalizer.normalize(s, Normalizer.Form.NFKC);
        Pattern pattern = Pattern.compile("[<>]"); // 檢查是否有尖括号
        Matcher matcher = pattern.matcher(s);
        if (matcher.find()) {
            throw new IllegalStateException();
        }
    }      

先進行normalize操作,然後再進行字元驗證。

注意不可信字元串的格式化

我們經常會使用到格式化來對字元串進行格式化,在格式化的時候如果格式化字元串裡面帶有使用者輸入資訊,那麼我們就要注意了。

看下面的例子:

    public void wrongFormat(){
        Calendar c = new GregorianCalendar(2020, GregorianCalendar.JULY, 27);
        String input=" %1$tm";
        System.out.format(input + " 時間不比對,應該是某個月的第 %1$terd 天", c);
    }      

粗看一下沒什麼問題,但是我們的input中包含了格式化資訊,最後輸出結果:

 07 時間不比對,應該是某個月的第 27rd 天      

變相的,我們擷取到了系統内部的資訊,在某些情況下面,可能會暴露系統的内部邏輯。

上面的例子我們應該将input也作為一個參數,如下所示:

    public void rightFormat(){
        Calendar c = new GregorianCalendar(2020, GregorianCalendar.JULY, 27);
        String input=" %1$tm";
        System.out.format("%s 時間不比對,應該是某個月的第 %terd 天",input, c);
    }      
 %1$tm 時間不比對,應該是某個月的第 27rd 天      

小心使用Runtime.exec()

我們知道Runtime.exec()使用來調用系統指令的,如果有惡意的使用者調用了“rm -rf /”,一切的一切都完蛋了。

是以,我們在調用Runtime.exec()的時候,一定要小心注意檢測使用者的輸入。

看下面的一個例子:

    public void wrongExec() throws IOException {
        String dir = System.getProperty("dir");
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(new String[] {"sh", "-c", "ls " + dir});
    }      

上面的例子中,我們從系統屬性中讀取dir,然後執行了系統的ls指令來檢視dir中的内容。

如果有惡意使用者給dir指派成:

/usr & rm -rf /      

那麼系統實際上執行的指令就是:

sh -c 'ls /usr & rm -rf /'      

進而導緻惡意的删除。

解決上面的問題也有幾個方法,第一個方法就是對輸入做個校驗,比如我們隻運作dir包含特定的字元:

    public void correctExec1() throws IOException {
        String dir = System.getProperty("dir");
        if (!Pattern.matches("[0-9A-Za-z[@.](https://my.oschina.net/sakkeil)]+", dir)) {
            // Handle error
        }
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(new String[] {"sh", "-c", "ls " + dir});
    }      

第二種方法就是使用switch語句,限定特定的輸入:

    public void correctExec2(){
        String dir = System.getProperty("dir");
        switch (dir){
            case "/usr":
                System.out.println("/usr");
                break;
            case "/local":
                System.out.println("/local");
                break;
            default:
                break;
        }
    }      

還有一種就是不使用Runtime.exec()方法,而是使用java自帶的方法。

正規表達式的比對

在正規表達式的建構過程中,如果使用使用者自定義輸入,同樣的也需要進行輸入校驗。

考慮下面的正規表達式:

(.*? +public\[\d+\] +.*.*)      

上面的表達式本意是想在public[1234]這樣的日志資訊中,搜尋使用者的輸入。

但是使用者實際上可以輸入下面的資訊:

.*)|(.*      

最終導緻正規表達式變成下面的樣子:

(.*? +public\[\d+\] +.*.*)|(.*.*)