forked from fcaviggia/hardened-centos7-kickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createiso.sh
executable file
·182 lines (161 loc) · 4.76 KB
/
createiso.sh
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/bash
###############################################################################
# HARDENED CentOS 7 DVD CREATOR
#
# This script was written by Frank Caviggia
# Last update was 16 September 2017
#
# Author: Frank Caviggia ([email protected])
# Copyright: Frank Caviggia, (c) 2018
# Version: 1.3
# License: Apache License, Version 2.0
# Description: Creates embedded kickstart from CentOS ISO that can preform
# a hardened installation based on DISA STIG
###############################################################################
# GLOBAL VARIABLES
DIR=`pwd`
# USAGE STATEMENT
function usage() {
cat << EOF
usage: $0 centos-7.X-x86_64-dvd.iso
Requires CentOS 7.4+ (1708)
EOF
}
while getopts ":vhq" OPTION; do
case $OPTION in
v)
echo "$0: HARDENED CentOS 7 DVD CREATOR Version: 1.2.1"
;;
h)
usage
exit 0
;;
q)
QUIET=1
;;
?)
echo "ERROR: Invalid Option Provided!"
echo
usage
exit 1
;;
esac
done
shift $((OPTIND-1))
if [ -z "$1" ]; then
usage
exit 1
fi
# Check for root user
if [[ $EUID -ne 0 ]]; then
if [ -z "$QUIET" ]; then
echo
tput setaf 1;echo -e "\033[1mThis script will attempt to use sudo execute commands as root!\033[0m";tput sgr0
SUDO="sudo -u root"
else
SUDO="sudo -nu root"
fi
else
SUDO=""
fi
# Check for required packages
which genisoimage &> /dev/null
if [ $? -ne 0 ]; then
$SUDO yum install -y genisoimage || $SUDO apt-get install -y genisoimage || {
which genisoimage || exit 1
}
fi
which isohybrid &> /dev/null
if [ $? -ne 0 ]; then
$SUDO yum install -y syslinux || $SUDO apt-get install -y syslinux || {
which syslinux || exit 1
}
fi
which implantisomd5 &> /dev/null
if [ $? -ne 0 ]; then
$SUDO yum install -y isomd5sum || $SUDO apt-get install -y isomd5sum || {
which implantisomd5 || exit 1
}
fi
# Determine if DVD is Bootable
`file $1 | grep -q -e "9660.*boot" -e "x86 boot" -e "DOS/MBR boot"`
if [[ $? -eq 0 ]]; then
echo "Mounting CentOS DVD Image..."
mkdir -p $DIR/original-mnt
mkdir $DIR/hardened-tmp
$SUDO mount -o loop $1 $DIR/original-mnt
if [[ -e $DIR/original-mnt/.discinfo && -e $DIR/original-mnt/.treeinfo ]]; then
CENTOS_VERSION=$(grep -E "^7\.[0-9]+" $DIR/original-mnt/.discinfo)
MAJOR=$(echo $CENTOS_VERSION | awk -F '.' '{ print $1 }')
MINOR=$(echo $CENTOS_VERSION | awk -F '.' '{ print $2 }')
BUILD=$(cd $DIR/original-mnt/Packages/; ls centos-release* | awk -F '.' '{ print $2 }')
ARCH=$(cd $DIR/original-mnt/Packages/; ls centos-release* | awk -F '.' '{ print $5 }')
HARDENED_ISO="CentOS-$MAJOR.$MINOR-$ARCH-DVD-$BUILD-hardened.iso"
if [[ $MAJOR -ne 7 ]]; then
echo "ERROR: Image is not CentOS 7.4+"
$SUDO umount $DIR/original-mnt
$SUDO rm -rf $DIR/original-mnt
exit 1
fi
if [[ $MINOR -lt 4 ]]; then
echo "ERROR: Image is not CentOS 7.4+"
$SUDO umount $DIR/original-mnt
$SUDO rm -rf $DIR/original-mnt
exit 1
fi
else
echo "ERROR: Image is not CentOS"
$SUDO umount $DIR/original-mnt
$SUDO rm -rf $DIR/original-mnt
exit 1
fi
echo "Done."
echo -n "Copying CentOS DVD Image..."
cp -a $DIR/original-mnt/* $DIR/hardened-tmp/
cp -a $DIR/original-mnt/.*info $DIR/hardened-tmp/
echo " Done."
$SUDO umount $DIR/original-mnt
rm -rf $DIR/original-mnt
else
echo "ERROR: ISO image is not bootable."
exit 1
fi
echo -n "Modifying CentOS DVD Image..."
cp -a $DIR/config/* $DIR/hardened-tmp/
echo " Done."
echo "Remastering CentOS DVD Image..."
cd $DIR/hardened-tmp
chmod u+w isolinux/isolinux.bin
find . -name TRANS.TBL -exec rm -f '{}' \;
genisoimage -l -r -J -V "CentOS 7 x86_64" -b isolinux/isolinux.bin -no-emul-boot -boot-load-size 4 -boot-info-table -c isolinux/boot.cat -o $DIR/$HARDENED_ISO -eltorito-alt-boot -e images/efiboot.img -no-emul-boot .
cd $DIR
rm -rf $DIR/hardened-tmp
echo "Done."
echo "Signing CentOS DVD Image..."
/usr/bin/isohybrid --uefi $DIR/$HARDENED_ISO &> /dev/null
/usr/bin/implantisomd5 $DIR/$HARDENED_ISO
echo "Done."
echo "DVD Created. [$HARDENED_ISO]"
echo "Creating the DVD metadata file..."
FULL_USER_NAME=$(getent passwd "$(whoami)" | cut -d ':' -f 5)
CREATION_DATE=$(date --utc --rfc-2822)
SCRIPT_NAME=$(basename $0)
SCRIPT_VERSION=$(grep -iE "# +version" createiso.sh | tail -1 | cut -d ' ' -f 3)
ORIGINAL_ISO_HASH=$(sha256sum "$1")
HARDENED_ISO_HASH=$(sha256sum "$DIR"/"$HARDENED_ISO")
SCRIPT_HASH=$(sha256sum "$SCRIPT_NAME")
cat <<EOF > "$DIR"/"${HARDENED_ISO%.*}.info.txt"
Prepared By: $FULL_USER_NAME
Creation Date: $CREATION_DATE
Compile Script: $SCRIPT_NAME
Version: $SCRIPT_VERSION
Hash: $SCRIPT_HASH
Project Source: hardened-centos7-kickstart
Project URL: https://github.com/fcaviggia/hardened-centos7-kickstart
$ORIGINAL_ISO_HASH
$HARDENED_ISO_HASH
Mainline Downloads: https://www.centos.org/download/
Rolling Downloads: https://buildlogs.centos.org/rolling/7/isos/x86_64/
EOF
echo "Done."
exit 0