Fork me on GitHub
KeKe Blog

Linux文件加密

  近段时间公司内爆出较多安全问题,导致一些版本包的源代码泄露。为了解决相关的安全事项,决定对相关的文件和数据进行加密操作。目前在网上找到了一些相关资料,自己也跟着相关的文档做了测试。暂时可以使用着先。

1 gzexe加密方式

  这种加密方式不是非常保险的方法,但是能够满足一般的加密用途,可以隐蔽脚本中的密码等信息。
它是使用系统自带的gzexe程序,它不但加密,同时压缩文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
[root@KE_03 ~]# which gzexe
/usr/bin/gzexe
[root@KE_03 ~]# echo "my test" >test.txt
[root@KE_03 ~]# cat test.txt
my test
[root@KE_03 ~]# ls
anaconda-ks.cfg install.log install.log.syslog sh_dir test.txt
# 对test.txt文件进行加密
[root@KE_03 ~]# gzexe test.txt
test.txt: 75.0%
[root@KE_03 ~]# ls
anaconda-ks.cfg install.log install.log.syslog sh_dir test.txt test.txt~
# 加密成功后会生成一个~结尾的备份文件
[root@KE_03 ~]# ls test.txt*
test.txt test.txt~
# 查看加密后的文件,显示乱码
[root@KE_03 ~]# cat test.txt
#!/bin/sh
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 µ;_Ztest.txt˭T(I-.8
[root@KE_03 ~]# cat test.txt~
my test
# 使用-d参数进行解密,test.txt和test.txt~的内容会互相调换
[root@KE_03 ~]# gzexe -d test.txt
[root@KE_03 ~]# ls test.txt*
test.txt test.txt~
[root@KE_03 ~]# cat test.txt
my test
[root@KE_03 ~]# cat test.txt~
#!/bin/sh
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 µ;_Ztest.txt˭T(I-.8
[root@KE_03 ~]#

2 tar加密方式

  使用tar命令,对文件加密压缩和解压

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[root@KE_03 mnt]# echo "my test" > test.txt
[root@KE_03 mnt]# cat test.txt
my test
# 对单个文件进行加密,也可以指定目录或者使用正则
[root@KE_03 mnt]# tar zcvf - test.txt | openssl des3 -salt -k 123@123 | dd of=test.txt.des3
test.txt
记录了0+1 的读入
记录了0+1 的写出
152字节(152 B)已复制,0.00401047 秒,37.9 kB/秒
[root@KE_03 mnt]# ls
test.txt test.txt.des3
# 加密后生成一个结尾为.des的文件,一般情况下会将原文件删除掉
[root@KE_03 mnt]# cat test.txt
my test
[root@KE_03 mnt]# cat test.txt.des3
Salted__( m?Ci$+ȖDbrЛȱŲxܼ7画±亠vWij+²¤w넑¤Oف)8緑兆
a,
eθT҅´
A7�_̣±ȷ§¾ߌ.´¾+¤蹼±ኞ뤑ʹµꌞQ.܌]¨ ۻ[root@KE_03 mnt]# Xshell
-bash: Xshell: command not found
[root@KE_03 mnt]# rm -rf test.txt
[root@KE_03 mnt]# ls
test.txt.des3
# 解压加密文件,得到原文件
[root@KE_03 mnt]# dd if=test.txt.des3 | openssl des3 -d -k 123@123 | tar zxvf -
记录了0+1 的读入
记录了0+1 的写出
152字节(152 B)已复制,3.2842e-05 秒,4.6 MB/秒
test.txt
[root@KE_03 mnt]# ls
test.txt test.txt.des3
[root@KE_03 mnt]# cat test.txt
my test
# 注意命令最后面的``"-"``,它将释放所有文件,
# -k 123@123可以没有,没有时在解压时会提示输入密码

3 tar与openssl结合加密方式

  当有重要的敏感数据的时候,给文件和目录额外加一层保护是至关重要的,特别是当需要通过网络与他人传输数据的时候。基于这个原因,
  可以用到tar(Linux 的一个压缩打包工具)和OpenSSL来解决的方案。借助这两个工具,你真的可以毫不费力地创建和加密 `tar `归档文件。

  Openssl的常规方式:
openssl command command-options arguments

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
[root@KE_03 mnt]# echo "123" > a.txt
[root@KE_03 mnt]# echo "234" > b.txt
[root@KE_03 mnt]# echo "987" > c.txt
[root@KE_03 mnt]# ls
a.txt b.txt c.txt
[root@KE_03 mnt]# ls
a.txt b.txt c.txt
# 对a/b/c.txt三个文件进行加密,需要输入两次密码
[root@KE_03 mnt]# tar -czf - * | openssl enc -e -aes256 -out test.tar.gz
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
# 上述命令的解释:
# enc 使用加密进行编码
# -e 用来加密输入文件的 enc 命令选项,这里是指前一个tar命令的输出
# -aes256 加密用的算法
# -out 用于指定输出文件名的 enc 命令选项,这里文件名是test.tar.gz
# 加密压缩成功后,删除原有的文件
[root@KE_03 mnt]# rm -rf *.txt
[root@KE_03 mnt]# ls
test.tar.gz
# 直接解压会显示报错
[root@KE_03 mnt]# tar zxvf test.tar.gz
gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now
# 需要先通过openssl先解密,然后再进行解压操作
[root@KE_03 mnt]# openssl enc -d -aes256 -in test.tar.gz | tar xz -C ./
enter aes-256-cbc decryption password:
# 上述命令的解释:
# -d 用于解密文件
# -C 将加压后的文件提取到目标目录下
[root@KE_03 mnt]# ls
a.txt b.txt c.txt test.tar.gz

4 shc加密方式(仅对shell脚本加密)

  shc是一个专业的加密shell脚本的工具.它的作用是把shell脚本转换为一个可执行的二进制文件,这个办法很好的解决了脚本中含有IP、密码等不希望公开的问题。
  如果你的shell脚本包含了敏感的口令或者其它重要信息, 而且你不希望用户通过ps -ef(查看系统每个进程的状态)捕获敏感信息. 你可以
  使用shc工具来给shell脚本增加一层额外的安全保护. shc是一个脚本编译工具, 使用RC4加密算法, 它能够把shell程序转换成二进制可执行文件(支持静态链接和动态链接). 该工具能够很好的支持: 需要加密, 解密, 或者通过命令参数传递口令的环境.

shc官方下载地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# 安装shc命令
[root@KE_03 ~]# cd /usr/local/src/
[root@KE_03 src]# wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.9.tgz
--2018-01-17 19:48:05-- http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.9.tgz
正在解析主机 www.datsi.fi.upm.es... 138.100.9.22
正在连接 www.datsi.fi.upm.es|138.100.9.22|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:20846 (20K) [application/x-gzip]
正在保存至: “shc-3.8.9.tgz”
100%[========================================================================>] 20,846 28.8K/s in 0.7s
2018-01-17 19:48:07 (28.8 KB/s) - 已保存 “shc-3.8.9.tgz” [20846/20846])
[root@KE_03 src]# tar -zvxf shc-3.8.9.tgz
shc-3.8.9/
shc-3.8.9/match
shc-3.8.9/shc-3.8.9.c
shc-3.8.9/CHANGES
shc-3.8.9/test.csh
shc-3.8.9/test.bash
shc-3.8.9/shc.c
shc-3.8.9/shc.1
shc-3.8.9/shc.html
shc-3.8.9/pru.sh
shc-3.8.9/test.ksh
shc-3.8.9/makefile
shc-3.8.9/shc.README
shc-3.8.9/Copying
[root@KE_03 src]# cd shc-3.8.9
# 这一步必须提前先创建好,否则无法正常进行安装
[root@KE_03 shc-3.8.9]# mkdir -p /usr/local/man/man1
[root@KE_03 shc-3.8.9]# make install
cc -Wall shc.c -o shc
*** Installing shc and shc.1 on /usr/local
# 需要输入yes/no,不然安装过程会报错
*** ¿Do you want to continue? yes
install -c -s shc /usr/local/bin/
install -c -m 644 shc.1 /usr/local/man/man1/
# 测试shc加密
[root@KE_03 tmp]# cd /mnt/
[root@KE_03 mnt]# ls
[root@KE_03 mnt]# vim test.sh
[root@KE_03 mnt]# sh test.sh 123
It is my shc test
my name is 123
# 对test.sh进行加密操作
[root@KE_03 mnt]# shc -r -f test.sh
[root@KE_03 mnt]# ls
test.sh test.sh.x test.sh.x.c
# 执行加密后文件无输出,经过验证和查询,需要加入-T参数,虽然-help中无改参数,但确实存在。
[root@KE_03 mnt]# ./test.sh.x 123
[1]+ Stopped ./test.sh.x 123
# 加入-T参数后,重新加密,获得的二进制文件可正常执行
[root@KE_03 mnt]# shc -r -T -f test.sh
[root@KE_03 mnt]# ./test.sh.x 123
It is my shc test
my name is 123
# 加密完成后一般会删除原有的脚本和生成的c文件
[root@KE_03 mnt]# rm -rf test.sh
[root@KE_03 mnt]# rm -rf test.sh.x
[root@KE_03 mnt]# rm -rf test.sh.x.c
[root@KE_03 mnt]# ls
test.sh.x
[root@KE_03 mnt]# ./test.sh.x oyk
It is my shc test
my name is oyk
# 可以对生成的二进制加密文件设定可执行的时间
[root@KE_03 mnt]# shc -e 27/02/2018 -m "this script file is about to expire" -v -r -T -f test.sh
shc shll=bash
shc [-i]=-c
shc [-x]=exec '%s' "$@"
shc [-l]=
shc opts=
shc: cc test.sh.x.c -o test.sh.x
shc: strip test.sh.x
shc: chmod go-r test.sh.x
[root@KE_03 mnt]# ./test.sh.x oyk
It is shc test
My name is oyk
[root@KE_03 mnt]# date
2018年 01月 17日 星期三 20:29:47 CST
[root@KE_03 mnt]# shc -e 16/01/2018 -m "this script file is about to expire" -v -r -T -f test.sh
shc shll=bash
shc [-i]=-c
shc [-x]=exec '%s' "$@"
shc [-l]=
shc opts=
shc: cc test.sh.x.c -o test.sh.x
shc: strip test.sh.x
shc: chmod go-r test.sh.x
# 日期过期后,改二进制脚本无法正常执行,并弹出相关的提示
[root@KE_03 mnt]# ./test.sh.x oyk
./test.sh.x: has expired!
this script file is about to expire
# shc相关参数及用法
[root@KE_03 mnt]# shc -help
shc Version 3.8.9, Generic Script Compiler
shc Copyright (c) 1994-2012 Francisco Rosales <frosal@fi.upm.es>
shc Usage: shc [-e date] [-m addr] [-i iopt] [-x cmnd] [-l lopt] [-rvDTCAh] -f script
-e %s Expiration date in dd/mm/yyyy format [none] (指定过期日期)
-m %s Message to display upon expiration ["Please contact your provider"] (指定过期提示信息)
-f %s File name of the script to compile (指定要编译的shell的路径及文件名)
-i %s Inline option for the shell interpreter i.e: -e
-x %s eXec command, as a printf format i.e: exec('%s',@ARGV);
-l %s Last shell option i.e: --
-r Relax security. Make a redistributable binary (可以相同操作系统的不通系统中执行)
-v Verbose compilation (编译的详细情况)
-D Switch ON debug exec calls [OFF]
-T Allow binary to be traceable [no]
-C Display license and exit
-A Display abstract and exit
-h Display help and exit
Environment variables used:
Name Default Usage
CC cc C compiler command
CFLAGS <none> C compiler flags
Please consult the shc(1) man page.
[root@KE_03 mnt]#

说明:
相同的os,shc后的可执行二进制文件直接可以移植运行,不通os可能会出现问题。


5 zip加密方式

  • 文件加密
    使用命令”zip -e filename.zip filename” 即可出现输入密码的提示,输入2次密码。 此文件即被加密解压时候是需要密码的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@KE_03 mnt]# cat test.txt
It is zip test
# 输入两次密码
[root@KE_03 mnt]# zip -e test.txt.zip test.txt
Enter password:
Verify password:
adding: test.txt (stored 0%)
[root@KE_03 mnt]# ls
test.txt test.txt.zip
[root@KE_03 mnt]# rm -rf test.txt
[root@KE_03 mnt]# ls
test.txt.zip
# 解压时需要输入密码
[root@KE_03 mnt]# unzip test.txt.zip
Archive: test.txt.zip
[test.txt.zip] test.txt password:
extracting: test.txt
[root@KE_03 mnt]# ls
test.txt test.txt.zip
[root@KE_03 mnt]# cat test.txt
It is zip test
  • 文件夹加密
    使用命令”zip -re dirname.zip dirname”即可出现输入密码的提示,输入2次密码。 此文件即被加密解压时候是需要密码的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[root@KE_03 mnt]# mkdir dirtest
[root@KE_03 mnt]# mv test.txt ./dirtest/
[root@KE_03 mnt]# tree ./
./
└── dirtest
└── test.txt
1 directory, 1 file
[root@KE_03 mnt]# zip -re dirtest.zip dirtest
Enter password:
Verify password:
adding: dirtest/ (stored 0%)
adding: dirtest/test.txt (stored 0%)
[root@KE_03 mnt]# ls
dirtest dirtest.zip
[root@KE_03 mnt]# rm -rf dirtest
[root@KE_03 mnt]# ls
dirtest.zip
[root@KE_03 mnt]# unzip dirtest.zip
Archive: dirtest.zip
creating: dirtest/
[dirtest.zip] dirtest/test.txt password:
extracting: dirtest/test.txt
[root@KE_03 mnt]# ls
dirtest dirtest.zip
[root@KE_03 mnt]# tree ./
./
├── dirtest
│ └── test.txt
└── dirtest.zip
1 directory, 2 files
[root@KE_03 mnt]# cat dirtest/test.txt
It is zip test

6 GnuPG加密方式

  GnuPG的全称是GNU隐私保护(GNU Privacy Guard),常常被称为GPG,它结合了一组加密软件。它是由GNU项目用C编程语言编写的。最新的稳定版本是2.0.27。在如今的大多数Linux发行版中,gnupg程序包都是默认随带的,所以万一它没有安装,你可以使用apt或yum从软件库来安装它(yum install gnupg)。注意:gpg只能对文件进行加密,对目录则无法完成加密!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@KE_03 mnt]# cat test.txt
It is test
[root@KE_03 mnt]# gpg -c test.txt
gpg: 已创建目录‘/root/.gnupg’
gpg: 新的配置文件‘/root/.gnupg/gpg.conf’已建立
gpg: 警告:在‘/root/.gnupg/gpg.conf’里的选项于此次运行期间未被使用
gpg: 钥匙环‘/root/.gnupg/pubring.gpg’已建立
can't connect to `/root/.gnupg/S.gpg-agent': 没有那个文件或目录
# gpg-agent[1324]: 已创建目录‘/root/.gnupg/private-keys-v1.d’这个信息可以忽略
# 注意:如上加密的时候,会弹出来一个对话框,要求Paraphrase输入两次密码,对这个特定的文件进行加密。
# 一旦运行带-c选项(完全使用对称密码算法加密)的gpc命令,它会生成一个文件.gpg文件。
[root@KE_03 mnt]# ls
test.txt test.txt.gpg
[root@KE_03 mnt]# rm -rf test.txt
[root@KE_03 mnt]# ls
test.txt.gpg
[root@KE_03 mnt]# gpg test.txt.gpg
gpg: 钥匙环‘/root/.gnupg/secring.gpg’已建立
gpg: 3DES 加密过的数据
can't connect to `/root/.gnupg/S.gpg-agent': 没有那个文件或目录
gpg: 以 1 个密码加密
gpg: 警告:报文未受到完整的保护
[root@KE_03 mnt]# ls
test.txt test.txt.gpg
[root@KE_03 mnt]# cat test.txt
It is test

参考资料

linux下文件加密方法总结

-------------本文结束 感谢您的阅读-------------