simulator_proberesponse.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # This is a customized version of the TraCINg attack simulator needed for
  2. # simulating probe response attacks.
  3. #
  4. # Requirements:
  5. # - Python 3.x
  6. # - requests
  7. #
  8. # TraCINg-Server - Gathering and visualizing cyber incidents on the world
  9. #
  10. # Copyright 2013 Matthias Gazzari, Annemarie Mattmann, André Wolski
  11. #
  12. # Licensed under the Apache License, Version 2.0 (the "License");
  13. # you may not use this file except in compliance with the License.
  14. # You may obtain a copy of the License at
  15. #
  16. # http://www.apache.org/licenses/LICENSE-2.0
  17. #
  18. # Unless required by applicable law or agreed to in writing, software
  19. # distributed under the License is distributed on an "AS IS" BASIS,
  20. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. # See the License for the specific language governing permissions and
  22. # limitations under the License.
  23. import logging
  24. import requests
  25. import random
  26. import json
  27. import time
  28. import sys
  29. import http.client
  30. import time
  31. # In order to enable server certificate check activate this imports
  32. #import ssl
  33. # disable warnings as we are simulating locally
  34. logging.captureWarnings(True)
  35. logger = logging.getLogger("probe_response_attack")
  36. # based on http://stackoverflow.com/questions/10218486/set-nested-dict-value-and-create-intermediate-keys
  37. #from collections import defaultdict
  38. #recursivedict = lambda: defaultdict(recursivedict)
  39. # constants and defaults
  40. sensor_name_base = "Simulator"
  41. sensorType = "Honeypot"
  42. #url = "https://localhost:9999"
  43. url = "https://localhost:443"
  44. cert = ("ssl/simulator/simulator_cert.pem", "ssl/simulator/simulator_key.pem")
  45. """
  46. headers = {"Content-type": "application/x-www-form-urlencoded",
  47. "Accept": "text/plain"}
  48. """
  49. #conn = http.client.HTTPSConnection("localhost", 443)
  50. request_session = requests.Session()
  51. incidentTypes = {
  52. 0: "Unknown",
  53. 10: "Transport Layer",
  54. 11: "Portscan",
  55. 20: "Shellcode Injection",
  56. 30: "SQL",
  57. 31: "MySQL",
  58. 32: "MS SQL",
  59. 40: "SMB",
  60. 50: "VoIP",
  61. 60: "Invalid" # invalid test case
  62. }
  63. def get_full_entry(ip_src=None, port_src=None, ip_dst=None, port_dst=None, sensor_name=None):
  64. """
  65. Return an entry using every fields possible.
  66. """
  67. if sensor_name is None:
  68. sensor_name = sensor_name_base + ip_dst
  69. return {
  70. "sensor": {
  71. "name": sensor_name,
  72. "type": sensorType,
  73. },
  74. "src": {
  75. "ip": ip_src,
  76. "port": port_src,
  77. },
  78. "dst": {
  79. "ip": ip_dst,
  80. "port": port_dst,
  81. },
  82. "type": 11,
  83. "log": "Random Testlog",
  84. "md5sum": "0x0123456789",
  85. "date": int(time.time()),
  86. }
  87. def send_accident(ip_src, port_src, ip_dst, port_dst, url=url, cert=None, verify=False):
  88. """
  89. Send an accident to the server.
  90. """
  91. entry = get_full_entry(ip_src=ip_src, port_src=port_src, ip_dst=ip_dst, port_dst=port_dst)
  92. #logger.warning("sending entry: %r" % entry)
  93. try:
  94. #s.headers.update(headers)
  95. request_session.post(url=url, data=json.dumps(entry), verify=False)
  96. #conn.request("POST", "/", body=json.dumps(entry))
  97. #response = requests.post(url, cert=cert, verify=verify, data=json.dumps(entry))
  98. except Exception as e:
  99. logger.warning("could not send request: %r" % e)
  100. rr = random.randrange
  101. def attack_single_source_single_dest(amount):
  102. for x in range(amount):
  103. ip_src = "12.34.56.78"
  104. ip_dst = "56.34.56.79"
  105. send_accident(
  106. ip_src=ip_src, port_src=65535,
  107. ip_dst=ip_dst, port_dst=65535)
  108. def attack_single_source_multiple_dest(amount):
  109. for x in range(amount):
  110. ip_src = "12.34.56.78"
  111. ip_dst = "%d.%d.%d.%d" % (rr(0, 255), rr(0, 255), rr(0, 255), rr(0, 255))
  112. send_accident(
  113. ip_src=ip_src, port_src=65535,
  114. ip_dst=ip_dst, port_dst=65535)
  115. def attack_multiple_source_multiple_dest(amount):
  116. for x in range(amount):
  117. ip_src = "%d.%d.%d.%d" % (rr(0, 255), rr(0, 255), rr(0, 255), rr(0, 255))
  118. ip_dst = "%d.%d.%d.%d" % (rr(0, 255), rr(0, 255), rr(0, 255), rr(0, 255))
  119. send_accident(
  120. ip_src=ip_src, port_src=65535,
  121. ip_dst=ip_dst, port_dst=65535)
  122. def attack_multiple_source_single_dest(amount):
  123. for x in range(amount):
  124. ip_src = "%d.%d.%d.%d" % (rr(0, 255), rr(0, 255), rr(0, 255), rr(0, 255))
  125. ip_dst = "12.34.56.78"
  126. send_accident(
  127. ip_src=ip_src, port_src=65535,
  128. ip_dst=ip_dst, port_dst=65535)
  129. def attack_single_source_multiple_destport(amount):
  130. for x in range(amount):
  131. ip_src = "12.34.56.78"
  132. ip_dst = "12.34.56.79"
  133. send_accident(
  134. ip_src=ip_src, port_src=65535,
  135. ip_dst=ip_dst, port_dst=rr(0, 65535))
  136. if __name__ == '__main__':
  137. if len(sys.argv) <= 1:
  138. send_accident( ip_src="123.45.67.89",
  139. port_src=65535,
  140. ip_dst="23.45.67.89",
  141. port_dst=65535)
  142. send_accident( ip_src="123.45.67.89",
  143. port_src=65535,
  144. ip_dst="23.45.67.89",
  145. port_dst=65535)
  146. else:
  147. amount = int(sys.argv[1])
  148. #attack_single_source_single_dest(amount)
  149. #attack_single_source_multiple_dest(amount)
  150. #attack_multiple_source_multiple_dest(amount)
  151. #attack_multiple_source_single_dest(amount)
  152. attack_single_source_multiple_destport(amount)