Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I28 enhance asset tags #37

Merged
merged 2 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions source/app/alembic/versions/0db700644a4f_add_tags_to_assets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""Add tags to assets

Revision ID: 0db700644a4f
Revises: 6a3b3b627d45
Create Date: 2022-01-06 13:47:12.648707

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy import engine_from_config
from sqlalchemy.engine import reflection

# revision identifiers, used by Alembic.
revision = '0db700644a4f'
down_revision = '6a3b3b627d45'
branch_labels = None
depends_on = None


def upgrade():
# Now issue changes on existing tables and migrate Asset tags
# Add column asset_tags to CaseAssets if not existing
if not _table_has_column('case_assets', 'asset_tags'):
op.add_column('case_assets',
sa.Column('asset_tags', sa.Text)
)

if _table_has_column('case_assets', 'asset_tags'):
# Set schema and make migration of data
t_case_assets = sa.Table(
'case_assets',
sa.MetaData(),
sa.Column('asset_id', sa.Integer, primary_key=True),
sa.Column('asset_name', sa.Text),
sa.Column('asset_description', sa.Text),
sa.Column('asset_domain', sa.Text),
sa.Column('asset_ip', sa.Text),
sa.Column('asset_info', sa.Text),
sa.Column('asset_compromised', sa.Boolean),
sa.Column('asset_type_id', sa.ForeignKey('asset_type.asset_id')),
sa.Column('asset_tags', sa.Text),
sa.Column('case_id', sa.ForeignKey('cases.case_id')),
sa.Column('date_added', sa.DateTime),
sa.Column('date_update', sa.DateTime),
sa.Column('user_id', sa.ForeignKey('user.id')),
sa.Column('analysis_status_id', sa.ForeignKey('analysis_status.id'))
)

# Migrate existing Assets
conn = op.get_bind()
res = conn.execute("SELECT asset_id from case_assets WHERE asset_tags IS NULL;")
results = res.fetchall()

if results:

for res in results:
conn.execute(t_case_assets.update().where(t_case_assets.c.asset_id == res[0]).values(
asset_tags=''
))


def downgrade():
pass


def _table_has_column(table, column):
config = op.get_context().config
engine = engine_from_config(
config.get_section(config.config_ini_section), prefix='sqlalchemy.')
insp = reflection.Inspector.from_engine(engine)
has_column = False

for col in insp.get_columns(table):
if column != col['name']:
continue
has_column = True
return has_column
1 change: 1 addition & 0 deletions source/app/blueprints/case/case_assets_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ def asset_view_modal(cur_id, caseid, url_redir):
form.asset_compromised.data = True if asset.asset_compromised else False
form.asset_type_id.choices = get_assets_types()
form.analysis_status_id.choices = get_analysis_status_list()
form.asset_tags.render_kw = {'value': asset.asset_tags}

return render_template("modal_add_case_asset.html", form=form, asset=asset, map={}, ioc=case_iocs,
ioc_prefill=ioc_prefill)
Expand Down
16 changes: 10 additions & 6 deletions source/app/blueprints/case/templates/case_assets.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% extends "layouts/default_ext.html" %} {% block title %} Case Assets {% endblock title %} {% block stylesheets %}
<link rel="stylesheet" href="/static/assets/css/bootstrap-select.min.css">
<link rel="stylesheet" href="/static/assets/css/suggestags.css">
<link rel="stylesheet" href="/static/assets/css/select2.css">
<link href="/static/assets/css/dataTables.buttons.min.css" rel="stylesheet"> {% endblock stylesheets %}
{% block content %}
Expand Down Expand Up @@ -47,22 +48,24 @@
<thead>
<tr>
<th>Name</th>
<th>Compromised</th>
<th>Type</th>
<th>Description</th>
<th>IOC</th>
<th>IP</th>
<th>Type</th>
<th>Compromised</th>
<th>IOC</th>
<th>Tags</th>
<th>Analysis</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Compromised</th>
<th>Type</th>
<th>Description</th>
<th>IOC</th>
<th>IP</th>
<th>Type</th>
<th>Compromised</th>
<th>IOC</th>
<th>Tags</th>
<th>Analysis</th>
</tr>
</tfoot>
Expand Down Expand Up @@ -92,6 +95,7 @@
{% block javascripts %}

<script src="/static/assets/js/core/jquery.validate.js"></script>
<script src="/static/assets/js/plugin/tagsinput/suggesttag.js"></script>
<script src="/static/assets/js/plugin/datatables/dataTables.cellEdit.js"></script>
<script src="/static/assets/js/plugin/datatables/dataTables.buttons.min.js"></script>
<script src="/static/assets/js/plugin/datatables/buttons.html5.min.js"></script>
Expand Down
10 changes: 10 additions & 0 deletions source/app/blueprints/case/templates/modal_add_case_asset.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ <h4>{{ "Asset {}".format(asset.asset_name) if asset.asset_name else "Add asset"
{{ form.analysis_status_id(class="selectpicker pl--6 col-md-12") }}
</div>
</div>
<div class="form-group mt-3">
<label for="asset_tags">Asset tags
</label>
<input type="text" id="asset_tags"
class="form-control col-md-12" {% if asset.asset_tags %} value="{{ asset.asset_tags }}" {% endif %}/>
</div>
<div class="form-group" data-select2-id="7">
<label>Linked IOC</label>
<div class="select2-input ml-12" data-select2-id="6">
Expand All @@ -84,6 +90,10 @@ <h4>{{ "Asset {}".format(asset.asset_name) if asset.asset_name else "Add asset"
</div>
<script>
$('form#form_new_case').validate();
$('#asset_tags').amsifySuggestags({
printValues: false,
suggestions: [ {% for tag in tags %} '{{ tag }}', {% endfor %} ]
});
$('#asset_type_id').selectpicker({
liveSearch: true,
title: "None",
Expand Down
6 changes: 4 additions & 2 deletions source/app/datamgmt/case/case_assets_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def get_assets(caseid):
CaseAssets.asset_compromised,
CaseAssets.asset_ip,
CaseAssets.asset_type_id,
AnalysisStatus.name.label('analysis_status')
AnalysisStatus.name.label('analysis_status'),
CaseAssets.asset_tags
).filter(
CaseAssets.case_id == caseid
).join(
Expand Down Expand Up @@ -80,7 +81,7 @@ def get_asset(asset_id, caseid):


def update_asset(asset_name, asset_description, asset_ip, asset_info, asset_domain,
asset_compromised, asset_type, asset_id, caseid, analysis_status):
asset_compromised, asset_type, asset_id, caseid, analysis_status, asset_tags):
asset = get_asset(asset_id, caseid)
asset.asset_name = asset_name
asset.asset_description = asset_description
Expand All @@ -90,6 +91,7 @@ def update_asset(asset_name, asset_description, asset_ip, asset_info, asset_doma
asset.asset_compromised = asset_compromised
asset.asset_type_id = asset_type
asset.analysis_status_id = analysis_status
asset.asset_tags = asset_tags

update_assets_state(caseid=caseid)

Expand Down
1 change: 1 addition & 0 deletions source/app/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class AssetBasicForm(FlaskForm):
asset_compromised = BooleanField(u'Is Compromised')
asset_type_id = SelectField(u'Asset Type', validators=[DataRequired()])
analysis_status_id = SelectField(u'Analysis Status', validators=[DataRequired()])
asset_tags = StringField(u'Asset Tags')


class AssetComputerForm(AssetBasicForm):
Expand Down
1 change: 1 addition & 0 deletions source/app/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class CaseAssets(db.Model):
asset_info = Column(Text)
asset_compromised = Column(Boolean)
asset_type_id = Column(ForeignKey('assets_type.asset_id'))
asset_tags = Column(Text)
case_id = Column(ForeignKey('cases.case_id'))
date_added = Column(DateTime)
date_update = Column(DateTime)
Expand Down
47 changes: 31 additions & 16 deletions source/app/static/assets/js/iris/case.asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function add_asset() {
if (typeof data["ioc_links"] == "string") {
data["ioc_links"] = [data["ioc_links"]]
}
data['asset_tags'] = $('#asset_tags').val();

$.ajax({
url: 'assets/add' + case_param(),
Expand Down Expand Up @@ -124,11 +125,12 @@ Table = $("#assets_table").DataTable({
return data;
}
},
{ "data": "asset_compromised",
"render": function(data, type, row) {
if (data == true) { ret = '<span class="badge badge-danger">Yes</span>';} else { ret = '<span class="badge badge-success">No</span>'}
return ret;
}
{
"data": "asset_type",
"render": function (data, type, row, meta) {
if (type === 'display') { data = sanitizeHTML(data);}
return data;
}
},
{ "data": "asset_description",
"render": function (data, type, row, meta) {
Expand All @@ -146,6 +148,18 @@ Table = $("#assets_table").DataTable({
return data;
}
},
{ "data": "asset_ip",
"render": function (data, type, row, meta) {
if (type === 'display') { data = sanitizeHTML(data);}
return data;
}
},
{ "data": "asset_compromised",
"render": function(data, type, row) {
if (data == true) { ret = '<span class="badge badge-danger">Yes</span>';} else { ret = '<span class="badge badge-success">No</span>'}
return ret;
}
},
{ "data": "ioc",
"render": function (data, type, row, meta) {
if (type === 'display' && data != undefined) {
Expand All @@ -158,18 +172,18 @@ Table = $("#assets_table").DataTable({
return data;
}
},
{ "data": "asset_ip",
"render": function (data, type, row, meta) {
if (type === 'display') { data = sanitizeHTML(data);}
return data;
}
},
{
"data": "asset_type",
"render": function (data, type, row, meta) {
if (type === 'display') { data = sanitizeHTML(data);}
return data;
{ "data": "asset_tags",
"render": function (data, type, row, meta) {
if (type === 'display') {
tags = "";
de = data.split(',');
for (tag in de) {
tags += '<span class="badge badge-light ml-2">' + sanitizeHTML(de[tag]) + '</span>';
}
return tags;
}
return data;
}
},
{
"data": "analysis_status",
Expand Down Expand Up @@ -300,6 +314,7 @@ function asset_details(asset_id) {
if (typeof data["ioc_links"] == "string") {
data["ioc_links"] = [data["ioc_links"]]
}
data['asset_tags'] = $('#asset_tags').val();

$.ajax({
url: 'assets/update/' + asset_id + case_param(),
Expand Down