Skip to content

Commit

Permalink
Merge branch 'master' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
kueblc committed Nov 7, 2019
2 parents b9aebed + cee07b8 commit 8061647
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 50 deletions.
2 changes: 2 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
patreon: kueblc
custom: ['https://paypal.me/kueblc', 'https://www.buymeacoffee.com/kueblc']
2 changes: 1 addition & 1 deletion install_prereq.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -e

sudo apt-get update
sudo apt-get install -y dnsmasq hostapd screen curl python-pip python3-pip python-setuptools python3-setuptools python-wheel python3-wheel python-dev python3-dev mosquitto haveged net-tools libssl-dev
sudo apt-get install -y git dnsmasq hostapd screen curl build-essential python-pip python3-pip python-setuptools python3-setuptools python-wheel python3-wheel python-dev python3-dev mosquitto haveged net-tools libssl-dev

PY3_DEPENDENCIES="paho-mqtt pyaes tornado git+https://github.com/M4dmartig4n/sslpsk.git pycrypto"
PY2_DEPENDENCIES="git+https://github.com/M4dmartig4n/sslpsk.git pycrypto"
Expand Down
25 changes: 4 additions & 21 deletions scripts/setup_ap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,6 @@
# Source config
. ../config.txt

check_config () {
if ! iw list | grep -q "* AP"; then
echo "AP mode not supported!"
echo "Please attach a WiFi card that supports AP mode."
exit 1
fi

echo -n "Checking for network interface $WLAN... "
if [ -e /sys/class/net/$WLAN ]; then
echo "Found."
else
echo "Not found!"
echo -n "Please edit WLAN in config.txt to one of: "
ls -m /sys/class/net
exit 1
fi
}

setup () {
wpa_supplicant_pid=$(pidof wpa_supplicant)
if [ -n "$wpa_supplicant_pid" ]; then
Expand All @@ -34,8 +16,10 @@ setup () {
fi

echo "Configuring AP interface..."
sudo ifconfig $WLAN down
sudo ifconfig $WLAN up $GATEWAY netmask 255.255.255.0
sudo ip link set $WLAN down
sudo ip addr add $GATEWAY/24 dev $WLAN
sudo ip link set $WLAN up
sudo ip route add 10.42.42.0/24 dev $WLAN src $GATEWAY
sudo ip route add 255.255.255.255 dev $WLAN

echo "Starting DNSMASQ server..."
Expand Down Expand Up @@ -64,7 +48,6 @@ cleanup () {
fi
}

check_config
trap cleanup EXIT
setup

95 changes: 95 additions & 0 deletions scripts/setup_checks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/bin/bash

# Source config
. ../config.txt

check_eula () {
if [ ! -f eula_accepted ]; then
echo "======================================================"
echo "${bold}TUYA-CONVERT${normal}"
echo
echo "https://github.com/ct-Open-Source/tuya-convert"
echo "TUYA-CONVERT was developed by Michael Steigerwald from the IT security company VTRUST (https://www.vtrust.de/) in collaboration with the techjournalists Merlin Schumacher, Pina Merkert, Andrijan Moecker and Jan Mahn at c't Magazine. (https://www.ct.de/)"
echo
echo
echo "======================================================"
echo "${bold}PLEASE READ THIS CAREFULLY!${normal}"
echo "======================================================"
echo "TUYA-CONVERT creates a fake update server environment for ESP8266/85 based tuya devices. It enables you to backup your devices firmware and upload an alternative one (e.g. ESPEasy, Tasmota, Espurna) without the need to open the device and solder a serial connection (OTA, Over-the-air)."
echo "Please make sure that you understand the consequences of flashing an alternative firmware, since you might lose functionality!"
echo
echo "Flashing an alternative firmware can cause unexpected device behavior and/or render the device unusable. Be aware that you do use this software at YOUR OWN RISK! Please acknowledge that VTRUST and c't Magazine (or Heise Medien GmbH & Co. KG) CAN NOT be held accountable for ANY DAMAGE or LOSS OF FUNCTIONALITY by typing ${bold}yes + Enter${normal}"
echo
read
if [ "$REPLY" != "yes" ]; then
exit
fi
touch eula_accepted
fi
}

check_config () {
if ! iw list | grep -q "* AP"; then
echo "AP mode not supported!"
echo "Please attach a WiFi card that supports AP mode."
exit 1
fi

echo -n "Checking for network interface $WLAN... "
if [ -e /sys/class/net/$WLAN ]; then
echo "Found."
else
echo "Not found!"
echo -n "Please edit WLAN in config.txt to one of: "
ls -m /sys/class/net
exit 1
fi
}

check_port () {
protocol="$1"
port="$2"
reason="$3"
echo -n "Checking ${protocol^^} port $port... "
process_pid=$(sudo ss -Hlnp -A "$protocol" "sport = :$port" | grep -Po "(?<=pid=)(\d+)" | head -n1)
if [ -n "$process_pid" ]; then
process_name=$(ps -p "$process_pid" -o comm=)
echo "Occupied by $process_name with PID $process_pid."
echo "Port $port is needed to $reason"
read -p "Do you wish to terminate $process_name? [y/N] " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Aborting due to occupied port"
exit 1
else
service=$(ps -p "$process_pid" -o unit= | grep .service | grep -Ev ^user)
if [ -n "$service" ]; then
echo "Attempting to stop $service"
sudo systemctl stop "$service"
else
echo "Attempting to terminate $process_name"
sudo kill "$process_pid"
if ! sudo timeout 10 tail --pid="$process_pid" -f /dev/null; then
echo "$process_name is still running after 10 seconds, sending SIGKILL"
sudo kill -9 "$process_pid"
sudo tail --pid="$process_pid" -f /dev/null
fi
fi
sleep 1
fi
else
echo "Available."
fi
}

check_eula
check_config
check_port udp 53 "resolve DNS queries"
check_port udp 67 "offer DHCP leases"
check_port tcp 80 "answer HTTP requests"
check_port tcp 443 "answer HTTPS requests"
check_port udp 6666 "detect unencrypted Tuya firmware"
check_port udp 6667 "detect encrypted Tuya firmware"
check_port tcp 1883 "run MQTT"
check_port tcp 8886 "run MQTTS"

33 changes: 5 additions & 28 deletions start_flash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,19 @@ fi

pushd scripts >/dev/null

if [ ! -f eula_accepted ]; then
echo "======================================================"
echo "${bold}TUYA-CONVERT${normal}"
echo
echo "https://github.com/ct-Open-Source/tuya-convert"
echo "TUYA-CONVERT was developed by Michael Steigerwald from the IT security company VTRUST (https://www.vtrust.de/) in collaboration with the techjournalists Merlin Schumacher, Pina Merkert, Andrijan Moecker and Jan Mahn at c't Magazine. (https://www.ct.de/)"
echo
echo
echo "======================================================"
echo "${bold}PLEASE READ THIS CAREFULLY!${normal}"
echo "======================================================"
echo "TUYA-CONVERT creates a fake update server environment for ESP8266/85 based tuya devices. It enables you to backup your devices firmware and upload an alternative one (e.g. ESPEasy, Tasmota, Espurna) without the need to open the device and solder a serial connection (OTA, Over-the-air)."
echo "Please make sure that you understand the consequences of flashing an alternative firmware, since you might lose functionality!"
echo
echo "Flashing an alternative firmware can cause unexpected device behavior and/or render the device unusable. Be aware that you do use this software at YOUR OWN RISK! Please acknowledge that VTRUST and c't Magazine (or Heise Medien GmbH & Co. KG) CAN NOT be held accountable for ANY DAMAGE or LOSS OF FUNCTIONALITY by typing ${bold}yes + Enter${normal}"
echo
read
if [ "$REPLY" != "yes" ]; then
exit
fi
touch eula_accepted
fi
. ./setup_checks.sh

echo "======================================================"
echo -n " Starting AP in a screen"
$screen_with_log smarthack-wifi.log -S smarthack-wifi -m -d ./setup_ap.sh
while ! ping -c 1 -W 1 -n $GATEWAY &> /dev/null; do
printf .
done
echo
echo " Stopping any apache web server"
sudo service apache2 stop >/dev/null 2>&1
sleep 5
echo " Starting web server in a screen"
$screen_with_log smarthack-web.log -S smarthack-web -m -d ./fake-registration-server.py
echo " Starting Mosquitto in a screen"
sudo service mosquitto stop >/dev/null 2>&1
sudo pkill mosquitto
$screen_with_log smarthack-mqtt.log -S smarthack-mqtt -m -d mosquitto -v
echo " Starting PSK frontend in a screen"
$screen_with_log smarthack-psk.log -S smarthack-psk -m -d ./psk-frontend.py -v
Expand All @@ -73,15 +50,15 @@ echo "Starting smart config pairing procedure"

echo "Waiting for the device to install the intermediate firmware"

i=60
i=120
while ! ping -c 1 -W 1 -n 10.42.42.42 &> /dev/null; do
printf .
if (( --i == 0 )); then
echo
echo "Device did not appear with the intermediate firmware"
echo "Check the *.log files in the scripts folder"
pkill -f smartconfig/main.py && echo "Stopping smart config"
read -p "Do you want to flash another device? [y/N] " -n 1 -r
read -p "Do you want to try flashing another device? [y/N] " -n 1 -r
echo
continue 2
fi
Expand Down

0 comments on commit 8061647

Please sign in to comment.