平常操做,常常明文指定了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
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>
参考地址:
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
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服务:
[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