入力の検証や表現の問題は、メタキャラクター、代替エンコーディング、数値表現などによって引き起こされます。セキュリティの問題は、入力を信頼することに起因します。この問題に含まれるのは、「Buffer Overflow」、「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
で必要な認証を回避でき、すべての顧客について '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
で必要な認証を回避でき、すべての顧客について 'Fax Machines'
と一致する領収書のレコードを表示できます。HttpRequest
クラスにより、プログラムは QueryString
、Form
、Cookies
、または ServerVariables
コレクションの変数に配列アクセスの形でアクセスすることができます (Request["myParam"]
など)。同じ名前の変数が 2 個以上存在する場合、.NET フレームワークは次の順序でコレクションを検索して最初に検出した変数の値を返します。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);
...
Example 1
のコードが http://www.example.com/GetBalance.aspx
にアクセス中に実行されると仮定してみましょう。攻撃者が認証済みユーザーに http://www.example.com/GetBalance.aspx?email=evil%40evil.com
をリクエストするリンクをクリックさせることができる場合、そのユーザーの口座残高が記載された電子メールは evil@evil.com
に送信されてしまいます。HttpRequest
クラスにより、プログラムは QueryString
、Form
、Cookies
、または ServerVariables
コレクションの変数に配列アクセスの形でアクセスすることができます (Request["myParam"]
など)。同じ名前の変数が 2 個以上存在する場合、.NET フレームワークは次の順序でコレクションを検索して最初に検出した変数の値を返します。QueryString
、Form
、Cookies
、ServerVariables
。QueryString
は最初に検索されるため、QueryString
のパラメーターがフォーム、cookie、およびサーバーの変数の値より優先される可能性があります。同様に、フォームの値は Cookies
と ServerVariables
コレクションの変数より優先され、Cookies
コレクションの変数は ServerVariables
の変数より優先される場合があります。www.example.com
からリクエストが送信されたことを確認するために、HTTP リファラー ヘッダーのサーバー変数をチェックしています。
...
if (Request["HTTP_REFERER"].StartsWith("http://www.example.com"))
ServeContent();
else
Response.Redirect("http://www.example.com/");
...
Example 1
のコードが http://www.example.com/ProtectedImages.aspx
にアクセス中に実行されると仮定してみましょう。攻撃者がこの URL に対して直接リクエストを送信すると、適切なリファラーヘッダーが設定されず、そのリクエストは失敗します。しかし、攻撃者が必要な値を持つ偽の HTTP_REFERER
パラメーター (http://www.example.com/ProtectedImages.aspx?HTTP_REFERER=http%3a%2f%2fwww.example.com
など) を送信した場合には、このルックアップによって ServerVariables
ではなく QueryString
の値が返され、チェックに合格することになります。