1. 问题
项目部署后,经常收到 '**检测到目标网站存在无效链接漏洞**'漏洞,虽然是低危漏洞,但是让我无比头疼,由于项目比较老, 找起来非常麻烦,有没有更方便的方式那? 最后写了个工具,分享给大家,方案很简单,希望能解决大家问题 ~ ~ ~
2. 解决方案
通过node 配合 axios + cheerio
1. 首先使用访问项目地址,获取html内容,
2. 然后使用cheerio抓取网页数据
3. 找到a、link、script 等标签,读取src 或者 href属性拿到引入地址
4. 然后拼接当前项目 url。
5. 最后进行访问,就能知道那个是无效的链接了!!!
具体代码如下:
前置工作:
mkdir myLink cd myLink npm init -y npm install axios cheerio
复制
新建 myLink.js:
const axios = require('axios'); const cheerio = require('cheerio'); // 你的网站地址 const baseURL = 'xxx'; // 获取 HTML 内容函数 async function acquireHTML(url) { try { const { data } = await axios.get(url, { // 请求头 headers: { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', }, httpsAgent: new (require('https').Agent)({ rejectUnauthorized: false, }), }); return data; } catch (error) { console.error(`错误 ${url}: ${error.message}`); return null; } } // 解析标签 function analyzeLinks(html) { const $ = cheerio.load(html); const links = []; $('a').each((index, element) => { const link = $(element).attr('href'); if (link && link.startsWith('http')) { links.push(link); } else if (link && link.startsWith('/')) { links.push(baseURL + link); } }); $('link').each((index, element) => { const link = $(element).attr('href'); if (link && link.startsWith('http')) { links.push(link); } else if (link && link.startsWith('/')) { links.push(baseURL + link); } $('script').each((index, element) => { const link = $(element).attr('src'); if (link && link.startsWith('http')) { links.push(link); } else if (link && link.startsWith('/')) { links.push(baseURL + link); } }); // ... 这里可以加你自己想要检测的标签 }); console.log(links); // 打印解析的地址 return links; } // 检测是否无效 async function verifyLink(url) { try { const response = await axios.head(url, { headers: { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', }, httpsAgent: new (require('https').Agent)({ rejectUnauthorized: false, }), }); if (response.status >= 400) { console.log(`无效链接: ${url} (Status: ${response.status})`); } } catch (error) { console.log(`无效链接: ${url} (Error: ${error.message})`); } } // 入口 async function main() { const html = await acquireHTML(baseURL); if (!html) return; const links = analyzeLinks(html); console.log(`找到 ${links.length}链接`); for (const link of links) { await verifyLink(link); } console.log('完毕.'); } // 入口 main();
复制
最后执行命令
node myLink.js
复制
输入结果
最后, 不放心的挨个访问下就可以了~~~
3. 总结
谢谢大家,希望对大家有帮助