From 13064879e097f682c556d043e287a180c2cefaee Mon Sep 17 00:00:00 2001 From: Simone Margaritelli Date: Thu, 31 Oct 2019 14:14:13 +0100 Subject: [PATCH] fix: fixed, refactored and centralized launchers logic (closes #473) --- builder/data/usr/bin/bettercap-launcher | 18 +++----- builder/data/usr/bin/bootblink | 10 ----- builder/data/usr/bin/monstart | 3 +- builder/data/usr/bin/monstop | 3 +- builder/data/usr/bin/pwnagotchi-launcher | 19 +++------ builder/data/usr/bin/pwnlib | 54 ++++++++++++++++++++++++ builder/pwnagotchi.json | 10 ++--- 7 files changed, 77 insertions(+), 40 deletions(-) delete mode 100755 builder/data/usr/bin/bootblink create mode 100644 builder/data/usr/bin/pwnlib diff --git a/builder/data/usr/bin/bettercap-launcher b/builder/data/usr/bin/bettercap-launcher index c3cdde2..8b02ada 100755 --- a/builder/data/usr/bin/bettercap-launcher +++ b/builder/data/usr/bin/bettercap-launcher @@ -1,15 +1,11 @@ #!/usr/bin/env bash -# start mon0 -/usr/bin/monstart +source /usr/bin/pwnlib -# if usb0 or eth0 are up -if [[ $(ifconfig | grep usb0 | grep RUNNING) ]] || [[ !$(grep '1' /sys/class/net/eth0/carrier) ]]; then - # if override file exists, go into auto mode - if [ -f /root/.pwnagotchi-auto ]; then - /usr/bin/bettercap -no-colors -caplet pwnagotchi-auto -iface mon0 - else - /usr/bin/bettercap -no-colors -caplet pwnagotchi-manual -iface mon0 - fi -else +# start mon0 +start_monitor_interface + +if is_auto_mode; then /usr/bin/bettercap -no-colors -caplet pwnagotchi-auto -iface mon0 +else + /usr/bin/bettercap -no-colors -caplet pwnagotchi-manual -iface mon0 fi diff --git a/builder/data/usr/bin/bootblink b/builder/data/usr/bin/bootblink deleted file mode 100755 index 7a3bbfd..0000000 --- a/builder/data/usr/bin/bootblink +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env bash -for i in $(seq 1 "$1"); -do -echo 0 >/sys/class/leds/led0/brightness -sleep 0.3 -echo 1 >/sys/class/leds/led0/brightness -sleep 0.3 -done -echo 0 >/sys/class/leds/led0/brightness -sleep 0.3 \ No newline at end of file diff --git a/builder/data/usr/bin/monstart b/builder/data/usr/bin/monstart index db86e4e..4c00a5c 100755 --- a/builder/data/usr/bin/monstart +++ b/builder/data/usr/bin/monstart @@ -1,2 +1,3 @@ #!/usr/bin/env bash -iw phy phy0 interface add mon0 type monitor && ifconfig mon0 up \ No newline at end of file +source /usr/bin/pwnlib +start_monitor_interface diff --git a/builder/data/usr/bin/monstop b/builder/data/usr/bin/monstop index f98cd2d..c90a3ae 100755 --- a/builder/data/usr/bin/monstop +++ b/builder/data/usr/bin/monstop @@ -1,2 +1,3 @@ #!/usr/bin/env bash -ifconfig mon0 down && iw dev mon0 del \ No newline at end of file +source /usr/bin/pwnlib +stop_monitor_interface \ No newline at end of file diff --git a/builder/data/usr/bin/pwnagotchi-launcher b/builder/data/usr/bin/pwnagotchi-launcher index 079467e..78c37fe 100755 --- a/builder/data/usr/bin/pwnagotchi-launcher +++ b/builder/data/usr/bin/pwnagotchi-launcher @@ -1,16 +1,11 @@ #!/usr/bin/env bash -# blink 10 times to signal ready state -/usr/bin/bootblink 10 & +source /usr/bin/pwnlib -# if usb0 or eth0 are up -if [[ $(ifconfig | grep usb0 | grep RUNNING) ]] || [[ !$(grep '1' /sys/class/net/eth0/carrier) ]]; then - # if override file exists, go into auto mode - if [ -f /root/.pwnagotchi-auto ]; then - rm /root/.pwnagotchi-auto - /usr/local/bin/pwnagotchi - else - /usr/local/bin/pwnagotchi --manual - fi -else +# blink 10 times to signal ready state +blink_led 10 & + +if is_auto_mode; then /usr/local/bin/pwnagotchi +else + /usr/local/bin/pwnagotchi --manual fi \ No newline at end of file diff --git a/builder/data/usr/bin/pwnlib b/builder/data/usr/bin/pwnlib new file mode 100644 index 0000000..811d90f --- /dev/null +++ b/builder/data/usr/bin/pwnlib @@ -0,0 +1,54 @@ +#!/usr/bin/env bash + +# well ... it blinks the led +blink_led() { + for i in $(seq 1 "$1"); do + echo 0 >/sys/class/leds/led0/brightness + sleep 0.3 + echo 1 >/sys/class/leds/led0/brightness + sleep 0.3 + done + echo 0 >/sys/class/leds/led0/brightness + sleep 0.3 +} + +# starts mon0 +start_monitor_interface() { + iw phy phy0 interface add mon0 type monitor && ifconfig mon0 up +} + +# stops mon0 +stop_monitor_interface() { + ifconfig mon0 down && iw dev mon0 del +} + +# returns 0 if the specificed network interface is up +is_interface_up() { + if grep -qi 'up' /sys/class/net/$1/operstate; then + return 0 + fi + return 1 +} + +# returns 0 if conditions for AUTO mode are met +is_auto_mode() { + # check override file first + if [ -f /root/.pwnagotchi-auto ]; then + # remove the override file if found + rm -rf /root/.pwnagotchi-auto + return 0 + fi + + # if usb0 is up, we're in MANU + if is_interface_up usb0; then + return 1 + fi + + # if eth0 is up (for other boards), we're in MANU + if is_interface_up eth0; then + return 1 + fi + + # no override, but none of the interfaces is up -> AUTO + return 0 +} diff --git a/builder/pwnagotchi.json b/builder/pwnagotchi.json index 56daeaf..c86468f 100644 --- a/builder/pwnagotchi.json +++ b/builder/pwnagotchi.json @@ -19,6 +19,11 @@ "apt-get install -y ansible" ] }, + { + "type": "file", + "source": "data/usr/bin/pwnlib", + "destination": "/usr/bin/pwnlib" + }, { "type": "file", "source": "data/usr/bin/bettercap-launcher", @@ -34,11 +39,6 @@ "source": "data/usr/bin/monstop", "destination": "/usr/bin/monstop" }, - { - "type": "file", - "source": "data/usr/bin/bootblink", - "destination": "/usr/bin/bootblink" - }, { "type": "file", "source": "data/usr/bin/monstart",