-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvm_injector.sh
executable file
·128 lines (116 loc) · 3.01 KB
/
vm_injector.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
#!/bin/bash
set -eu
# Path to the key to add in the machine
KEY="id_rsa"
# Privileged user
USER=root
# Unprivileged user
UNPRIVUSER=vagrant
# IP Address
IP=""
# Target port
PORT="22"
# Target image
IMG="target.img"
# Target operating system
TARGET_OS="OpenBSD"
# Base directory of the script
BASEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# Number of seconds to wait for the host
SECONDS_TO_WAIT=0
usage() {
echo "Usage: $1 <-i IP> [optargs]" >&2
echo -e "Optional arguments (optargs):" >&2
echo -e "\t-h print this help and exit" >&2
echo -e "\t-i IP Set the target IP address to IP" >&2
echo -e "\t-I IMAGE Set the target injected image to IMAGE" >&2
echo -e "\t-k KEY Set the target SSH key to KEY" >&2
echo -e "\t-p PORT Set the target SSH port to PORT" >&2
echo -e "\t-s SECONDS Number of seconds to wait for the host" >&2
echo -e "\t-t TARGET Set the target operating system to TARGET" >&2
echo -e "\t-u USER Set the target unprivileged user to USER" >&2
echo -e "\t-U USER Set the target privileged user to USER" >&2
}
while getopts "hi:I:k:p:s:t:u:U:" options; do
case "$options" in
i)
IP="$OPTARG"
;;
h)
usage "$0"
exit 1;
;;
t)
TARGET_OS="$OPTARG"
;;
s)
SECONDS_TO_WAIT="$OPTARG"
;;
p)
PORT="$OPTARG"
;;
k)
KEY="$OPTARG"
;;
u)
UNPRIVUSER="$OPTARG"
;;
U)
USER="$OPTARG"
;;
I)
IMG="$OPTARG"
;;
*)
echo "Warning: Option not recognized."
;;
esac
done
if [ "x$IP" == "x" ]; then
usage "$0"
exit 1
fi
echo "[ ] Checking if host is ready"
HOST_READY=false
for WAITED_SECONDS in $(seq 0 "$(( $SECONDS_TO_WAIT + 1 ))" ); do
ssh-keygen -R "$IP"
if ssh -p "$PORT" -q -n \
-o PasswordAuthentication=no \
-o StrictHostKeyChecking=no \
-i "$KEY" "$USER@$IP" 'true' ; then
echo -e "\n[+] Success! Host is ready."
HOST_READY=true
break
else
echo -n "."
sleep 1
fi
done
if ! $HOST_READY; then
echo -e "\n[-] Seems that the connection has problems, exiting"
exit 1
fi
# Check if the host is already the target one
OS="$(ssh -p "$PORT" -n -o PasswordAuthentication=no -o StrictHostKeyChecking=no -i "$KEY" "$UNPRIVUSER@$IP" 'uname -s' || true)"
if [ "x$OS" == "x$TARGET_OS" ]; then
echo "Seems that the system is already installed."
exit 0
fi
echo "Seems that this is the first run, nuking the system."
# This machine will get destroyed
scp -P "$PORT" -i "$KEY" "$BASEDIR/injectors/inject.sh" "$USER@$IP:/root/inject.sh"
EXT=$(echo "$IMG" | awk -F . '{print $NF}')
case $EXT in
ova)
scp -P "$PORT" -i "$KEY" "$IMG" "$USER@$IP:/root/target.ova"
;;
vmdk)
scp -P "$PORT" -i "$KEY" "$IMG" "$USER@$IP:/root/target.vmdk"
;;
*)
scp -P "$PORT" -i "$KEY" "$IMG" "$USER@$IP:/root/target.img"
;;
esac
ssh -p "$PORT" -o PasswordAuthentication=no -o StrictHostKeyChecking=no -i "$KEY" "$USER@$IP" /root/inject.sh || true
ssh-keygen -R "$IP"
echo "In 5 minute connect to the machine"