Exercise 1

Links and Archiving Tools

Archiving

For this exercise, information from the manual pages of ln, tar, and zip may be helpful.

Use the dd command to create a file that is 1 MB in size and contains random bits:

dd if=/dev/urandom of=testfile bs=1M count=1

Create a hard link named testlink to testfile:

ln testfile testlink

Archive the two files using the following tools:

tar

tar -cf archive.tar testfile testlink

tar + gzip

tar -czf archive.tar.gz testfile testlink

tar + bzip2

tar -cjf archive.tar.bz2 testfile testlink

zip

zip archive.zip testfile testlink

Analysis

ls -l
-rw-rw-r-- 1 user group 1054720 Jan  1 00:00 archive.tar
-rw-rw-r-- 1 user group 1054338 Jan  1 00:00 archive.tar.bz2
-rw-rw-r-- 1 user group 1049059 Jan  1 00:00 archive.tar.gz
-rw-rw-r-- 1 user group 2097782 Jan  1 00:00 archive.zip
-rw-rw-r-- 2 user group 1048576 Jan  1 00:00 testfile
-rw-rw-r-- 2 user group 1048576 Jan  1 00:00 testlink

Display the contents of the archives! Can you explain the size of the compressed file?

tar -tvf archive.tar
-rw-rw-r-- user/group 1048576 1970-01-01 00:00 testfile
hrw-rw-r-- user/group       0 1970-01-01 00:00 testlink link to testfile
tar -tvf archive.tar.gz
-rw-rw-r-- user/group 1048576 1970-01-01 00:00 testfile
hrw-rw-r-- user/group       0 1970-01-01 00:00 testlink link to testfile
tar -tvf archive.tar.bz2
-rw-rw-r-- user/group 1048576 1970-01-01 00:00 testfile
hrw-rw-r-- user/group       0 1970-01-01 00:00 testlink link to testfile
unzip -l archive.zip
Archive:  archive.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
  1048576  1970-01-01 00:00   testfile
  1048576  1970-01-01 00:00   testlink
---------                     -------
  2097152                     2 files

Zeros

Compare the size of the compressed file when the original file consists only of zeros:

dd if=/dev/zero of=testfile0 bs=1M count=1
ln testfile0 testlink0
tar -cf  archive0.tar     testfile0 testlink0
tar -czf archive0.tar.gz  testfile0 testlink0
tar -cjf archive0.tar.bz2 testfile0 testlink0
zip      archive0.zip     testfile0 testlink0

Analysis

ls -l
-rw-rw-r-- 1 user group 1054720 Jan  1 00:00 archive0.tar
-rw-rw-r-- 1 user group     162 Jan  1 00:00 archive0.tar.bz2
-rw-rw-r-- 1 user group    1191 Jan  1 00:00 archive0.tar.gz
-rw-rw-r-- 1 user group    2380 Jan  1 00:00 archive0.zip
-rw-rw-r-- 1 user group 1054720 Jan  1 00:00 archive.tar
-rw-rw-r-- 1 user group 1054338 Jan  1 00:00 archive.tar.bz2
-rw-rw-r-- 1 user group 1049059 Jan  1 00:00 archive.tar.gz
-rw-rw-r-- 1 user group 2097782 Jan  1 00:00 archive.zip
-rw-rw-r-- 2 user group 1048576 Jan  1 00:00 testfile
-rw-rw-r-- 2 user group 1048576 Jan  1 00:00 testfile0
-rw-rw-r-- 2 user group 1048576 Jan  1 00:00 testlink
-rw-rw-r-- 2 user group 1048576 Jan  1 00:00 testlink0
tar -tvf archive.tar
-rw-rw-r-- user/group 1048576 1970-01-01 00:00 testfile
hrw-rw-r-- user/group       0 1970-01-01 00:00 testlink link to testfile

tar -tvf archive0.tar
-rw-rw-r-- user/group 1048576 1970-01-01 00:00 testfile0
hrw-rw-r-- user/group       0 1970-01-01 00:00 testlink0 link to testfile0
tar -tvf archive.tar.gz
-rw-rw-r-- user/group 1048576 1970-01-01 00:00 testfile
hrw-rw-r-- user/group       0 1970-01-01 00:00 testlink link to testfile

tar -tvf archive0.tar.gz
-rw-rw-r-- user/group 1048576 1970-01-01 00:00 testfile0
hrw-rw-r-- user/group       0 1970-01-01 00:00 testlink0 link to testfile0
tar -tvf archive.tar.bz2
-rw-rw-r-- user/group 1048576 1970-01-01 00:00 testfile
hrw-rw-r-- user/group       0 1970-01-01 00:00 testlink link to testfile

tar -tvf archive0.tar.bz2
-rw-rw-r-- user/group 1048576 1970-01-01 00:00 testfile0
hrw-rw-r-- user/group       0 1970-01-01 00:00 testlink0 link to testfile0
unzip -l archive.zip
Archive:  archive.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
  1048576  1970-01-01 00:00   testfile
  1048576  1970-01-01 00:00   testlink
---------                     -------
  2097152                     2 files

unzip -l archive0.zip
Archive:  archive0.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
  1048576  1970-01-01 00:00   testfile0
  1048576  1970-01-01 00:00   testlink0
---------                     -------
  2097152                     2 files

Relevance

This topic has security relevance in connection with ZIP bombs: