RE env for inspecting APKs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
5.3 KiB

  1. import asyncio
  2. import threading
  3. from scapy.all import *
  4. import argparse
  5. DEBUG=False
  6. def print_dbg(s):
  7. if DEBUG:
  8. print(s)
  9. class NGServer:
  10. def __init__(self, bridge_ip='127.0.0.1', bridge_port=8000,
  11. ioip='127.0.0.1',
  12. incoming_port=8001, outgoing_port=8002):
  13. self.incoming_queue = None # asyncio.Queue()
  14. self.outgoing_queue = None # asyncio.Queue()
  15. self.client_reader = None
  16. self.client_writer = None
  17. self.server_reader = None
  18. self.server_writer = None
  19. self.server_thread = None
  20. self.bridge_ip = bridge_ip
  21. self.bridge_port = bridge_port
  22. self.ioip = ioip
  23. self.incoming_port = incoming_port
  24. self.outgoing_port = outgoing_port
  25. async def handle_client(self, reader, writer):
  26. self.client_reader = reader
  27. self.client_writer = writer
  28. while True:
  29. data = await reader.read(1024)
  30. if not data:
  31. writer.close()
  32. break
  33. await self.incoming_queue.put(data)
  34. async def handle_incoming(self, reader, writer):
  35. self.incoming_queue = asyncio.Queue()
  36. while True:
  37. data = await self.incoming_queue.get()
  38. writer.write(data)
  39. await writer.drain()
  40. async def handle_outgoing(self, reader, writer):
  41. self.server_reader = reader
  42. self.server_writer = writer
  43. print_dbg("handle_outgoing - ENTER")
  44. self.outgoing_queue = asyncio.Queue()
  45. while True:
  46. print_dbg("handle_outgoing - IN LOOP")
  47. data = await reader.read(1024)
  48. print_dbg("handle_outgoing - IP(data))={}".format(
  49. IP(data)))
  50. self.client_writer.write(data)
  51. await self.client_writer.drain()
  52. async def read_from_server(self):
  53. print_dbg("read_from_server - ENTER")
  54. while True:
  55. # Switch reader to writer
  56. print_dbg("read_from_server - IN LOOP")
  57. data = await self.client_reader.read(1024)
  58. print_dbg("read_from_server - IP(data))={}".format(
  59. IP(data)))
  60. if not data:
  61. self.client_writer.close()
  62. break
  63. await self.outgoing_queue.put(data)
  64. async def start_server(self):
  65. server = await asyncio.start_server(
  66. self.handle_client,
  67. host=self.bridge_ip,
  68. port=self.bridge_port)
  69. async with server:
  70. await server.serve_forever()
  71. async def start_incoming_server(self):
  72. server = await asyncio.start_server(
  73. self.handle_incoming,
  74. host=self.ioip,
  75. port=self.incoming_port)
  76. async with server:
  77. await server.serve_forever()
  78. async def start_outgoing_server(self):
  79. server = await asyncio.start_server(
  80. self.handle_outgoing,
  81. host=self.ioip,
  82. port=self.outgoing_port)
  83. async with server:
  84. await server.serve_forever()
  85. async def start_thread(ng):
  86. """ """
  87. await asyncio.gather(
  88. ng.start_server(),
  89. ng.start_incoming_server(),
  90. ng.start_outgoing_server(),
  91. )
  92. await asyncio.gather(
  93. ng.read_from_server(),
  94. ng.handle_incoming(),
  95. ng.handle_outgoing(),
  96. )
  97. def run_thread(ng):
  98. asyncio.run(start_thread(ng))
  99. def startServerAndWait(args):
  100. ng = NGServer(bridge_ip=args.bridge_ip,
  101. bridge_port=args.bridge_port,
  102. ioip=args.ioip,
  103. incoming_port=args.incoming_port,
  104. outgoing_port=args.outgoing_port)
  105. ng.server_thread = threading.Thread(
  106. target=run_thread,
  107. args=(ng,))
  108. ng.server_thread.start()
  109. ng.server_thread.join()
  110. def startThread(ng):
  111. ng.server_thread = threading.Thread(
  112. target=run_thread,
  113. args=(ng,))
  114. ng.server_thread.start()
  115. def connectIO(inaddr='127.0.0.1', inport=8001, outaddr='127.0.0.1', outport=8002):
  116. insock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
  117. insock.connect((inaddr, inport))
  118. outsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
  119. outsock.connect((outaddr, outport))
  120. return insock, outsock
  121. def parseArgs():
  122. args = argparse.ArgumentParser()
  123. # The bridge ip and port
  124. args.add_argument("-B","--bridge_ip", default='127.0.0.1',
  125. help='The IP that listens for NetGenie mobile app client connections.')
  126. args.add_argument("-b","--bridge_port", default=8000, help='The port that listens for NetGenie mobile app client connections.')
  127. # The IO ip and ports
  128. args.add_argument("-I","--ioip", default='127.0.0.1', help='The IP that listens on the server for connections from whoever is debugging the phone.')
  129. args.add_argument("-i","--incoming_port", default=8001,
  130. help='The port that the person debugging will read packets from the device.')
  131. args.add_argument("-o","--outgoing_port", default=8002,
  132. help='The port that the person debugging will write packets to the device.')
  133. return args.parse_args()
  134. def main():
  135. """
  136. The NGServer class implements our NetGenie debug/bridge server.
  137. """
  138. # Get the arguments
  139. args = parseArgs()
  140. # Start the server.
  141. startServerAndWait(args)
  142. if __name__ == '__main__':
  143. main()