-
Notifications
You must be signed in to change notification settings - Fork 5
/
fapi.js
232 lines (210 loc) · 6.16 KB
/
fapi.js
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Faraday api connections
var alerts = {'internalErrorServer': '<div class="alert alert-danger"><strong>ERROR!</strong> An internal error occurred on the server, make sure you have logged in or are still active.</div>',
'messageAddVuln': '<div class="alert alert-success"><strong>Success!</strong> The vulnerability has been added to Faraday.</div>',
};
var faraday_api = null;
var workspace = null;
function post(data, path){ // Post con ajax
var xhttp = new XMLHttpRequest();
xhttp.open("POST", faraday_api + path, false);
xhttp.withCredentials = true;
//xhttp.timeout = 10000; // Timeout de 10 segundos
xhttp.setRequestHeader("User-Agent", "Faraddon");
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.send(JSON.stringify(data));
return xhttp.responseText;
}
function get(path){ // Get con ajax
var xhttp = new XMLHttpRequest();
xhttp.open("GET", faraday_api + path, false);
console.log(faraday_api + path);
xhttp.withCredentials = true;
//xhttp.timeout = 20000; // Timeout de 10 segundos
xhttp.setRequestHeader("User-Agent", "Faraddon");
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.send();
return xhttp.responseText;
}
function addHost(ip, hostname, os){ // Agrega un Host en el workspace
data = {"ip": ip,
"hostnames":[hostname],
"mac":"00:00:00:00:00:00",
"description":"",
"default_gateway":"None",
"os": os,
"owned":false,
"owner":""};
r = post(data, '/v2/ws/' + workspace + '/hosts/');
//console.log(r);
return r; // Retorna el id del host, necesario para agregar el servicio
}
function addService(name, port, host_id){ // Agrega un servicio al host
data = {"name":name,
"description":"",
"owned":false,
"owner":"",
"ports":[port],
"protocol":name,
"parent":host_id,
"status":"open",
"version":"1.1",
"metadata":{"update_time":0,
"update_user":"",
"update_action":0,
"creator":"",
"create_time":0,
"update_controller_action":"UI Web New",
"owner":""},
"type":"Service"};
r = post(data, '/v2/ws/' + workspace + '/services/');
//console.log(r);
return r; // Retorna el id del host + servicio, necesario para agregar la vuln
}
function addVuln(name, vuln_data, description, resolution, easeofresolution, request, response, severity, service_id, method, path, website, params, refs = []){ // Le clava una vuln al host
data = {"metadata":{"update_time":0,
"update_user":"",
"update_action":0,
"creator":"Web UI",
"create_time":0,
"update_controller_action":"UI Web New",
"owner":""},
"obj_id":"",
"owner":"",
"parent":service_id,
"parent_type":"Service",
"type":"VulnerabilityWeb",
"ws":workspace,
"confirmed":true,
"data":vuln_data,
"desc":description,
"easeofresolution":easeofresolution,
"impact":{"accountability":false,
"availability":false,
"confidentiality":false,
"integrity":false},
"name":name,
"owned":false,
"policyviolations":[],
"refs":refs,
"resolution":resolution,
"severity":severity,
"status":"opened",
"method":method,
"params":params,
"path":path,
"pname":"",
"query":"",
"request":request,
"response":response,
"website":website,
"_attachments":{},
"description":"",
"protocol":"",
"version":""};
r = post(data, '/v2/ws/' + workspace + '/vulns/');
return r;
//console.log(r);
}
function getAllWorkspaces(){ // Retorna un array con todos los workspaces ['test', 'example']
try{
workspaces = [];
r = JSON.parse(get('/v2/ws/'));
for(var i=0; i < r.length; i++){
workspaces.push(r[i].name);
}
return workspaces;
}
catch(err){
return false
}
}
function getVulnerabilitiesTemplates(){
try{
j = JSON.parse(get('/v2/vulnerability_template/'));
return j.rows;
}
catch(err){
return false;
}
}
function info(){ // Checkea el estado de faraday server
var ra = null;
try{
ra = JSON.parse(get('/v2/info'));
if(ra['Faraday Server'] === 'Running'){
console.log(ra);
return true;
}
}
catch(error){
console.log("Entro en el catch "+error);
console.log(ra);
return false;
}
}
function checkIsDataIsEmpty(server, workspace){
if(server != '' && workspace != ''){
return true;
}
else{
return false;
}
}
function checkIsWorkspaceIsValid(){
try{
JSON.parse(get('/v2/ws/' + workspace + '/'));
return true;
}
catch(err){
return false;
}
}
function createHost(ip, hostname, os){
try{
host = JSON.parse(addHost(ip, hostname, os));
}
catch(err){
return false;
}
try{
host_id = host.message.indexOf('Existing value for unique columns') == 0 ? host.object.id : host.object.id;
return host_id;
}
catch(err){
if(host.type == 'Host'){
return host.id;
}
}
}
function createService(name, port, host_id){
try{
service = JSON.parse(addService(name, port, host_id));
}
catch(err){
return false; //Si arroja error al parsear el JSON quiere decir que hubo un error interno del servidor
}
try{
service_id = service.message.indexOf('Existing value for unique columns') == 0 ? service.object.id : service.object.id;
return service_id;
}
catch(err){
if(service.status == 'open'){
return service.id;
}
}
}
function createVuln(name, vuln_data, description, resolution, easeofresolution, request, response, severity, service_id, method, path, website, params, refs = []){
try{
vuln = JSON.parse(addVuln(name, vuln_data, description, resolution, easeofresolution, request, response, severity, service_id, method, path, website, params, refs));
}
catch(err){
return alerts.internalErrorServer;
}
try{
message = vuln.message.indexOf('Existing value for unique columns') == 0 ? vuln.message : vuln.message;
return '<div class="alert alert-danger"><strong>ERROR! </strong>' + message + '</div>';
}
catch(err){
return alerts.messageAddVuln;
}
}