輸入驗證和表示法問題是由中繼字元、替代編碼和數值表示法引起的。信任輸入會導致安全問題。問題包括:「Buffer Overflows」、「Cross-Site Scripting」攻擊、「SQL Injection」及其他許多問題。
select()
查詢,以搜尋符合使用者指定產品類別的清單。使用者還可以指定欄按結果排序。假設應用程式已經過適當驗證,並已在此程式碼片段之前設定 customerID
的值。
...
String customerID = getAuthenticatedCustomerID(customerName, customerCredentials);
...
AmazonSimpleDBClient sdbc = new AmazonSimpleDBClient(appAWSCredentials);
String query = "select * from invoices where productCategory = '"
+ productCategory + "' and customerID = '"
+ customerID + "' order by '"
+ sortColumn + "' asc";
SelectResult sdbResult = sdbc.select(new SelectRequest(query));
...
select * from invoices
where productCategory = 'Fax Machines'
and customerID = '12345678'
order by 'price' asc
productCategory
和 price
不包含單引號字元的時候,查詢才會正確執行。但是,如果攻擊者提供「Fax Machines' or productCategory = \"
」字串給 productCategory
和「\" order by 'price
」字串給 sortColumn
,那麼查詢將會變為:
select * from invoices
where productCategory = 'Fax Machines' or productCategory = "'
and customerID = '12345678'
order by '" order by 'price' asc
select * from invoices
where productCategory = 'Fax Machines'
or productCategory = "' and customerID = '12345678' order by '"
order by 'price' asc
customerID
所需的 Authentication,並允許攻擊者檢視所有符合 'Fax Machines'
清單記錄的客戶。customerID
的值。
...
productCategory = this.getIntent().getExtras().getString("productCategory");
sortColumn = this.getIntent().getExtras().getString("sortColumn");
customerID = getAuthenticatedCustomerID(customerName, customerCredentials);
c = invoicesDB.query(Uri.parse(invoices), columns, "productCategory = '" + productCategory + "' and customerID = '" + customerID + "'", null, null, null, "'" + sortColumn + "'asc", null);
...
select * from invoices
where productCategory = 'Fax Machines'
and customerID = '12345678'
order by 'price' asc
productCategory
所動態建構而成,所以只有在 productCategory
和 sortColumn
不包含單引號字元時,查詢才會正確執行。如果攻擊者提供「Fax Machines' or productCategory = \"
」字串給 productCategory
和「\" order by 'price
」字串給 sortColumn
,那麼查詢將會變為:
select * from invoices
where productCategory = 'Fax Machines' or productCategory = "'
and customerID = '12345678'
order by '" order by 'price' asc
select * from invoices
where productCategory = 'Fax Machines'
or productCategory = "' and customerID = '12345678' order by '"
order by 'price' asc
customerID
所需的 Authentication,並允許攻擊者檢視所有符合 'Fax Machines'
清單記錄的客戶。HttpRequest
類別以陣列存取形式 (例如 Request["myParam"]
) 提供對 QueryString
、Form
、Cookies
或 ServerVariables
集合中變數的程式化存取。存在多個名稱相同的變數時,.NET Framework 會傳回在集合中以下列順序搜尋時第一個出現的變數值:QueryString
、Form
、Cookies
,然後 ServerVariables
。因為 QueryString
依搜尋順序第一個出現,因此 QueryString
參數可以取代表單、Cookie 及伺服器變數的值。同樣地,表單值可以取代 Cookies
和 ServerVariables
集合中的變數,而 Cookies
集合的變數可取代 ServerVariables
的變數。
...
String toAddress = Request["email"]; //Expects cookie value
Double balance = GetBalance(userID);
SendAccountBalance(toAddress, balance);
...
http://www.example.com/GetBalance.aspx
時執行 Example 1
中的程式碼。若攻擊者能夠使通過驗證的使用者按下要求 http://www.example.com/GetBalance.aspx?email=evil%40evil.com
的連結,則含有使用者帳戶餘額的電子郵件將會送往 evil@evil.com
。HttpRequest
類別以陣列存取形式 (例如 Request["myParam"]
) 提供對 QueryString
、Form
、Cookies
或 ServerVariables
集合中變數的程式化存取。存在多個名稱相同的變數時,.NET Framework 會傳回在集合中以下列順序搜尋時第一個出現的變數值:QueryString
、Form
、Cookies
,然後 ServerVariables
。因為 QueryString
依搜尋順序第一個出現,因此 QueryString
參數可以取代表單、Cookie 及伺服器變數的值。同樣地,表單值可以取代 Cookies
和 ServerVariables
集合中的變數,而 Cookies
集合的變數可取代 ServerVariables
的變數。www.example.com
。
...
if (Request["HTTP_REFERER"].StartsWith("http://www.example.com"))
ServeContent();
else
Response.Redirect("http://www.example.com/");
...
http://www.example.com/ProtectedImages.aspx
時執行 Example 1
中的程式碼。若攻擊者對 URL 做一個直接的要求,則不會設定適當的 Referer 表頭,而要求將失敗。不過,若攻擊者以必要值提交偽造 HTTP_REFERER
參數 (如 http://www.example.com/ProtectedImages.aspx?HTTP_REFERER=http%3a%2f%2fwww.example.com
),則查詢將從 QueryString
傳回值,而不是 ServerVariables
,同時檢查也將成功。