Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

MySQL 新特性解析:mysql_config_editor 加密原理與原始碼

編輯:關於 Android 開發

MySQL 新特性解析:mysql_config_editor 加密原理與原始碼

從 MySQL 5.6 開始,官方推出了加密工具 mysql_config_editor。在此之前,我們通常把帳號與密碼以明文寫入 my.cnf,如此一來使用 mysql 客戶端登入時便無需再指定帳密。有了 mysql_config_editor 之後,我們改將加密後的帳密存入一個二進位檔案,登入時由客戶端解密該檔以完成連線。由於加解密都在記憶體中進行,因此無法明文顯示檔案內容;只要妥善保存檔案權限,就能有效防止有心人士破解資料庫密碼。

使用方式如下:

mysql_config_editor set --login-path=client --host=localhost --user=localuser --password

這就設定了一組本地資料源:login-path 指定登入時的識別標識、host 為要連線的資料庫、user 為連線所用帳號、password 為連線所用密碼(此處假設輸入 password1)。若需遠端連線,還要加上特定埠號資訊。設定完成後,登入時只需執行 mysql --login-path=client 即可連上該資料庫。

該工具包含 set/remove/print/reset/help 等功能,以下僅分析 set 功能的實作。set 由 set_command 函數完成,主要負責配置帳密等資料源資訊並存入二進位檔:

  1. static int set_command(void)
  2. {
  3. DBUG_ENTER("set_command");
  4. DYNAMIC_STRING file_buf, path_buf;
  5. init_dynamic_string(&path_buf, "", MY_LINE_MAX, MY_LINE_MAX);
  6. init_dynamic_string(&file_buf, "", file_size, 3 * MY_LINE_MAX);
  7. if (tty_password)
  8. opt_password= get_tty_password(NullS);
  9. if (file_size)
  10. {
  11. if (read_and_decrypt_file(&file_buf) == -1) //如果文件存在,就讀取文件,並將文件的密文解密後存放到file_buf中.
  12. goto error;
  13. }
  14. dynstr_append(&path_buf, "["); /* --login=path */
  15. if (opt_login_path)
  16. dynstr_append(&path_buf, opt_login_path);
  17. else
  18. dynstr_append(&path_buf, "client");
  19. dynstr_append(&path_buf, "]");
  20. if (opt_user) /* --user */
  21. {
  22. dynstr_append(&path_buf, "\nuser = ");
  23. dynstr_append(&path_buf, opt_user);
  24. }
  25. if (opt_password) /* --password */
  26. {
  27. dynstr_append(&path_buf, "\npassword = ");
  28. dynstr_append(&path_buf, opt_password);
  29. }
  30. if (opt_host) /* --host */
  31. {
  32. dynstr_append(&path_buf, "\nhost = ");
  33. dynstr_append(&path_buf, opt_host);
  34. }
  35. if (opt_socket)
  36. {
  37. dynstr_append(&path_buf, "\nsocket = ");
  38. dynstr_append(&path_buf, opt_socket);
  39. }
  40. if (opt_port)
  41. {
  42. dynstr_append(&path_buf, "\nport = ");
  43. dynstr_append(&path_buf, opt_port);
  44. }
  45. dynstr_append(&path_buf, "\n");
  46. /* Warn if login path already exists */
  47. if (opt_warn && ((locate_login_path (&file_buf, opt_login_path)) != NULL))
  48. {
  49. int choice;
  50. printf ("WARNING : \'%s\' path already exists and will be " "overwritten. \n Continue? (Press y|Y for Yes, any " "other key for No) : ", opt_login_path);
  51. choice= getchar();
  52. if (choice != (int) 'y' && choice != (int) 'Y') //如果login-path存在是否選擇覆蓋
  53. goto done; /* skip */
  54. }
  55. /* Remove the login path. */
  56. remove_login_path(&file_buf, opt_login_path); //從原來文件中讀取的內容中,刪掉該login-path信息
  57. /* Append the new login path to the file buffer. */
  58. dynstr_append(&file_buf, path_buf.str); //將該login-path的信息加到file_buf的末尾
  59. if (encrypt_and_write_file(&file_buf) == -1) //將包含新的log-path的所有信息和原來的信息加密寫入文件
  60. goto error;
  61. done:
  62. dynstr_free(&file_buf);
  63. dynstr_free(&path_buf);
  64. DBUG_RETURN(0);
  65. error:
  66. dynstr_free(&file_buf);
  67. dynstr_free(&path_buf);
  68. DBUG_RETURN(-1);
  69. }

其邏輯重點在於幾個關鍵函數:read_and_decrypt_file(讀取並解密檔案內容)、locate_login_path(判斷 login-path 是否存在)、remove_login_path(刪除既有 login-path)、dynstr_append(將新的 login-path 附加到末尾),以及 encrypt_and_write_file(加密後寫入檔案)。

先來看加密後的檔案格式:

由於加密檔的前 4 個 byte 為 '\0'(未使用),可跳過;緊接的 20 個 bytes 存放對稱加密演算法的秘鑰,這部分在 read_login_key 呼叫前已讀取,因此也可跳過。所以 read_and_decrypt_file 的過程如下:

  1. /*
  2. Header length for the login file.
  3. 4-byte (unused) + LOGIN_KEY_LEN
  4. */
  5. #define MY_LOGIN_HEADER_LEN (4 + LOGIN_KEY_LEN)
  6. static int read_and_decrypt_file(DYNAMIC_STRING *file_buf)
  7. {
  8. DBUG_ENTER("read_and_decrypt_file");
  9. char cipher[MY_LINE_MAX], plain[MY_LINE_MAX];
  10. uchar len_buf[MAX_CIPHER_STORE_LEN];
  11. int cipher_len= 0, dec_len= 0;
  12. /* Move past key first. */
  13. if (my_seek(g_fd, MY_LOGIN_HEADER_LEN, SEEK_SET, MYF(MY_WME)) != (MY_LOGIN_HEADER_LEN))
  14. goto error; /* Error while seeking. */
  15. /* First read the length of the cipher. */
  16. while (my_read(g_fd, len_buf, MAX_CIPHER_STORE_LEN, MYF(MY_WME)) == MAX_CIPHER_STORE_LEN)
  17. {
  18. cipher_len= sint4korr(len_buf); //將密文的長度轉換成整形
  19. if (cipher_len > MY_LINE_MAX)
  20. goto error;
  21. /* Now read 'cipher_len' bytes from the file. */
  22. if ((int) my_read(g_fd, (uchar *) cipher, cipher_len, MYF(MY_WME)) == cipher_len) //讀取相應密文長度的密文
  23. {
  24. if ((dec_len= decrypt_buffer(cipher, cipher_len, plain)) < 0) //解密該密文
  25. goto error;
  26. plain[dec_len]= 0;
  27. dynstr_append(file_buf, plain); //將解密後的密文追加到file_buf中
  28. }
  29. }
  30. verbose_msg("Successfully decrypted the login file.\n");
  31. DBUG_RETURN(0);
  32. error:
  33. my_perror("couldn't decrypt the file");
  34. DBUG_RETURN(-1);
  35. }

因此該函數本質上是以下四步的循環,直到檔案中所有密文都被解密:1. 取得密文長度;2. 依長度讀取密文;3. 解密;4. 將解密結果附加到 file_buf。函數中透過 sint4korr 轉換密文長度,是因為每個 cipher 由「4 bytes 長度 + cipher 字串」組成,int4store 把長度存在前 4 個 bytes,sint4korr 則反向還原:

  1. #define int4store(T,A) do { *((char *)(T))=(char) ((A));\
  2. *(((char *)(T))+1)=(char) (((A) >> 8));\
  3. *(((char *)(T))+2)=(char) (((A) >> 16));\
  4. *(((char *)(T))+3)=(char) (((A) >> 24));\
  5. } while(0)
  6. #define sint4korr(A) (int32) (((int32) ((uchar) (A)[0])) +\
  7. (((int32) ((uchar) (A)[1]) << 8)) +\
  8. (((int32) ((uchar) (A)[2]) << 16)) +\
  9. (((int32) ((int16) (A)[3]) << 24)))

接著看 locate_login_path 的實作:

  1. static char* locate_login_path(DYNAMIC_STRING *file_buf, const char *path_name)
  2. {
  3. DBUG_ENTER("locate_login_path");
  4. char *addr= NULL;
  5. DYNAMIC_STRING dy_path_name;
  6. init_dynamic_string(&dy_path_name, "", 512, 512); // 初始化dy_path_name動態字符串
  7. //將dy_path_name 設置為[path_name]
  8. dynstr_append(&dy_path_name, "\n[");
  9. dynstr_append(&dy_path_name, path_name);
  10. dynstr_append(&dy_path_name, "]");
  11. //檢查第一個login-path是否就是要尋找的login-path
  12. if (file_buf->str == strstr(file_buf->str, dy_path_name.str + 1))
  13. addr= file_buf->str;
  14. else
  15. {
  16. addr= strstr(file_buf->str, dy_path_name.str);
  17. if (addr)
  18. addr ++; /* Move past '\n' */
  19. }
  20. dynstr_free(&dy_path_name);
  21. DBUG_RETURN(addr); //返回找到的login-path在file_buf的首地址
  22. }

該函數負責判斷 login-path 是否已存在,若存在則回傳它在 file_buf 中的首地址。

若 login-path 已存在,我們可能選擇先移除再新增,接著看 remove_login_path 的實作:

  1. static void remove_login_path(DYNAMIC_STRING *file_buf, const char *path_name)
  2. {
  3. DBUG_ENTER("remove_login_path");
  4. char *start=NULL, *end= NULL;
  5. int to_move, len, diff;
  6. if((start= locate_login_path(file_buf, path_name)) == NULL) //如果該login-path不存在,直接結束
  7. goto done;
  8. end= strstr(start, "\n["); //end為從start開始尋找,下一個login-path的起始位置
  9. if (end) //如果該login-path是file_buf中間的某一個login-path
  10. {
  11. end ++; /* Move past '\n' */
  12. len= ((diff= (start - end)) > 0) ? diff : - diff;
  13. to_move= file_buf->length - (end - file_buf->str);
  14. }
  15. else //如果該login-path是該file_buf中最後一個log-path
  16. {
  17. *start= '\0';
  18. file_buf->length= ((diff= (file_buf->str - start)) > 0) ? diff : - diff;
  19. goto done;
  20. }
  21. while(to_move --) //將該login-path之後的login-path整體前移,覆蓋move掉的login-path
  22. *(start ++)= *(end ++);
  23. *start= '\0';
  24. file_buf->length -= len;
  25. done:
  26. DBUG_VOID_RETURN;
  27. }

該函數負責以「整體前移覆蓋」的方式刪除既有 login-path 的字串,再以 dynstr_append 把新的 login-path 附加到末尾。

最後來看最核心的加密函數 encrypt_and_write_file:

  1. static int encrypt_and_write_file(DYNAMIC_STRING *file_buf)
  2. {
  3. DBUG_ENTER("encrypt_and_write_file");
  4. my_bool done= FALSE;
  5. char cipher[MY_LINE_MAX], *tmp= NULL;
  6. uint bytes_read=0, len= 0;
  7. int enc_len= 0; // Can be negative.
  8. if (reset_login_file(0) == -1) //清空文件,並重新生成隨機加密秘鑰,並將對稱加密秘鑰寫入文件頭部
  9. goto error;
  10. if (my_seek(g_fd, MY_LOGIN_HEADER_LEN, SEEK_SET, MYF(MY_WME)) != (MY_LOGIN_HEADER_LEN))
  11. goto error; /* Error while seeking. */
  12. tmp= &file_buf->str[bytes_read];
  13. while(! done)
  14. {
  15. len= 0;
  16. while(*tmp++ != '\n') //讀取file_buf中的每一行內容
  17. if (len < (file_buf->length - bytes_read))
  18. len ++;
  19. else
  20. {
  21. done= TRUE;
  22. break;
  23. }
  24. if (done)
  25. break;
  26. if ((enc_len= encrypt_buffer(&file_buf->str[bytes_read],++len,cipher+MAX_CIPHER_STORE_LEN)) < 0) //對讀到的這一行內容進行加密,並將密文存放到cipher + MAX_CIPHER_STORE_LEN的地址處
  27. goto error;
  28. bytes_read += len;
  29. if (enc_len > MY_LINE_MAX)
  30. goto error;
  31. /* Store cipher length first. */
  32. int4store(cipher, enc_len); //將密文的長度存放到cipher的頭部
  33. if ((my_write(g_fd, (const uchar *)cipher, enc_len + MAX_CIPHER_STORE_LEN, MYF(MY_WME))) != (enc_len + MAX_CIPHER_STORE_LEN)) //將該行加密過的密文寫到文件
  34. goto error;
  35. }
  36. verbose_msg("Successfully written encrypted data to the login file.\n");
  37. /* Update file_size */
  38. file_size= bytes_read; //更新文件大小
  39. DBUG_RETURN(0);
  40. error:
  41. my_perror("couldn't encrypt the file");
  42. DBUG_RETURN(-1);
  43. }

該函數的主要流程為:讀取 file_buf 中的一行 → 依產生的 KEY 加密並將結果放到 cipher+MAX_CIPHER_STORE_LEN → 將密文長度存放到 cipher 與 cipher+MAX_CIPHER_STORE_LEN 之間 → 將 cipher 寫入檔案 → 更新檔案大小。上述 1~5 步驟循環執行,直到 file_buf 內容全部加密並寫入檔案為止。

下一節將說明具體採用的加密演算法,並透過相關解密演算法撰寫程式對該檔進行解密。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved