Guest User

Untitled

a guest
Sep 16th, 2018
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # This file is part of P4wnP1.
  4. #
  5. # Copyright (c) 2017, Marcus Mengs.
  6. #
  7. # P4wnP1 is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # P4wnP1 is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with P4wnP1. If not, see <http://www.gnu.org/licenses/>.
  19.  
  20. #
  21. # P4wnP1 startup script
  22. # Author: Marcus Mengs (MaMe82)
  23. #
  24. # Notes:
  25. # - work in progress (contains possible errors and typos)
  26. # - if the device isn't detected changing the USB port (USB 2.0 prefered) or plug out and in
  27. # again could help
  28. #
  29. # ToDo:
  30. # - add manual system date adjusment, to not mess up logs due to missing NTP (store datetime of last boot)
  31. # - add shutdown capability to script, to allow file system syncing before power loss (could be done from
  32. # payload by calling "sync && sudo halt")
  33.  
  34.  
  35. # find working dir of script
  36. wdir=$( cd $(dirname $BASH_SOURCE[0]) && cd .. && pwd)
  37.  
  38. echo =================================== P4wnP1 startup ===========================================
  39. #echo "P4wnP1: Init LED control..."
  40. #source $wdir/boot/init_led.sh
  41.  
  42. echo "P4wnP1: Loading config ..."
  43. source $wdir/boot/init_config.sh
  44.  
  45. # Create bash script which could be altered from /home/pi/.profile
  46. touch /tmp/profile.sh
  47. echo "#!/bin/bash" >> /tmp/profile.sh
  48. echo "wdir=$wdir" >> /tmp/profile.sh
  49. echo "lang=$lang" >> /tmp/profile.sh
  50. echo $(declare -p ledtrigger | cut -d" " -f3) >> /tmp/profile.sh
  51. declare -f led_blink >> /tmp/profile.sh
  52. declare -f onLogin >> /tmp/profile.sh
  53. chown pi:pi /tmp/profile.sh
  54.  
  55. echo "P4wnP1: Initializing USB gadget ..."
  56. source $wdir/boot/init_usb.sh
  57. init_usb
  58.  
  59. echo "P4wnP1: Checking for WiFi capabilities ..."
  60. source $wdir/boot/init_wifi.sh
  61. check_wifi
  62. if $WIFI; then
  63. echo "P4wnP1: Seems WiFi module is present !"
  64. iw reg set $WIFI_REG
  65.  
  66. source $wdir/boot/init_wifi_nexmon.sh
  67. if $WIFI_NEXMON; then
  68. # swap firmware and driver
  69. WIFI_activate_nexmon
  70.  
  71. fi
  72.  
  73.  
  74. # start WIFI client
  75. if $WIFI_CLIENT; then
  76. # try to connect to existing WiFi according to the config
  77. sleep 1 # pause to make new reg domain accessible in scan
  78. if start_wifi_client; then
  79. WIFI_CLIENT_CONNECTION_SUCCESS=true
  80. else
  81. echo "P4wnP1: Join present WiFi didn't succeed, failing over to access point mode"
  82. WIFI_CLIENT_CONNECTION_SUCCESS=false
  83. fi
  84. fi
  85.  
  86. # start ACCESS POINT if needed
  87. # - if WiFi client mode is disabled and ACCESPOINT mode is enabled
  88. # - if WiFi client mode is enabled, but failed and ACCESPOINT mode is enabled
  89. if $WIFI_ACCESSPOINT && ( ! $WIFI_CLIENT_CONNECTION_SUCCESS || ! $WIFI_CLIENT); then
  90. start_wifi_accesspoint
  91.  
  92. # check if acces point is up and trigger callback
  93. # Warning!!! This uses the SSID and isn't tested against hidden SSID configurations
  94. (
  95. AP_DOWN=true
  96. while $AP_DOWN; do
  97. iw dev | grep -q -E "ssid $WIFI_ACCESSPOINT_NAME"; res=$?
  98. if [ $res == 0 ]; then
  99. AP_DOWN=false
  100.  
  101. if $WIFI_ACCESSPOINT_KARMA && $WIFI_NEXMON; then
  102. WIFI_enable_KARMA
  103. fi
  104. if $WIFI_ACCESSPOINT_KARMA_LOUD && $WIFI_NEXMON; then
  105. WIFI_enable_KARMA_LOUD
  106. fi
  107.  
  108. declare -f onAccessPointUp > /dev/null && onAccessPointUp # run only once
  109. fi
  110. done
  111. )&
  112. fi
  113. fi
  114.  
  115. detect_usb_hostmode # creates OTG_MODE=true if P4wnP1 is in OTG mode
  116.  
  117. # early out if P4wnP1 is used in OTG mode
  118. if $OTG_MODE; then
  119. echo "As P4wnP1 is detected to run in Host (interactive) mode, we abort device setup now!"
  120. exit
  121. else
  122. echo "P4wnP1: ... USB gadget initialized"
  123. fi
  124.  
  125. # check if ethernet over USB should be used
  126. if $USB_RNDIS || $USB_ECM; then
  127. USB_ETHERNET=true
  128. fi
  129.  
  130. # change hostname to make P4wnP1 resolveable on "name.local"
  131. if $WIFI || $USB_ETHERNET; then
  132. hostname="NFC-Pi"
  133.  
  134. hostname $hostname
  135. echo $hostname > /etc/hostname
  136.  
  137. # add to /etc/hosts
  138. if ! grep -q -E "^127\.0\.0\.1 $hostname\$" /etc/hosts; then
  139. echo "127.0.0.1 $hostname" >> /etc/hosts
  140. fi
  141. fi
  142.  
  143.  
  144.  
  145. # if ethernet over USB is in use, detect active interface and start DHCP (all as background job)
  146. if $USB_ETHERNET; then
  147. echo "P4wnP1: Initializing Ethernet over USB..."
  148. source $wdir/boot/init_usb_ethernet.sh
  149. (
  150. detect_active_interface
  151.  
  152. if [ "$active_interface" != "none" ]; then
  153. create_DHCP_config
  154. dnsmasq -C /tmp/dnsmasq_usb_eth.conf
  155.  
  156. # callback onNetworkUp() of payload script
  157. declare -f onNetworkUp > /dev/null && onNetworkUp
  158.  
  159. # wait for client to receive DHCP lease
  160. target_ip=""
  161. while [ "$target_ip" == "" ]; do
  162. target_ip=$(cat /tmp/dnsmasq.leases | cut -d" " -f3)
  163. target_name=$(cat /tmp/dnsmasq.leases | awk '{print $4}')
  164. sleep 0.2
  165. done
  166.  
  167. # callback onNetworkGotIP() of payload script
  168. declare -f onTargetGotIP > /dev/null && onTargetGotIP
  169.  
  170. fi
  171. )&
  172. fi
  173.  
  174. # take care of AutoSSH reachback
  175. #source $wdir/boot/init_autossh.sh
  176. #start_autossh # start_autossh takes care of checking AUTOSSH_ENABLED
  177.  
  178. if $USE_HID_MOUSE; then
  179. # import mouse functions
  180. source $wdir/boot/init_hid_mouse.sh
  181. declare -f outmouse >> /tmp/profile.sh
  182. chown pi:pi /tmp/profile.sh
  183. fi
  184.  
  185. if $USE_HID; then
  186. # import keyboard functions
  187. source $wdir/boot/init_hid_keyboard.sh
  188.  
  189. declare -f outhid >> /tmp/profile.sh
  190. declare -f duckhid >> /tmp/profile.sh
  191. chown pi:pi /tmp/profile.sh
  192.  
  193. # if HID keyboard should be tested for readyness, do it (triggers "onKeyboardUp" callback of payload)
  194. if $HID_KEYBOARD_TEST; then
  195. # detect_HID_keyboard&
  196.  
  197. (
  198. echo "Waiting for HID keyboard to be usable..."
  199. # blocking read of LED status
  200. python -c "with open('/dev/hidg0','rb') as f: print ord(f.read(1))"
  201. # fire 'onKeyboardUp' after read has succeeded
  202. declare -f onKeyboardUp > /dev/null && onKeyboardUp
  203. )&
  204. fi
  205. fi
  206.  
  207.  
  208.  
  209. # trigger callback for on boot finished
  210. #(
  211. # declare -f onBootFinished > /dev/null && onBootFinished # run only once
  212. #)&
Advertisement
Add Comment
Please, Sign In to add comment