Skip to content

Commit b524be3

Browse files
committed
examples, src: Merge ThAPI and ThFeedAPI
1 parent a3b2464 commit b524be3

File tree

7 files changed

+57
-99
lines changed

7 files changed

+57
-99
lines changed

examples/threathunter/create_feed.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import validators
77
import hashlib
88

9-
from cbapi.example_helpers import build_cli_parser, get_cb_threathunter_feed_object
9+
from cbapi.example_helpers import build_cli_parser, get_cb_threathunter_object
1010
from cbapi.psc.threathunter import Feed
1111

1212

@@ -35,7 +35,7 @@ def main():
3535
parser.add_argument("--rep_visibility", type=str, help="Report visibility")
3636

3737
args = parser.parse_args()
38-
cb = get_cb_threathunter_feed_object(args)
38+
cb = get_cb_threathunter_object(args)
3939

4040
feed_info = {
4141
"name": args.name,

examples/threathunter/feed_operations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import sys
55
from cbapi.psc.threathunter.models import Feed, Report
6-
from cbapi.example_helpers import build_cli_parser, get_cb_threathunter_feed_object
6+
from cbapi.example_helpers import build_cli_parser, get_cb_threathunter_object
77
import logging
88
import json
99

@@ -206,7 +206,7 @@ def main():
206206
specifier.add_argument("-f", "--feedname", type=str, help="Feed Name")
207207

208208
args = parser.parse_args()
209-
cb = get_cb_threathunter_feed_object(args)
209+
cb = get_cb_threathunter_object(args)
210210

211211
if args.command_name == "list":
212212
return list_feeds(cb, parser, args)

examples/threathunter/watchlist_operations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import sys
55
from cbapi.psc.threathunter.models import Watchlist, Report, Feed
6-
from cbapi.example_helpers import build_cli_parser, get_cb_threathunter_feed_object
6+
from cbapi.example_helpers import build_cli_parser, get_cb_threathunter_object
77
from cbapi.errors import ObjectNotFoundError
88
import logging
99
import json
@@ -329,7 +329,7 @@ def main():
329329
commands.add_parser("import", help="Import a previously exported watchlist")
330330

331331
args = parser.parse_args()
332-
cb = get_cb_threathunter_feed_object(args)
332+
cb = get_cb_threathunter_object(args)
333333

334334
if args.command_name == "list":
335335
return list_watchlists(cb, parser, args)

src/cbapi/example_helpers.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from cbapi.protection import CbEnterpriseProtectionAPI
1414
from cbapi.psc.defense import CbDefenseAPI
15-
from cbapi.psc.threathunter import CbThreatHunterAPI, CbThreatHunterFeedAPI
15+
from cbapi.psc.threathunter import CbThreatHunterAPI
1616
from cbapi.response import CbEnterpriseResponseAPI
1717

1818
log = logging.getLogger(__name__)
@@ -102,21 +102,6 @@ def get_cb_threathunter_object(args):
102102
return cb
103103

104104

105-
def get_cb_threathunter_feed_object(args):
106-
if args.verbose:
107-
import logging
108-
logging.basicConfig()
109-
logging.getLogger("cbapi").setLevel(logging.DEBUG)
110-
logging.getLogger("__main__").setLevel(logging.DEBUG)
111-
112-
if args.cburl and args.apitoken:
113-
cb = CbThreatHunterFeedAPI(url=args.cburl, token=args.apitoken, ssl_verify=(not args.no_ssl_verify))
114-
else:
115-
cb = CbThreatHunterFeedAPI(profile=args.profile)
116-
117-
return cb
118-
119-
120105
def get_object_by_name_or_id(cb, cls, name_field="name", id=None, name=None, force_init=True):
121106
clsname = cls.__name__
122107
try:

src/cbapi/psc/threathunter/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@
77
from .rest_api import CbThreatHunterAPI
88
from cbapi.psc.threathunter.models import Process, Event, Tree, Feed, Report, IOCs, IOC_V2, Watchlist
99
from cbapi.psc.threathunter.query import QueryBuilder
10-
from .feed_api import CbThreatHunterFeedAPI

src/cbapi/psc/threathunter/feed_api.py

Lines changed: 0 additions & 76 deletions
This file was deleted.

src/cbapi/psc/threathunter/rest_api.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from cbapi.psc.threathunter.query import Query
22
from cbapi.connection import BaseAPI
3+
from cbapi.psc.threathunter.models import ReportSeverity
34
import logging
45

56
log = logging.getLogger(__name__)
@@ -26,6 +27,55 @@ def _perform_query(self, cls, **kwargs):
2627
else:
2728
return Query(cls, self, **kwargs)
2829

30+
def create(self, cls, data=None):
31+
"""Creates a new model.
32+
33+
>>> feed = cb.create(Feed, feed_data)
34+
35+
:param cls: The model being created
36+
:param data: The data to pre-populate the model with
37+
:type data: dict(str, object)
38+
:return: an instance of `cls`
39+
"""
40+
return cls(self, initial_data=data)
41+
42+
def validate_query(self, query):
43+
"""Validates the given IOC query.
44+
45+
>>> cb.validate_query("process_name:chrome.exe") # True
46+
47+
:param str query: the query to validate
48+
:return: whether or not the query is valid
49+
:rtype: bool
50+
"""
51+
args = {"q": query}
52+
resp = self.get_object("/pscr/query/v1/validate", query_parameters=args)
53+
54+
return resp.get("valid", False)
55+
56+
def convert_query(self, query):
57+
"""Converts a legacy CB Response query to a ThreatHunter query.
58+
59+
:param str query: the query to convert
60+
:return: the converted query
61+
:rtype: str
62+
"""
63+
args = {"query": query}
64+
resp = self.get_object("/threathunter/feedmgr/v1/query/translate", query_parameters=args)
65+
66+
return resp.get("query")
67+
68+
@property
69+
def custom_severities(self):
70+
"""Returns a list of active :py:class:`ReportSeverity` instances
71+
72+
:rtype: list[:py:class:`ReportSeverity`]
73+
"""
74+
# TODO(ww): There's probably a better place to put this.
75+
resp = self.get_object("/threathunter/watchlistmgr/v1/severity")
76+
items = resp.get("results", [])
77+
return [self.create(ReportSeverity, item) for item in items]
78+
2979
def queries(self):
3080
"""Retrieves a list of queries, active or complete, known by
3181
the ThreatHunter server.

0 commit comments

Comments
 (0)