溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

shell 腳本加密

發(fā)布時(shí)間:2020-08-02 22:51:31 來(lái)源:網(wǎng)絡(luò) 閱讀:908 作者:雨后的春筍 欄目:開(kāi)發(fā)技術(shù)

    想想好久沒(méi)更新博客了,今天在群里看到討論關(guān)于shell腳本加密的事情。

    想想也是,我們?cè)趯?xiě)腳本有時(shí)候會(huì)配置相關(guān)賬號(hào)和密碼的事情,這樣只要能權(quán)限都能看到該信息,非常的不安全,有沒(méi)有在正常運(yùn)行的情況下對(duì)文件進(jìn)行加密。大致有以下兩個(gè)特點(diǎn):

  1. 加密文件,防止別人看到我寫(xiě)的具體內(nèi)容。

  2. 可以對(duì)隱藏敏感性知識(shí)點(diǎn),比如腳本涉及到用戶和密碼,如mysql

    我特意搜了一下,有兩種方法,漲知識(shí)了,所以驗(yàn)證了一下,過(guò)程如下:


第一種方法(gzexe):基于ubuntu14.04

    這種加密方式不是非常保險(xiǎn)的方法,但是能夠滿足一般的加密用途。它是使用系統(tǒng)自帶的gzexe程序,它不但加密,同時(shí)壓縮文件。

使用方法: gzexe file.sh 它會(huì)把原來(lái)沒(méi)有加密的文件備份為 xx.sh~ ,同時(shí) xx.sh 即被變成加密文件

root@leco:~/test# ls
test.sh
root@leco:~/test# cat test.sh 
#!/bin/bash
echo `date`
root@loocha50:~/test# sh test.sh 
Mon Jul 17 15:08:01 HKT 2017
root@loocha50:~/test# gzexe  test.sh 
test.sh: 25.0%
root@loocha50:~/test# ls
test.sh  test.sh~
root@loocha50:~/test# sh test.sh
Mon Jul 17 15:08:14 HKT 2017
root@loocha50:~/test# sh test.sh~
Mon Jul 17 15:08:18 HKT 2017
root@loocha50:~/test# cat test.sh~
#!/bin/bash
echo `date`
root@loocha50:~/test# cat test.sh
#!/bin/bash
skip=44
tab=''
nl='
'
IFS=" $tab$nl"
umask=`umask`
umask 77
gztmpdir=
trap 'res=$?
  test -n "$gztmpdir" && rm -fr "$gztmpdir"
  (exit $res); exit $res
' 0 1 2 3 5 10 13 15
if type mktemp >/dev/null 2>&1; then
  gztmpdir=`mktemp -dt`
else
  gztmpdir=/tmp/gztmp$$; mkdir $gztmpdir
fi || { (exit 127); exit 127; }
gztmp=$gztmpdir/$0
case $0 in
-* | */*'
') mkdir -p "$gztmp" && rm -r "$gztmp";;
*/*) gztmp=$gztmpdir/`basename "$0"`;;
esac || { (exit 127); exit 127; }
case `echo X | tail -n +1 2>/dev/null` in
X) tail_n=-n;;
*) tail_n=;;
esac
if tail $tail_n +$skip <"$0" | gzip -cd > "$gztmp"; then
  umask $umask
  chmod 700 "$gztmp"
  (sleep 5; rm -fr "$gztmpdir") 2>/dev/null &
  "$gztmp" ${1+"$@"}; res=$?
else
  echo >&2 "Cannot decompress $0"
  (exit 127); res=127
fi; exit $res

此時(shí)我們可以看出test.sh 文件已經(jīng)被加密了,但是不影響運(yùn)行。此時(shí)你刪除test.sh~ 文件,別人就徹底看不到了(自己也看不到,請(qǐng)酌情刪除)。

root@leco:~/test# ls
test.sh  test.sh~
root@leco:~/test# rm test.sh~
root@leco:~/test# ls
test.sh
root@leco:~/test# sh test.sh 
Mon Jul 17 15:10:36 HKT 2017

建議使用,一方面系統(tǒng)自帶命令,達(dá)到目的即可,沒(méi)人閑的破解你的腳本,就算要破解,先進(jìn)入你系統(tǒng)再說(shuō)。


我基于centos6.8 (ubuntu14.04有問(wèn)題,沒(méi)具體排查)

方法2: shc

shc 常用參數(shù)

-e date   
  Expiration date in dd/mm/yyyy format [none](指定過(guò)期日期)


-m message
  message to display  upon  expiration  ["Please  contact your provider"](指定過(guò)期提示的信息)
 
-f script_name
  File name of the script to compile(指定要編譯的shell的路徑及文件名)
 
-r   Relax security. 
   Make  a  redistributable  binary  which executes  on different systems running the same operat-ing system.(可以相同操作系統(tǒng)的不同系統(tǒng)中執(zhí)行)
 
-v   Verbose compilation(編譯的詳細(xì)情況)

shc是一個(gè)專(zhuān)業(yè)的加密shell腳本的工具.它的作用是把shell腳本轉(zhuǎn)換為一個(gè)可執(zhí)行的二進(jìn)制文件,這個(gè)辦法也很好的解決了腳本中含有IP、密碼等不希望公開(kāi)的問(wèn)題

包下載鏈接:

http://www.datsi.fi.upm.es/~frosal/sources/

[root@leco ~]# wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.6.tgz
--2017-07-17 15:23:39--  http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.6.tgz
Resolving www.datsi.fi.upm.es... 138.100.9.22
Connecting to www.datsi.fi.upm.es|138.100.9.22|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 35071 (34K) [application/x-gzip]
Saving to: “shc-3.8.6.tgz”
100%[=============================================================================================================================>] 35,071      21.9K/s   in 1.6s    
2017-07-17 15:23:46 (21.9 KB/s) - “shc-3.8.6.tgz” saved [35071/35071]
[root@leco ~]# tar xf shc-3.8.6.tgz 
[root@leco ~]# mkdir -p /usr/local/man/man1

這步是必須的,不然安裝過(guò)程中會(huì)報(bào)錯(cuò),shc將安裝命令到/usr/local/bin/目錄下;

將幫助文檔存放在/usr/local/man/man1/目錄下,如果系統(tǒng)中無(wú)此目錄,安裝時(shí)會(huì)報(bào)錯(cuò),可創(chuàng)建此目錄后再執(zhí)行安裝。

[root@leco ~]# cd shc-3.8.6
[root@leco shc-3.8.6]# make install
***Installing shc and shc.1 on /usr/local
***Do you want to continue? y  #此時(shí)等待輸入y
install -c -s shc /usr/local/bin/
install -c -m 644 shc.1 /usr/local/man/man1/
使用方法:-f 
[root@leco test]# cat test.sh
#!/bin/bash
echo `date`
[root@leco test]#  shc -r -f test.sh
test.sh.x.c: In function 'chkenv':
test.sh.x.c:211: warning: cast from pointer to integer of different size
You have new mail in /var/spool/mail/root
[root@leco test]# ls
test.sh  test.sh.x  test.sh.x.c
運(yùn)行后會(huì)生成兩個(gè)文件,xx.x 和 xx.x.c.

 

其中xx.x是加密后的可執(zhí)行的二進(jìn)制文件;用./xx.x即可運(yùn)行,xx.x.c是生成xx.x的原文件(c語(yǔ)言).

不建議使用,需要單獨(dú)安裝管理,比較麻煩。如果安全性要求極高,倒是可以參數(shù)。


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI