Json injection

1. Introduction to Json

JSON is a grammar for storing and exchanging text information, and is a lightweight text data exchange format. Similar to xml, but JSON is smaller, faster, and easier to parse than XML. So now the interface data transmission is carried out in json mode. The MIME type of JSON text is "application/json".

json syntax

  • Data is in name/value pairs
  • Data is separated by comma
  • Curly braces save objects
  • Brackets save the array

JSON value

The JSON value can be:

  • Number (integer or floating point) {"age":30}
  • String (in double quotes) {"uname":"yang"}
  • Logical value (true or false) {"flag":true}
  • Array (in square brackets) {"sites":[{"name":"yang"},{"name":"ming"}]}
  • Objects (in braces) JSON objects are written in braces ({}):
  • null    { "runoob":null }

Json-demo:


  
   
   
  1. {
  2. "users": {
  3. "user": [
  4. {
  5. "id": "1",
  6. "username": "admin",
  7. "passwd": "admin888"
  8. },
  9. {
  10. "id": "2",
  11. "username": "root",
  12. "passwd": "root123"
  13. },
  14. {
  15. "id": "3",
  16. "username": "system",
  17. "passwd": "system456"
  18. }
  19. ]
  20. }
  21. }

Two, JSON injection

It is the same as xml injection, except that the data representation is different.


  
   
   
  1. <?php
  2. header( 'content-type:text/html;charset=utf-8');
  3. if( isset($_POST[ 'json'])){
  4. $json_str=$_POST[ 'json'];
  5. $json=json_decode($json_str);
  6. if(!$json){
  7. die ( 'The format of the JSON document is wrong, please check' );
  8. }
  9. $username=$json->username;
  10. //$passwd=$json->passwd;
  11. $mysqli= new mysqli();
  12. $mysqli->connect( 'localhost', 'root', 'root');
  13. if($mysqli->connect_errno){
  14. die ( 'Database connection failed:' .$mysqli->connect_error);
  15. }
  16. $mysqli->select_db( 'user');
  17. if($mysqli->errno){
  18. dir( 'Failed to open the database:' .$mysqli->error);
  19. }
  20. $mysqli->set_charset( 'utf-8');
  21. $sql= "SELECT username,paawd FROM users WHERE username='{$username}'";
  22. $result=$mysqli->query($sql);
  23. if(!$result){
  24. die ( 'Failed to execute SQL statement:' .$mysqli->error);
  25. } else if($result->num_rows== 0){
  26. die ( 'The query result is empty' );
  27. } else {
  28. $array1=$result->fetch_all(MYSQLI_ASSOC);
  29. echo "Username: {$array1[0]['username']}, Password: {$array1[0]['paawd']}" ;
  30. }
  31. $result->free();
  32. $mysqli->close();
  33. }
  34. ?>

Like SQL injection, insert injection statements. But one thing to pay attention to is to escape the json statement, such as double quotation marks, curly braces, etc.

 

Guess you like

Origin blog.csdn.net/weixin_44110913/article/details/109513243