安全提取文件的技术有哪些?

安全提取文件的技术有哪些?

昨天我正在做一些实验斯利塔兹。它使用多个 initrd.img 来存储文件/更改。

我想将其 initrd.gz 图像之一(这是一个 cpio 存档)提取到一个文件夹,编辑/删除它们,然后再次重新打包。

我使用了这段代码:

cat rootfs.img | cpio -idvm

然后所有文件都被提取到我的根文件系统。我的整个操作系统都损坏了。 (这是多么尴尬的场面啊……)

我应该怎样做才能安全且轻松地进行此类操作?克罗特? LXC? (VirtualBox是最后的手段)

答案1

使用相对路径存档

我建议不要在根级别运行此类命令/。那是自找麻烦。我总是cpio -idvm在自己的目录中运行相关命令,然后使用mvcp手动将文件放在需要的地方。

您还可以使用我在其他 U&L 问答中描述的方法,标题为:如何在 SliTaz Linux 中安装 TazPkg,这也利用了cpio.

使用绝对路径存档

如果存档是使用绝对路径构建的,您可以cpio使用--no-absolute-filenames开关阻止其提取到/.

$ mkdir /tmp/cpio-root
$ cd /tmp/cpio-root
$ cat rootfs.img | cpio -idvm --no-absolute-filenames

参考

答案2

您可以使用该unp实用程序来执行此操作。

unp是一个用于解压多种格式的实用程序。它的功能之一(-U参数)是能够查看存档并查看它是否具有多个根元素。如果是,它将它们提取到一个目录中。

例如:

$ echo $RANDOM > a
$ echo $RANDOM > b
$ tar -cf archive.tar a b
$ rm a b
$ unp -U archive.tar
$ ls -l a b archive
ls: cannot access a: No such file or directory
ls: cannot access b: No such file or directory
archive:
total 8
-rw-r--r-- 1 root root 5 Jun 15 03:16 a
-rw-r--r-- 1 root root 6 Jun 15 03:16 b

unp适用于许多不同的格式(包括cpio)。它只是调用适当的实用程序来处理存档:

# unp -s
Known archive formats and tools:
7z:           p7zip or p7zip-full
ace:          unace
ar,deb:       binutils
arj:          arj
bz2:          bzip2
cab:          cabextract
chm:          libchm-bin or archmage
cpio,afio:    cpio or afio
dat:          tnef
dms:          xdms
exe:          maybe orange or unzip or unrar or unarj or lha 
gz:           gzip
hqx:          macutils
lha,lzh:      lha
lz:           lzip
lzma:         xz-utils or lzma
lzo:          lzop
lzx:          unlzx
mbox:         formail and mpack
pmd:          ppmd
rar:          rar or unrar or unrar-free
rpm:          rpm2cpio and cpio
sea,sea.bin:  macutils
shar:         sharutils
tar:          tar
tar.bz2,tbz2: tar with bzip2
tar.lzip:     tar with lzip
tar.lzop,tzo: tar with lzop
tar.xz,txz:   tar with xz-utils
tar.z:        tar with compress
tgz,tar.gz:   tar with gzip
uu:           sharutils
xz:           xz-utils
zip,cbz,cbr,jar,war,ear,xpi,adf: unzip
zoo:          zoo

输出--help显示它可以做什么:

# unp --help

USAGE:
   /usr/bin/unp [ options ] file [ files... ]
   file: compressed file(s) to expand/extract

   Use -- [ ARGUMENTS ] to pass arguments to external programs, eg. some tar options:
   unp fastgl.tgz xmnt.tgz -- -C /tmp

   Options:
   -f Continue even if program availability checks fail or directory collision occurs
   -u Special helper mode.
      For most archive types:
      - create directory <filename without suffix>/
      - extract contents there
      For Debian/Ubuntu packages:
      - extract data.tar.gz after each operation in local directory
      - extract control.tar.gz into control/<package_version_arch>/
   -U Smart mode, acts like -u (see above) if archive contains multiple
      elements but if there is only one file/directory element then it's stored 
      in the current directory.
   -s Show the list of supported formats
   -v More verbosity
   -h Show this help

相关内容