MySQL之登錄密碼加密認證腳本

1、登錄密碼加密認證腳本應用場景

平常操做,常常明文指定了MySQL密碼來登陸MySQL服務,在登陸成功以後就會拋出下面的警告:
[root@git-server ~]# mysql -uroot -p'wujianwei'html

Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 510
Server version: 5.6.36-log Source distribution
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

對於要求嚴格的業務生產場景不容許出現Warning的,因此可能須要本身定製一下這個錯誤的邏輯。
固然若是不須要知道密碼,能不能換個方式來作呢,其實也行,在5.6中開始有了loginpath,和Oracle中的錢包的功能差很少,其實就是一種認證,作了受權,你不須要知道這些信息,loginpath就是一道橋樑爲你作了認證。
若是你是5.5的版本,沒了loginpath,有沒有其餘的方案來知足需求呢。
有的人可能這個時候開始問,需求是什麼?
咱們設想一下,命令行的方式中,輸入明文密碼,那還要密碼幹嗎,乾脆我輸入密碼的時候你別看,可是history命令裏面有啊。
因此這也算是一個風險點的入口,若是由於一些意外的狀況登陸,那麼這種狀況就很尷尬了。這是需求一。
還有一種場景,若是咱們有大量的MySQL環境,每一個環境的DBA帳戶密碼是統一的,可是密碼很複雜。咱們不能輸入明文,那麼就輸入密碼格式,那就意味着交互和手動輸入,手動輸入簡直了,你會發現這種操做真是原始,高級一點,用下keypass或者keepass等,這個是依賴於本地的環境配置。因此需求二的特色就是手工維護密碼囉嗦,手工輸入密碼太原始。
那咱們寫腳本,可是腳本里面的密碼仍是可見的,調用的明文密碼問題解決了,可是內容中的密碼仍是可讀的。
因此這種狀況下,一個很天然的方法就是加密。
其中一種是對密碼加密,好比咱們獲得一個密碼加密後的串,在須要調用的時候作一下解密,獲得真實的密碼。這個過程是在腳本里的邏輯來實現,因此咱們獲得明文密碼的機率要低一些。
另一類就是對文件加密,好比對整個文件加密,加密以後文件就無法讀了。因此加密後的密碼又被加密了。對文件加密有shell的方式還有python等語言。
若是要調用腳本的時候,其實就是先解密文件,而後調用解密邏輯,獲得真正的密碼,而後開啓訪問的請求。
好比我獲得了一個加密後的密碼串。調用的解密邏輯是decrypt_passwd,固然這個是可讀還可逆的。python

2、Linux下用base64命令加解密字符串

base64加密解密站長工具:
https://base64.supfree.net/mysql

2.1加密:linux

[root@git-server ~]# echo qwq|base64 
d3VqaWFud2VpCg==

2.2解密:git

[root@git-server ~]# echo d3VqaWFud2VpCg==|base64 -d
qwq

2.3下面對MySQL數據庫備份的帳戶密碼加密的方式來源於base64加密
腳本內容以下:github

[root@git-server ~]# cat test03.sh
#!/bin/sh
Pass='d3VqaWFud2VpCg=='
sock=/tmp/mysql.sock

function decrypt_passwd
{
tmp_pass=$1
dec_pass=`echo $tmp_pass|base64 -d`
}

decrypt_passwd $Pass
port=$1

#if [ ! -n "$port" ]; then
#echo '############################################'
#echo 'Please input correct MySQL Port and try again.'
#echo '############################################'
#ps -ef|grep mysqld|grep -v grep |grep -v mysqld_safe
#exit
#fi
/usr/local/mysql/bin/mysql -uroot -p$dec_pass  -P$1  -S$sock

經過此腳本登錄MySQL服務,到此處已經實現了腳本密碼轉換方式登錄MySQL服務sql

[root@git-server ~]# sh test03.sh.sh 3306

Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 513
Server version: 5.6.36-log Source distribution
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

3、Linux Shell 加密解密方法gzexe

參考地址:
http://www.isays.cn/7336.htmlshell

gzexe無需安裝任何軟件是linux自帶的功能使用只須要執行命令便可數據庫

3.一、加密方法:
假如說咱們這個腳本名字叫test03.sh
那咱們就在linux服務器命令執行gzexe test03.sh便可安全

[root@git-server ~]# gzexe  test03.sh
test03.sh:   51.3%

原來的文件就加密了以後會在目錄產生一個test03.sh~的文件這個就是原來文件的備份

[root@git-server ~]# ll test03.sh*
-rwxr-xr-x 1 root root 1122 Jul 20 16:57 test03.sh
-rwxr-xr-x 1 root root  587 Jul 20 16:55 test03.sh~

發現test03.sh腳本已經變成二進制文件
以下圖:

[root@git-server ~]# chmod +x test03.sh
[root@git-server ~]# ll test03.sh
-rwxr-xr-x 1 root root 1128 Jul 20 22:56 test03.sh
[root@git-server ~]# cp test03.sh /usr/local/sbin/

登錄MySQL:

[root@git-server ~]# test03.sh 3306

Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 517
Server version: 5.6.36-log Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

3.二、解密方法:
假如說咱們這個腳本名字叫test03.sh
那咱們就執行
gzexe -d test03.sh
原來的文件就加解密了放在目錄裏面
查看test03.sh內容,事實證實文件內容已經解密了

[root@git-server ~]# cat test03.sh

#!/bin/sh
sock=/tmp/mysql.sock
Pass="d3VqaWFud2VpCg=="
function decrypt_passwd
{
tmp_pass=$1
dec_pass=`echo $tmp_pass|base64 -d`
}

decrypt_passwd $Pass
port=$1

#if [ ! -n "$port" ]; then
#echo '############################################'
#echo 'Please input correct MySQL Port and try again.'
#echo '############################################'
#ps -ef|grep mysqld|grep -v grep |grep -v mysqld_safe
#exit
#fi
/usr/local/mysql/bin/mysql -uroot -p$dec_pass  -P$1  -S$sock

4、加密軟件shc

shc是linux的一款加密腳本的插件東西比較安全咱們能夠利用

4.一、shc軟件安裝
shc官網:https://github.com/yanncam/UnSHc
wget -q http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.9.tgz
tar zxvf shc-3.8.9.tgz
cd shc-3.8.9
make
[root@git-server shc-3.8.9]# make
cc -Wall  shc.c -o shc
*** Do you want to probe shc with a test script?
*** Please try...   make test

[root@git-server shc-3.8.9]# make install
*** Installing shc and shc.1 on /usr/local
*** Do you want to continue? yes
install -c -s shc /usr/local/bin/
install -c -m 644 shc.1 /usr/local/man/man1/
install: target `/usr/local/man/man1/' is not a directory: No such file or directory
make: *** [install] Error 1
請建立 mkdir -p /usr/local/man/man1/  ,而後運行make install
4.二、經常使用參數介紹
-e date (指定過時日期)
-m message (指定過時提示的信息)
-f script_name(指定要編譯的shell的路徑及文件名)
-r Relax security. (能夠相同操做系統的不一樣系統中執行)
-v Verbose compilation(編譯的詳細狀況)

4.三、shc軟件加密使用
假如說咱們這個腳本名字叫test03.sh
那咱們就執行
shc -v -f test03.sh
-v 是現實加密過程
-f 後面跟須要加密的文件

[root@git-server ~]# shc -v -f test03.sh

shc shll=sh
shc [-i]=-c
shc [-x]=exec '%s' "$@"
shc [-l]=
shc opts=
shc: cc  test03.sh.x.c -o test03.sh.x
shc: strip test03.sh.x
shc: chmod go-r test03.sh.x
[root@git-server ~]# ll test03.sh*
-rwxr-xr-x 1 root root   598 Jul 20 17:36 test03.sh
-rwx--x--x 1 root root 12376 Jul 20 17:36 test03.sh.x
-rw-r--r-- 1 root root 12805 Jul 20 17:36 test03.sh.x.c
test03.sh.x爲二進制文件,賦予執行權限後,可直接執行。更更名字mv test03.sh.x test03.sh
test03.sh.x.c 是c源文件。基本沒用,能夠刪除
[root@git-server ~]# mv test03.sh.x test03.sh
驗證文件是否爲二進制文件:

MySQL之登錄密碼加密認證腳本

登錄MySQL服務:

[root@git-server ~]# ./test03.sh 3306

Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 518
Server version: 5.6.36-log Source distribution
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

4.四、shc過時加密法
另shc還提供了一種設定有效執行期限的方法,過時時間,如:
#shc -e 14/09/2016 -m -f test03.sh
選項「-e」指定過時時間,格式爲「日/月/年」;選項「-m」指定過時後執行此shell程序的提示信息。
若是在過時後執行,則會有以下提示:
#./test03.sh.x
./test03.sh.x: has expired!(文件已通過期)
使用以上方法要注意,需防止用戶更改系統時間,能夠經過在程序中加入自動更新系統時間的命令來解決此問題。

4.五、shc加密過的文件的解密方法
利用這個腳原本解密
https://github.com/yanncam/UnSHc

[root@git-server ~]# wget https://github.com/yanncam/UnSHc/archive/master.zip
[root@git-server ~]# unzip master.zip 
Archive:  master.zip
202e5c200005a1b8e474fbfccfb983a582708da1
   creating: UnSHc-master/
  inflating: UnSHc-master/README.md  
   creating: UnSHc-master/latest/
  inflating: UnSHc-master/latest/unshc.sh  
   creating: UnSHc-master/release/
   creating: UnSHc-master/release/0.2/
  inflating: UnSHc-master/release/0.2/unshc-v0.2.sh  
  inflating: UnSHc-master/release/0.2/unshc-v0.2b.sh  
   creating: UnSHc-master/release/0.3/
  inflating: UnSHc-master/release/0.3/unshc-v0.3.sh  
   creating: UnSHc-master/release/0.4/
  inflating: UnSHc-master/release/0.4/unshc-v0.4.sh  
   creating: UnSHc-master/release/0.5/
  inflating: UnSHc-master/release/0.5/unshc-v0.5.sh  
   creating: UnSHc-master/release/0.6/
  inflating: UnSHc-master/release/0.6/unshc-v0.6.sh  
   creating: UnSHc-master/release/0.7/
  inflating: UnSHc-master/release/0.7/unshc-v0.7.sh  
   creating: UnSHc-master/release/0.8/
  inflating: UnSHc-master/release/0.8/unshc-v0.8.sh  
   creating: UnSHc-master/sample/
  inflating: UnSHc-master/sample/test.sh  
  inflating: UnSHc-master/sample/test.sh.x  
  inflating: UnSHc-master/sample/test.sh.x.c  
[root@git-server latest]# cd /root/UnSHc-master/latest;
[root@git-server latest]# ./unshc.sh -h

[root@git-server ~]# /root/UnSHc-master/latest/unshc.sh test03.sh
[root@git-server ~]# ll test03.sh*

-rwx--x--x 1 root root 12184 Jul 20 23:36 test03.sh
-rw-r--r-- 1 root root   597 Jul 20 23:43 test03.sh.sh
-rw-r--r-- 1 root root 11964 Jul 20 23:36 test03.sh.x.c
[root@git-server ~]# 
此時test03.sh.sh 這個文件就是原來的文件
[root@git-server ~]# cat test03.sh.sh

#!/bin/sh
sock=/tmp/mysql.sock
Pass="d3VqaWFud2VpCg=="
function decrypt_passwd
{
tmp_pass=$1
dec_pass=`echo $tmp_pass|base64 -d`
}

decrypt_passwd $Pass

port=$1

#if [ ! -n "$port" ]; then
#echo '############################################'
#echo 'Please input correct MySQL Port and try again.'
#echo '############################################'
#ps -ef|grep mysqld|grep -v grep |grep -v mysqld_safe
#exit
#fi
/usr/local/mysql/bin/mysql -uroot -p$dec_pass  -P$1  -S$sock
相關文章
相關標籤/搜索