Hi guys I am trying to make an audit web page and I want to test a scan using Openvas which I have hosted in a docker container, when I start the scan I get error Dashboard.js:13
**POST http://localhost:5000/api/scan/start 400 (Bad Request) My native language is Spanish, I apologize if there is a discrepancy in the translation.
**
in the frontend where I request scanning is Dashboard.js
import React, { useState } from 'react';
import './Dashboard.css';
function Dashboard() {
const [target, setTarget] = useState('');
const [configId, setConfigId] = useState('');
const [scanId, setScanId] = useState('');
const [status, setStatus] = useState('');
const [report, setReport] = useState('');
const startScan = async () => {
try {
const response = await fetch('http://localhost:5000/api/scan/start', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ target, configId }),
});
const data = await response.json();
if (data.success) {
setScanId(data.scanId);
} else {
alert(data.message);
}
} catch (error) {
console.error('Error:', error);
}
};
const checkStatus = async () => {
try {
const response = await fetch(`http://localhost:5000/api/scan/status/${scanId}`);
const data = await response.json();
setStatus(data.status);
} catch (error) {
console.error('Error:', error);
}
};
const getReport = async () => {
try {
const response = await fetch(`http://localhost:5000/api/scan/report/${scanId}`);
const data = await response.json();
setReport(data.report);
} catch (error) {
console.error('Error:', error);
}
};
return (
<div className="dashboard-container">
<h2>Dashboard</h2>
<div className="form-group">
<label>Target</label>
<input
type="text"
value={target}
onChange={(e) => setTarget(e.target.value)}
/>
</div>
<div className="form-group">
<label>Config ID</label>
<input
type="text"
value={configId}
onChange={(e) => setConfigId(e.target.value)}
/>
</div>
<button onClick={startScan}>Start Scan</button>
<div>
<button onClick={checkStatus}>Check Status</button>
<p>Status: <strong>{status}</strong></p>
</div>
<div>
<button onClick={getReport}>Get Report</button>
<p>Report: <strong>{report}</strong></p>
</div>
</div>
);
}
export default Dashboard;
Backend
This is the docker-compose.yml file
version: '3'
services:
openvas:
image: mikesplain/openvas
ports:
- "443:443"
- "9390:9390"
- "9391:9391"
environment:
- PASSWORD=admin
volumes:
- openvas_data:/var/lib/openvas
- openvas_logs:/var/log/openvas
- openvas_etc:/etc/openvas
volumes:
openvas_data:
openvas_logs:
openvas_etc:
The index.js is configured as follows:
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const bodyParser = require('body-parser');
const userRoutes = require('./routes/userRoutes');
const scanRoutes = require('./routes/scanRoutes');
const app = express();
const PORT = process.env.PORT || 5000;
mongoose.connect(process.env.MONGODB_URI).then(() => {
console.log('MongoDB connected');
}).catch((error) => {
console.error('MongoDB connection error:', error);
});
app.use(cors());
app.use(bodyParser.json());
app.use('/api/users', userRoutes);
app.use('/api/scan', scanRoutes);
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
I have this scanRoutes.js file
const express = require('express');
const router = express.Router();
const { startScan, getScanStatus, getScanReport } = require('../controllers/scanController');
router.post('/start', startScan);
router.get('/status/:scanId', getScanStatus);
router.get('/report/:scanId', getScanReport);
module.exports = router;
I have this scanController.js file
const axios = require('axios');
// Configuración de Axios para OpenVAS
const openvasInstance = axios.create({
baseURL: 'http://127.0.0.1:9390',
timeout: 5000,
headers: { 'Content-Type': 'application/json' },
});
exports.startScan = async (req, res) => {
const { target } = req.body;
if (!target) {
return res.status(400).json({ success: false, message: 'Target is required' });
}
try {
const response = await openvasInstance.post('/scan/start', { target });
res.status(201).json({ success: true, scanId: response.data.scanId });
} catch (error) {
console.error('Error al iniciar escaneo:', error.message);
res.status(500).json({ success: false, message: 'Failed to start scan' });
}
};
exports.getScanStatus = async (req, res) => {
const { scanId } = req.params;
try {
const response = await openvasInstance.get(`/scan/status/${scanId}`);
res.status(200).json({ success: true, status: response.data.status });
} catch (error) {
console.error('Error al obtener estado del escaneo:', error.message);
res.status(500).json({ success: false, message: 'Failed to get scan status' });
}
};
exports.getScanReport = async (req, res) => {
const { scanId } = req.params;
try {
const response = await openvasInstance.get(`/scan/report/${scanId}`);
res.status(200).json({ success: true, report: response.data.report });
} catch (error) {
console.error('Error al obtener reporte del escaneo:', error.message);
res.status(500).json({ success: false, message: 'Failed to get scan report' });
}
};
I tried to try with another Openvas image but it didn’t work, I really can’t find logical errors in the code, that’s why I come here since I’m new to using web languages. I thank you in advance if you can tell me why it does not scan.
Note: My native language is Spanish, I apologize if there is a discrepancy in the translation.
ESTIVEN DE MOYA SUAREZ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.