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.

165 lines
5.2 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 = asyncio.Queue()
  14. self.outgoing_queue = 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. while True:
  36. data = await self.incoming_queue.get()
  37. writer.write(data)
  38. await writer.drain()
  39. async def handle_outgoing(self, reader, writer):
  40. self.server_reader = reader
  41. self.server_writer = writer
  42. print_dbg("handle_outgoing - ENTER")
  43. while True:
  44. print_dbg("handle_outgoing - IN LOOP")
  45. data = await reader.read(1024)
  46. print_dbg("handle_outgoing - IP(data))={}".format(
  47. IP(data)))
  48. self.client_writer.write(data)
  49. await self.client_writer.drain()
  50. async def read_from_server(self):
  51. print_dbg("read_from_server - ENTER")
  52. while True:
  53. # Switch reader to writer
  54. print_dbg("read_from_server - IN LOOP")
  55. data = await self.client_reader.read(1024)
  56. print_dbg("read_from_server - IP(data))={}".format(
  57. IP(data)))
  58. if not data:
  59. self.client_writer.close()
  60. break
  61. await self.outgoing_queue.put(data)
  62. async def start_server(self):
  63. server = await asyncio.start_server(
  64. self.handle_client,
  65. host=self.bridge_ip,
  66. port=self.bridge_port)
  67. async with server:
  68. await server.serve_forever()
  69. async def start_incoming_server(self):
  70. server = await asyncio.start_server(
  71. self.handle_incoming,
  72. host=self.ioip,
  73. port=self.incoming_port)
  74. async with server:
  75. await server.serve_forever()
  76. async def start_outgoing_server(self):
  77. server = await asyncio.start_server(
  78. self.handle_outgoing,
  79. host=self.ioip,
  80. port=self.outgoing_port)
  81. async with server:
  82. await server.serve_forever()
  83. async def start_thread(ng):
  84. """ """
  85. await asyncio.gather(
  86. ng.start_server(),
  87. ng.start_incoming_server(),
  88. ng.start_outgoing_server(),
  89. )
  90. await asyncio.gather(
  91. ng.read_from_server(),
  92. ng.handle_incoming(),
  93. ng.handle_outgoing(),
  94. )
  95. def run_thread(ng):
  96. asyncio.run(start_thread(ng))
  97. def startServerAndWait(args):
  98. ng = NGServer(bridge_ip=args.bridge_ip,
  99. bridge_port=args.bridge_port,
  100. ioip=args.ioip,
  101. incoming_port=args.incoming_port,
  102. outgoing_port=args.outgoing_port)
  103. ng.server_thread = threading.Thread(
  104. target=run_thread,
  105. args=(ng,))
  106. ng.server_thread.start()
  107. ng.server_thread.join()
  108. def startThread(ng):
  109. ng.server_thread = threading.Thread(
  110. target=run_thread,
  111. args=(ng,))
  112. ng.server_thread.start()
  113. def connectIO(inaddr='127.0.0.1', inport=8001, outaddr='127.0.0.1', outport=8002):
  114. insock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
  115. insock.connect((inaddr, inport))
  116. outsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
  117. outsock.connect((outaddr, outport))
  118. return insock, outsock
  119. def parseArgs():
  120. args = argparse.ArgumentParser()
  121. # The bridge ip and port
  122. args.add_argument("-B","--bridge_ip", default='127.0.0.1',
  123. help='The IP that listens for NetGenie mobile app client connections.')
  124. args.add_argument("-b","--bridge_port", default=8000, help='The port that listens for NetGenie mobile app client connections.')
  125. # The IO ip and ports
  126. 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.')
  127. args.add_argument("-i","--incoming_port", default=8001,
  128. help='The port that the person debugging will read packets from the device.')
  129. args.add_argument("-o","--outgoing_port", default=8002,
  130. help='The port that the person debugging will write packets to the device.')
  131. return args.parse_args()
  132. def main():
  133. """
  134. The NGServer class implements our NetGenie debug/bridge server.
  135. """
  136. args = parseArgs()
  137. startServerAndWait(args)
  138. if __name__ == '__main__':
  139. main()