-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexamples.py
More file actions
198 lines (177 loc) · 6.28 KB
/
examples.py
File metadata and controls
198 lines (177 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from hive_library import HiveLibrary
from hive_library.rest import HiveRestApi
from hive_library.enum import RecordTypes
from uuid import UUID
from ipaddress import IPv4Address
from time import sleep
from typing import Optional, List
# Connect to Hive server
username: Optional[str] = None
password: Optional[str] = None
server: Optional[str] = None
proxy: Optional[str] = None
hive_api: HiveRestApi = HiveRestApi(username=username, password=password, server=server)
# Create project
new_project: Optional[HiveLibrary.Project] = hive_api.create_project(
HiveLibrary.Project(name="test_project", description="test project")
)
print(f"New project: \n{new_project}\n")
# Get list of projects
projects_list: Optional[List[HiveLibrary.Project]] = hive_api.get_projects_list()
print(f"Projects list: \n{projects_list}\n")
project: HiveLibrary.Project = projects_list[0]
# Create host
new_host: HiveLibrary.Host = HiveLibrary.Host()
new_host.ip = IPv4Address("192.168.0.1")
new_host.ports = [
HiveLibrary.Host.Port(
port=80,
service=HiveLibrary.Host.Port.Service(
cpelist="test service cpelist",
name="http",
product="Unit test",
version="0.1",
),
protocol="tcp",
state="open",
records=[
HiveLibrary.Record(
name="test string port record",
tool_name="test_tool_name",
record_type=RecordTypes.NESTED.value,
value=[
HiveLibrary.Record(
name="test nested port record 1",
tool_name="test_tool_name",
record_type=RecordTypes.STRING.value,
value="test nested port record 1 value",
),
HiveLibrary.Record(
name="test nested port record 2",
tool_name="test_tool_name",
record_type=RecordTypes.STRING.value,
value="test nested port record 2 value",
),
],
)
],
tags=[HiveLibrary.Tag(name="port_tag")],
)
]
new_host.names = [
HiveLibrary.Host.Name(
hostname="unit.test.com",
records=[
HiveLibrary.Record(
name="test list hostname record",
tool_name="test_tool_name",
record_type=RecordTypes.LIST.value,
value=[
"test list hostname record value 1",
"test list hostname record value 2",
],
)
],
tags=[HiveLibrary.Tag(name="hostname_tag")],
)
]
new_host.records = [
HiveLibrary.Record(
name="test nested host record",
tool_name="test_tool_name",
record_type=RecordTypes.STRING.value,
value="test nested host record value",
)
]
new_host.tags = [HiveLibrary.Tag(name="host_tag")]
task_id: Optional[UUID] = hive_api.create_host(project_id=project.id, host=new_host)
# Wait before import task is completed
for _ in range(10):
if hive_api.task_is_completed(project_id=project.id, task_id=task_id):
break
else:
sleep(1)
print(f"New host: \n{new_host}\n")
# Get list of hosts
hosts_list: Optional[List[HiveLibrary.Host]] = hive_api.get_hosts(project_id=project.id)
print(f"Hosts list: \n{hosts_list}\n")
host: HiveLibrary.Host = hosts_list[0]
# Create note
new_note: Optional[HiveLibrary.Note] = hive_api.create_note(
note_text="test note text", project_id=project.id, node_id=host.id
)
print(f"New note: \n{new_note}\n")
# Get list of notes
updated_hosts_list: Optional[List[HiveLibrary.Host]] = hive_api.get_hosts(
project_id=project.id
)
print(f"Notes list: \n{updated_hosts_list[0].notes}\n")
# Create file
new_file: Optional[HiveLibrary.File] = hive_api.upload_file(
file_name="test_file.txt",
file_content=b"test file content",
project_id=project.id,
node_id=host.id,
)
print(f"New file: \n{new_file}\n")
# Get list of files
updated_hosts_list: Optional[List[HiveLibrary.Host]] = hive_api.get_hosts(
project_id=project.id
)
print(f"Files list: \n{updated_hosts_list[0].files}\n")
# Search variables
search_ip: IPv4Address = IPv4Address("192.168.0.1")
search_port: int = 80
search_hostname: str = "unit.test.com"
search_tag: str = "host_tag"
search_service: str = "http"
# Search by IP
search_hosts: Optional[List[HiveLibrary.Host]] = hive_api.search_by_ip(
project_id=project.id, ip=search_ip
)
print(f"Search by IP ({search_ip}) hosts: \n{search_hosts}\n")
# Search by Port
search_hosts: Optional[List[HiveLibrary.Host]] = hive_api.search_by_port(
project_id=project.id, port=search_port
)
print(f"Search by Port ({search_port}) hosts: \n{search_hosts}\n")
# Search by IP and Port
search_hosts: Optional[List[HiveLibrary.Host]] = hive_api.search_by_ip_and_port(
project_id=project.id, ip=search_ip, port=search_port
)
print(f"Search by IP and Port ({search_ip}, {search_port}) hosts: \n{search_hosts}\n")
# Search by Hostname
search_hosts: Optional[List[HiveLibrary.Host]] = hive_api.search_by_hostname(
project_id=project.id, hostname=search_hostname
)
print(f"Search by Hostname ({search_hostname}) hosts: \n{search_hosts}\n")
# Search by Tag
search_hosts: Optional[List[HiveLibrary.Host]] = hive_api.search_by_tag(
project_id=project.id, tag=search_tag
)
print(f"Search by Tag ({search_tag}) hosts: \n{search_hosts}\n")
# Search by Service
search_hosts: Optional[List[HiveLibrary.Host]] = hive_api.search_by_service(
project_id=project.id, service=search_service
)
print(f"Search by Service ({search_service}) hosts: \n{search_hosts}\n")
# Import from nmap xml
task_id: Optional[UUID] = hive_api.import_from_file(
file_location="tests/nmap_test.xml", import_type="nmap", project_id=project.id
)
# Wait before import task is completed
for _ in range(10):
if hive_api.task_is_completed(project_id=project.id, task_id=task_id):
break
else:
sleep(1)
# Get imported hosts
imported_hosts: Optional[List[HiveLibrary.Host]] = hive_api.get_hosts(
project_id=project.id
)
print(f"Imported hosts: \n{imported_hosts}\n")
# Delete project
deleted_project: Optional[HiveLibrary.Project] = hive_api.delete_project_by_name(
project_name=new_project.name
)
print(f"Deleted project: \n{deleted_project}\n")