Java Web SQL injection Test (3)---phenomenon analysis

Source: Internet
Author: User
Tags java web stringbuffer

Then why do the above problems occur? This is caused by improper control of the program code layer. If the Web front-end control of the input data strictly, the database will be manipulated by the string, the client to do the sensitive character escapes, or in the operational database of the DAO layer, the use of dynamic parameters of SQL, not the use of splicing SQL, can prevent the occurrence of such problems.

In general, if the tester understands the specific design of the DAO layer, if the use is non-splicing, it is basically possible to intercept most of these problematic SQL. If you use the stitching method, you can design the test cases and test them well.

So why non-splicing method can effectively prevent SQL injection test it?

Modify the upper part of the core code block, using dynamic SQL (Precompiled SQL Method):

String hql= "Delete from Department where name=?";

Query query = session.createquery (HQL);

Query.setstring (0, name);

There is a problem with the same code entered:

But the result was not deleted. Why is it?

Because the escape of sensitive characters in a string is implemented in the SetString method. The JDK provides the Preparedstatemen interface, in the MySQL jar package, PreparedStatement implements the PreparedStatement in the JDK, the SetString method is as follows:

  1. Public void setString (int parameterindex, String x) throws SQLException {
  2. If the passed string is null and then set this column to null
  3. if (x = = null) {
  4. SetNull (Parameterindex, Types.char);
  5. } Else {
  6. StringBuffer buf = new stringbuffer ((int) (x.length () * 1.1));
  7. Buf.append (' \ ');
  8. int stringlength = X.length ();
  9. //
  10. Note:buf.append (char) is _faster_ than
  11. Appending in blocks, because the block
  12. Append requires a system.arraycopy () ....
  13. Go figure ...
  14. //
  15. for (int i = 0; i < stringlength; ++i) {
  16. char c = X.charat (i);
  17. Switch (c) {
  18. Case 0:/* must is escaped for ' MySQL ' */
  19. Buf.append (' \ \ ');
  20. Buf.append (' 0 ');
  21. break;
  22. Case ' \ n ':/* must is escaped for logs */
  23. Buf.append (' \ \ ');
  24. Buf.append (' n ');
  25. break;
  26. Case ' \ R ':
  27. Buf.append (' \ \ ');
  28. Buf.append (' R ');
  29. break;
  30. Case ‘\\‘:
  31. Buf.append (' \ \ ');
  32. Buf.append (' \ \ ');
  33. break;
  34. Case ‘\‘‘:
  35. Buf.append (' \ \ ');
  36. Buf.append (' \ ');
  37. break;
  38. Case ' ':/* Better safe than sorry */
  39. if (this. Usingansimode) {
  40. Buf.append (' \ \ ');
  41. }
  42. Buf.append (' "');
  43. break;
  44. Case ' \032 ':/* This gives problems on Win32 */
  45. Buf.append (' \ \ ');
  46. Buf.append (' Z ');
  47. break;
  48. default:
  49. Buf.append (c);
  50. }
  51. }
  52. Buf.append (' \ ');
  53. String parameterasstring = buf.tostring ();
  54. byte [] parameterasbytes = null;
  55. if (! this. isloaddataquery) {
  56. Parameterasbytes = Stringutils.getbytes (parameterasstring,
  57. this. Charconverter, this. charencoding, this. Connection
  58. . getservercharacterencoding (), this. Connection
  59. . Parserknowsunicode ());
  60. } Else {
  61. Send with Platform character encoding
  62. Parameterasbytes = Parameterasstring.getbytes ();
  63. }
  64. Setinternal (Parameterindex, parameterasbytes);
  65. }
  66. }

HQL There are many other methods for dynamic parameter binding: Binding by parameter name, binding by parameter location, Setparameter () method, and so on. Http://baike.baidu.com/link?url=NKt6I-Gk0HnyFRWyZ0_ZuDe0pz_aDqVul-VDJZCDCGl9K5LsBghBfxhPVJmZh9qmBKtXgY2EqAqK1oQUNK2Su_

Java Web SQL injection Test (3)---phenomenon analysis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.