Keine Beschreibung

search_routes.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # IRIS Source Code
  2. # Copyright (C) 2024 - DFIR-IRIS
  3. # contact@dfir-iris.org
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 3 of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program; if not, write to the Free Software Foundation,
  17. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. from flask import Blueprint
  19. from flask import request
  20. from sqlalchemy import and_
  21. from app.iris_engine.utils.tracker import track_activity
  22. from app.models.models import Comments
  23. from app.models.authorization import Permissions
  24. from app.models.cases import Cases
  25. from app.models.models import Client
  26. from app.models.models import Ioc
  27. from app.models.models import IocType
  28. from app.models.models import Notes
  29. from app.models.models import Tlp
  30. from app.blueprints.access_controls import ac_api_requires
  31. from app.blueprints.responses import response_success
  32. search_rest_blueprint = Blueprint('search_rest', __name__)
  33. @search_rest_blueprint.route('/search', methods=['POST'])
  34. @ac_api_requires(Permissions.search_across_cases)
  35. def search_file_post():
  36. jsdata = request.get_json()
  37. search_value = jsdata.get('search_value')
  38. search_type = jsdata.get('search_type')
  39. files = []
  40. search_condition = and_()
  41. track_activity("started a global search for {} on {}".format(search_value, search_type))
  42. if search_type == "ioc":
  43. res = Ioc.query.with_entities(
  44. Ioc.ioc_value.label('ioc_name'),
  45. Ioc.ioc_description.label('ioc_description'),
  46. Ioc.ioc_misp,
  47. IocType.type_name,
  48. Tlp.tlp_name,
  49. Tlp.tlp_bscolor,
  50. Cases.name.label('case_name'),
  51. Cases.case_id,
  52. Client.name.label('customer_name')
  53. ).filter(
  54. and_(
  55. Ioc.ioc_value.like(search_value),
  56. Ioc.case_id == Cases.case_id,
  57. Client.client_id == Cases.client_id,
  58. Ioc.ioc_tlp_id == Tlp.tlp_id,
  59. search_condition
  60. )
  61. ).join(Ioc.ioc_type).all()
  62. files = [row._asdict() for row in res]
  63. if search_type == "notes":
  64. ns = []
  65. if search_value:
  66. search_value = "%{}%".format(search_value)
  67. ns = Notes.query.filter(
  68. Notes.note_content.like(search_value),
  69. Cases.client_id == Client.client_id,
  70. search_condition
  71. ).with_entities(
  72. Notes.note_id,
  73. Notes.note_title,
  74. Cases.name.label('case_name'),
  75. Client.name.label('client_name'),
  76. Cases.case_id
  77. ).join(
  78. Notes.case
  79. ).order_by(
  80. Client.name
  81. ).all()
  82. ns = [row._asdict() for row in ns]
  83. files = ns
  84. if search_type == "comments":
  85. search_value = "%{}%".format(search_value)
  86. comments = Comments.query.filter(
  87. Comments.comment_text.like(search_value),
  88. Cases.client_id == Client.client_id,
  89. search_condition
  90. ).with_entities(
  91. Comments.comment_id,
  92. Comments.comment_text,
  93. Cases.name.label('case_name'),
  94. Client.name.label('customer_name'),
  95. Cases.case_id
  96. ).join(
  97. Comments.case
  98. ).join(
  99. Cases.client
  100. ).order_by(
  101. Client.name
  102. ).all()
  103. files = [row._asdict() for row in comments]
  104. return response_success("Results fetched", files)