顯示具有 form 標籤的文章。 顯示所有文章
顯示具有 form 標籤的文章。 顯示所有文章

2019年8月22日 星期四

[macOS] 使用 cURL command 上傳檔案且修改 Content-Disposition filename 欄位資訊

故事是為了檢驗 embedded linux 上,一些 CGI 資安問題。例如有隻 CGI 提供檔案上傳時,通常上傳完後就把檔案擺在指定位置。但擺放到指定位置時,若沒有寫好,直接拿 filename 去擺放時,就有機會做邪惡的事了!

而透過 cURL 上傳檔案,通常是用以下簡單的指定就完成:

$ curl -F "file=@/tmp/sample.txt" http://192.168.1.1/cgi-bin/upload.cgi

但如果細看表單內容物時,產出的結果:

POST /cgi-bin/upload.cgi HTTP/1.1
Host: 192.168.1.1
Content-Type: multipart/form-data
Content-Length: ##

----------########
Content-Disposition: form-data; name="file";
filename="sample.txt"

...


這時,有些 CGI 開發上會直接拿上述 filename 參數資訊來用,寫不好就會產生跳脫問題,如:

Content-Disposition: form-data; name="file";
filename="../etc/sample.txt"


若底層是直接用 sprintf(des_filepath, "/tmp/%s", filename); 就會有機會逃脫去覆蓋掉東西。幸運的是 embedded linux 大多會設定檔案系統是 Read-only file system 來保護 :P 若是用 PHP 時,大多都靠 basename 來保護了。

最後,補上靠 curl 串改 filename 的招數:

$ curl -F "file=@/tmp/sample.txt; filename=../etc/sample.txt" http://192.168.1.1/cgi-bin/upload.cgi

2017年5月10日 星期三

[Javascript] 添加密碼規則要求 - 使用 jQuery.validator

剛好碰到需求,且沒有太嚴重的部分,就先偷懶用前端擋:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.min.js"></script>

<script>

$(document).ready(function() {
// 要有 a-z
$.validator.methods.passwd_rule1 = function( value, element ) {
return this.optional( element ) || /[a-z]+/.test( value );
}
// 要有 A-Z
$.validator.methods.passwd_rule2 = function( value, element ) {
return this.optional( element ) || /[A-Z]+/.test( value );
}
// 要有 0-9
$.validator.methods.passwd_rule3 = function( value, element ) {
return this.optional( element ) || /[0-9]+/.test( value );
}


$("form").validate({
rules: {
newPassword: {
required: true,
minlength: 8,
passwd_rule1: true,
passwd_rule2: true,
passwd_rule3: true,
},

checkNewPassword: {
equalTo: "#newPassword"
}
},

messages: {
newPassword: "密碼必須8位數以上,且需含大小寫英文跟數字",
checkNewPassword: "輸入錯誤,請確認密碼",
}
});
});
</script>


其他把玩:

在 Chrome console 引入 library 測試,而非修改 source code

var js = document.createElement('script');
js.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(js);
var js = document.createElement('script');
js.src = "https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.min.js";
document.getElementsByTagName('head')[0].appendChild(js);


替沒有 id tag 的 input 添加 id:

$('[name=newPassword]').attr('id', 'newPassword');