Linux 用于安全地压缩和解压缩文件的ZIP命令示例

时间:2020-03-21 11:46:09  来源:igfitidea点击:

Zip实用程序用于在Linux中组合和压缩文件。
在本文中,我介绍了zip命令的用法以及如何保护zip文件。
由于我将在此处讨论与zip有关的安全性主题,因此我包括了与安全性有关的漏洞也有一些技巧或者想法可以通过示例来详细说明,以消除此类漏洞。
我们可以看到zip实用程序的使用方式以及使用它的好处以及如何保护zip文件,以及在压缩或者为zip文件提供安全性时应采取的预防措施。
为增强zip文件的安全性,我还提供了加密和解密的概念。

Zip实用程序用于压缩,或者我们可以说压缩一个或者多个文件或者目录,但是重要的是压缩率。
它可以用于不同的压缩级别,每个级别都有其自身的优势和要求,具体取决于客户的要求。

此处要注意的有关zip命令的重要事实是,压缩率始终取决于要压缩的文件类型。
与简单的文本文件一起使用时,Zip,Gzip等提供了出色的压缩率。
因此,例如,我们可以压缩大型日志文件,并获得良好的压缩率。
但是,如果使用zip或者gzip压缩一些较大的视频/音频文件,则不会获得更好的结果。
因此,我建议仅保留zip压缩文本内容的文件。

如何在Linux中使用zip实用工具压缩文件?

因此,现在如果要在Linux中压缩文件,则必须要压缩一些文件。
其中我创建了三个文件theitroad.txt,sarath.txt和theitroad.txt仅用于测试目的。

让我们看一下这些文件的大小。

[root@localhost test]# ls -lh
total 196K
-rw-r--r-- 1 root root  12K Apr 19 05:26 sarath.txt
-rw-r--r-- 1 root root 113K Apr 19 05:26 theitroad.txt
-rw-r--r-- 1 root root  57K Apr 19 05:26 theitroad.txt

我们可以看到sarath.txt,theitroad.txt和theitroad.txt的文件大小分别为12K,113K和57K。

现在,我将在此处压缩theitroad.txt文件。

[root@localhost test]# zip theitroad.zip theitroad.txt
  adding: theitroad.txt (deflated 98%)
[root@localhost test]# ls -lh
total 200K
-rw-r--r-- 1 root root  12K Apr 19 05:26 sarath.txt
-rw-r--r-- 1 root root 113K Apr 19 05:26 theitroad.txt
-rw-r--r-- 1 root root 2.1K Apr 19 05:27 theitroad.zip
-rw-r--r-- 1 root root  57K Apr 19 05:26 theitroad.txt

现在我们可以看到zip文件的大小小于原始文件的大小。
例如:theitroad.txt文件。

theitroad.txt的大小为113 KB,而压缩后仅为2.1KB,这意味着98%的压缩了,因此我们可以看到使用zip命令可以节省98%的磁盘空间。

这样,我们可以在大小为1 TB的硬盘中保留大约2 TB的数据(前提是我们有大量的大文本文件),我认为这样做是一个很好的主意,稍后我还将介绍该方法保护zip文件,这样我们不仅可以优化磁盘空间的使用,还可以为文件和文件夹提供一层安全保护。

现在如何将zip文件解压缩回原始文件?

首先,我将删除theitroad.txt文件,然后使用unzip命令将theitroad.zip文件解压缩,将其取回来。

[root@localhost test]# rm -rf theitroad.txt

[root@localhost test]# unzip theitroad.zip
Archive:  theitroad.zip
  inflating: theitroad.txt              
[root@localhost test]#

如何压缩多个文件并将其保存在单个文件中?

我有三个文件theitroad.txt,sarath.txt和theitroad.txt,我要一次将它们全部压缩,然后将它们归档在一个名为allfiles.zip的文件中。

[root@localhost test]# zip allfiles.zip theitroad.txt sarath.txt theitroad.txt
  adding: theitroad.txt (deflated 98%)
  adding: sarath.txt (deflated 97%)
  adding: theitroad.txt (deflated 98%)
[root@localhost test]#

现在查看是否压缩文件,或者压缩了多少压缩后,键入以下命令。

[root@localhost test]# ls -lh
total 200K
-rw-r--r-- 1 root root 3.7K Apr 19 06:01 allfiles.zip
-rw-r--r-- 1 root root  12K Apr 19 05:26 sarath.txt
-rw-r--r-- 1 root root 113K Apr 19 05:26 theitroad.txt
-rw-r--r-- 1 root root  57K Apr 19 05:26 theitroad.txt

其中我们可以轻松比较大小,因此可以使用简单的数学计算来计算压缩量。

现在要解压缩它,我们只需使用unzip命令即可。

让我们先删除文件,以便它不要求覆盖选项。

[root@localhost test]# rm -rf sarath.txt theitroad.txt theitroad.txt

现在我们可以看到我们只有一个zip文件。
因此,我将使用unzip命令将其解压缩。

[root@localhost test]# unzip allfiles.zip
Archive:  allfiles.zip
  inflating: theitroad.txt              
  inflating: sarath.txt              
  inflating: theitroad.txt

我们甚至可以将加密解密技术与zip文件一起使用,以增强或者提高文件的安全性。

如何在Linux中加密zip文件?

技巧1:

为了测试zip文件的加密解密技术,我创建了一个纯文本文件linux.txt。

现在,我将首先压缩它,然后再加密该zip文件。

[root@localhost test]# zip linux.zip linux.txt
  adding: linux.txt (deflated 9%)

通过gpg命令加密linux.zip文件:

[root@localhost test]# gpg -c linux.zip
gpg: directory `/root/.gnupg' created
gpg: new configuration file `/root/.gnupg/gpg.conf' created
gpg: WARNING: options in `/root/.gnupg/gpg.conf' are not yet active during this run
gpg: keyring `/root/.gnupg/pubring.gpg' created
passphrase:

gpg实用程序已在此处用于加密zip文件。

-c选项用于创建基于gpg的加密文件。

查看加密的文件:

[root@localhost test]# ls -lh
total 208K
-rw-r--r-- 1 root root 3.7K Apr 19 06:01 allfiles.zip
-rw-r--r-- 1 root root  199 Apr 19 06:26 linux.zip
-rw-r--r-- 1 root root  188 Apr 19 07:10 linux.zip.gpg
-rw-r--r-- 1 root root  12K Apr 19 05:26 sarath.txt
-rw-r--r-- 1 root root 113K Apr 19 05:26 theitroad.txt
-rw-r--r-- 1 root root  57K Apr 19 05:26 theitroad.txt
[root@localhost test]#

我们可以看到linux.zip.gpg文件。
该文件是linux.zip的加密形式,并受密码保护。

现在,让我们看看尝试解压缩该加密的zip文件时会发生什么情况。

[root@localhost test]# unzip linux.zip.gpg
Archive:  linux.zip.gpg
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
unzip:  cannot find zipfile directory in one of linux.zip.gpg or
        linux.zip.gpg.zip, and cannot find linux.zip.gpg.ZIP, period.

因此我们可以看到我们无法简单地将其解压缩而不解密。

那么现在如何解密加密的zip文件呢?

[root@localhost test]# gpg linux.zip.gpg
gpg: keyring `/root/.gnupg/secring.gpg' created
gpg: CAST5 encrypted data
Enter passphrase:

现在,当我们输入在加密期间提供的密码时,文件将被解密。

现在解密后会看到输出:

[root@localhost test]# ls -lh
total 208K
-rw-r--r-- 1 root root 3.7K Apr 19 06:01 allfiles.zip
-rw-r--r-- 1 root root  199 Apr 19 07:22 linux.zip
-rw-r--r-- 1 root root  188 Apr 19 07:10 linux.zip.gpg
-rw-r--r-- 1 root root  12K Apr 19 05:26 sarath.txt
-rw-r--r-- 1 root root 113K Apr 19 05:26 theitroad.txt
-rw-r--r-- 1 root root  57K Apr 19 05:26 theitroad.txt

如何在不加密的情况下为ZIP文件提供密码?

绝招2

在上面看到的以前的方法中,我们借助于GPG命令进行的加密保护了文件。
现在,如果我们不想加密文件,而只想提供最小程度的安全性,则可以通过使用密码锁定zip文件来做到这一点。

这可以通过zip命令中的-P选项来完成。
让我们来看一个例子。

[root@localhost test]# zip -P redhat123 linux.zip linux.txt
  adding: linux.txt (deflated 9%)
  • 在上面的命令中,redhat123是密码。
  • Linux.zip现在是受密码保护的文件。
  • Linux.txt是我们实施此安全性的文件。

现在,如果要解压缩linux.zip,则必须知道密码。

[root@localhost test]# unzip linux.zip
Archive:  linux.zip
[linux.zip] linux.txt password:

我们甚至可以通过压缩整个目录而不是文件来获得相同的结果。
让我们看一个例子。
我将压缩整个/var/log /目录,然后看看会发生什么。

[root@localhost test]# zip -P redhat123  varlogprotected.zip /var/log/*
  adding: var/log/acpid (deflated 88%)
  adding: var/log/anaconda.log (deflated 87%)
  adding: var/log/anaconda.syslog (deflated 70%)
  adding: var/log/anaconda.xlog (deflated 77%)
  adding: var/log/audit/(stored 0%)
  adding: var/log/boot.log (stored 0%)
  adding: var/log/boot.log.1 (stored 0%)
  adding: var/log/boot.log.2 (stored 0%)

上面显示的命令输出相当长,因为我们正在压缩/var/log中的所有内容,这可能会花费一些时间。

上述命令中的循环:

通过上述方法实现的安全性是不安全的。
该技术存在漏洞。
使用历史记录命令,任何人都可以看到加密密码。

#history
 1013  cat linux.txt
 1014  zip -P redhat123 linux.zip linux.txt

我们可以清楚地看到history命令的输出,其中还显示了所应用的密码。
因此,此技术并不安全。

因此,让我们看看另一种方法可以非常安全地做到这一点。

绝招3:

该技巧将消除TRICK2的漏洞,例如:上面的技术。

[root@localhost test]# zip -e linux.zip linux.txt
Enter password:
Verify password:
  adding: linux.txt (deflated 9%)
[root@localhost test]#

在上面显示的示例中,我们在zip命令中使用了-e选项。
现在,如果我们在linux中阅读zip命令的手册页,我们将知道-e选项既用于加密文件,又用于通过密码保护加密。

因此,这里我们不是使用gpg进行加密,而是使用zip命令本身进行加密和密码保护。

甚至我们也可以通过这种方式保护或者提供完整目录的安全性:

敌人的例子,我想保护/var/log /目录。

#zip -e varlogprotected.zip    /var/log/*

现在我们也可以通过以下方法将其解压缩

[root@localhost test]# unzip linux.zip
Archive:  linux.zip
[linux.zip] linux.txt password:
  • 要解压缩它,我们必须知道密码。因为Linux.zip受密码保护。
  • 此技术能够消除TRICK2的漏洞。

但是,即使这项技术也存在另一个漏洞

绝招3:

如果我们仔细观察上述命令的输出,我们会发现即使不提供密码,任何人都可以知道该zip文件中的文件名或者文件列表。
即使输入错误的密码也可以公开所有文件的名称。

现在,让我们消除TRICK 3中的安全问题

绝招4:

前一种方法的主要问题在于,普通用户可以看到zip文件的内容。
我们可以通过归档文件来解决此问题。

存档仅创建一个包含所有必需文件的文件。
对所需文件进行存档,然后将其压缩将提供更高的安全性,因为用户将无法看到存档的内容。

[root@localhost test]# ls
linux.txt  sarath.txt  theitroad.txt  theitroad.txt

这样我们就可以在此处看到四个文本文件。
现在,我将提供安全性,同时使用其他方法压缩这些文件。
例如:TRICK4.
如前所述,我们将首先使用tar命令进行存档。

[root@localhost test]# tar -cvzf securefile.tgz *.txt
linux.txt
sarath.txt
theitroad.txt
theitroad.txt

现在,使用相同的zip命令和-e选项来压缩该存档文件。

[root@localhost test]# zip -e securezip securefile.tgz
Enter password:
Verify password:
  adding: securefile.tgz (deflated 77%)

现在,让我们看看解压缩该文件时会发生什么。
它肯定会要求输入密码,但不会透露其中的文件名。
因为文件在存档中。

[root@localhost test]# ls
securezip.zip
[root@localhost test]# unzip securezip.zip
Archive:  securezip.zip
[securezip.zip] securefile.tgz password:

现在,如果我们输入正确的密码,我们将获得.tgz文件,然后只需使用tar命令将其解压缩即可。

[root@localhost test]# ls
securefile.tgz  securezip.zip

如何解压缩.tgz文件?

注意:.tar.gz和.tgz相同。

[root@localhost test]# tar -xvzf securefile.tgz
linux.txt
sarath.txt
theitroad.txt
theitroad.txt
[root@localhost test]# ls
linux.txt  sarath.txt  theitroad.txt  securefile.tgz  securezip.zip  theitroad.txt
[root@localhost test]#

现在如何在不解压缩的情况下验证zip存档?

[root@localhost test]# unzip -t securezip.zip
Archive:  securezip.zip
[securezip.zip] securefile.tgz password:
    testing: securefile.tgz           OK
No errors detected in compressed data of securezip.zip.

我们可以清楚地看到,securezip.zip文件的压缩数据中未检测到错误。

如何递归压缩目录及其子目录?

我将使用所有子目录和文件将zip/var/spool /目录压缩。

[root@localhost test]# zip -r var-spool.zip /var/spool/
  adding: var/spool/(stored 0%)
  adding: var/spool/mail/(stored 0%)
  adding: var/spool/mail/p4 (stored 0%)

解压缩文件时如何查看详细的输出

我们可以简单地使用详细模式来查看解压缩时的详细输出。

[root@localhost test]# unzip -v varspool.zip
Archive:  varspool.zip
 Length   Method    Size  Ratio   Date   Time   CRC-32    Name
--------  ------  ------- -----   ----   ----   ------    ---
       0  Stored        0   0%  03-14-13 23:23  00000000  var/spool/
       0  Stored        0   0%  03-14-13 23:30  00000000  var/spool/anacron/
       0  Stored        0   0%  04-14-13 05:58  00000000  var/spool/at/
       0  Stored        0   0%  04-23-13 01:35  00000000  var/spool/clientmqueue/
       0  Stored        0   0%  07-15-08 10:41  00000000  var/spool/cron/
       0  Stored        0   0%  03-14-13 23:18  00000000  var/spool/cups/
       0  Stored        0   0%  08-08-08 11:26  00000000  var/spool/lpd/
       0  Stored        0   0%  04-23-13 01:35  00000000  var/spool/mail/
       0  Stored        0   0%  04-23-13 01:35  00000000  var/spool/mqueue/
       0  Stored        0   0%  07-24-09 01:56  00000000  var/spool/repackage/
       0  Stored        0   0%  06-08-09 17:39  00000000  var/spool/vbox/
--------          -------  ---                            ------
       0                0   0%                            11 files
[root@localhost test]#

ZIP命令提供的压缩级别:

Zip命令支持从最低级别0到最大压缩级别9的10级压缩。

ZIP压缩等级0:

级别0不会压缩文件。
它只是将它们存档。

[root@localhost test]# zip -0 level0.zip *.txt
  adding: linux.txt (stored 0%)
  adding: sarath.txt (stored 0%)
  adding: theitroad.txt (stored 0%)
  adding: theitroad.txt (stored 0%)
[root@localhost test]# ls -lh
total 388K
-rw-r--r-- 1 root root 181K Apr 24 01:07 level0.zip
-rw-r--r-- 1 root root   54 Apr 19 06:21 linux.txt
-rw-r--r-- 1 root root  12K Apr 19 05:26 sarath.txt
-rw-r--r-- 1 root root 113K Apr 19 05:26 theitroad.txt
-rw-r--r-- 1 root root  57K Apr 19 05:26 theitroad.txt

ZIP压缩等级1:

1级压缩速度非常快,但仅会进行少量压缩。

[root@localhost test]# zip -1 level1.zip *.txt
  adding: linux.txt (deflated 9%)
  adding: sarath.txt (deflated 96%)
  adding: theitroad.txt (deflated 97%)
  adding: theitroad.txt (deflated 97%)

我们可以清楚地看到级别1的压缩级别大于级别0。

ZIP压缩级别6 :(默认为压缩级别)

[root@localhost test]# zip  level6.zip *.txt
  adding: linux.txt (deflated 9%)
  adding: sarath.txt (deflated 97%)
  adding: theitroad.txt (deflated 98%)
  adding: theitroad.txt (deflated 98%)

现在,我们可以清楚地看到,级别6的压缩级别比以前的压缩级别高。

ZIP压缩等级9 :(最大压缩)

与其他压缩级别相比,此压缩级别有点慢。

[root@localhost test]# zip  -9 level9.zip *.txt
  adding: linux.txt (deflated 9%)
  adding: sarath.txt (deflated 98%)
  adding: theitroad.txt (deflated 99%)
  adding: theitroad.txt (deflated 99%)

因此,我们可以看到压缩级别9是最大的。

1.9K <3.9K <5.6K <181K

因此,我们可以按降序查看压缩级别。

ZIP文件的基于OPENSSL的安全性

让我们看看如何在openssl的帮助下加密zip文件。

将介绍如何使用openssl命令加密zip文件。
为了进行演示,我将在这里使用level6.zip文件。

[root@localhost test]# openssl des3 -salt -in level6.zip -out level6zip.des3
enter des-ede3-cbc encryption password:
Verifying - enter des-ede3-cbc encryption password:

在上面的示例中,我使用了DES加密算法来加密文件。
这里仅显示了另一种保护zip文件的方法。

现在如何解密加密的zip文件?

其中我将解密level6zip.des3文件,并将其输出重定向到解密文件.zip。

[root@localhost test]# openssl des3 -d -salt -in level6zip.des3 -out decryptedfile.zip
enter des-ede3-cbc decryption password:

如何创建zip文件的MD5或者Sha1摘要?

[root@localhost test]# openssl dgst -md5 decryptedfile.zip
MD5(decryptedfile.zip)= 8aec575873fd14b9c7ac738073e2e95d

or

[root@localhost test]# md5sum decryptedfile.zip
8aec575873fd14b9c7ac738073e2e95d  decryptedfile.zip

我们可以看到两个命令是相同的。

显示如何创建zip文件的sha1值的示例。

[root@localhost test]# openssl dgst -sha1 decryptedfile.zip
SHA1(decryptedfile.zip)= b833d6dc64762a2afb1804109a3e79573b7af987

or

[root@localhost test]# sha1sum decryptedfile.zip
b833d6dc64762a2afb1804109a3e79573b7af987  decryptedfile.zip

这两个命令也是一样的。