Open himynamesdave opened 1 week ago
# example_scripts/add_objects.py from taxii2client.v21 import Server import json import requests from requests.auth import HTTPBasicAuth # Create a Server instance with correct URL and credentials server = Server('http://127.0.0.1:8000/api/taxii2/', user='read_write_user', password='testing123') # Dictionary to hold collections collections_dict = {} # Iterate over API roots and collections for api_root in server.api_roots: try: for collection in api_root.collections: collections_dict[collection.id] = collection except Exception as e: print(f"Error processing API root {api_root.url}: {e}") continue # Get the specific collection by ID collection_id = 'blog' collection = collections_dict.get(collection_id) if collection: try: # JSON object to be added to the collection objects_to_add = { "objects": [ { "type": "attack-pattern", "spec_version": "2.1", "id": "attack-pattern--6b948b5a-3c09-5365-b48a-da95c3964cb5", "created_by_ref": "identity--d2916708-57b9-5636-8689-62f049e9f727", "created": "2020-01-01T11:21:07.478851Z", "modified": "2020-01-01T11:21:07.478851Z", "name": "Spear Phishing", "description": "Used for tutorial content", "object_marking_refs": ["marking-definition--34098fce-860f-48ae-8e50-ebd3cc5e41da"] }, { "type": "attack-pattern", "spec_version": "2.1", "id": "attack-pattern--6b948b5a-3c09-5365-b48a-da95c3964cb5", "created_by_ref": "identity--d2916708-57b9-5636-8689-62f049e9f727", "created": "2020-01-02T11:21:07.478851Z", "modified": "2020-01-02T11:21:07.478851Z", "name": "Spear Phishing Updated ONCE", "description": "Used for tutorial content", "object_marking_refs": ["marking-definition--34098fce-860f-48ae-8e50-ebd3cc5e41da"] }, { "type": "attack-pattern", "spec_version": "2.1", "id": "attack-pattern--6b948b5a-3c09-5365-b48a-da95c3964cb5", "created_by_ref": "identity--d2916708-57b9-5636-8689-62f049e9f727", "created": "2020-01-03T11:21:07.478851Z", "modified": "2020-01-03T11:21:07.478851Z", "name": "Spear Phishing Updated TWICE", "description": "Used for tutorial content", "object_marking_refs": ["marking-definition--34098fce-860f-48ae-8e50-ebd3cc5e41da"] } ] } # Prepare the headers headers = { 'Content-Type': 'application/taxii+json;version=2.1', 'Accept': 'application/taxii+json;version=2.1' } # Print the request URL and headers request_url = collection.url + 'objects/' print(f"Request URL: {request_url}") print("Request Headers:", json.dumps(headers, indent=4)) # Make the request using requests library response = requests.post( request_url, headers=headers, auth=HTTPBasicAuth('read_write_user', 'testing123'), json=objects_to_add ) # Print the response in JSON format print(json.dumps(response.json(), indent=4)) except Exception as e: print(f"Error adding objects to the collection: {e}") else: print(f"Collection with ID {collection_id} not found.")
(cti-taxii-client_env) dgreenwood@Davids-MBP-2 cti-taxii-client % python3 example_scripts/add_objects.py Request URL: http://127.0.0.1:8000/api/taxii2/demo_database/collections/blog/objects/ Request Headers: { "Content-Type": "application/taxii+json;version=2.1", "Accept": "application/taxii+json;version=2.1" } { "title": "Unsupported media type \"{media_type}\" in request.", "http_status": 415, "details": { "content": { "message": "Unsupported media type \"application/taxii+json;version=2.1\" in request.", "code": "unsupported_media_type" } } }