我試圖從 CVEdetails 源代碼中提取、編碼一個 bash 腳本、兩個不同的值“供應商”和“產品”,并將每個值存盤在一個 bash 變數中。這是vendor=$(requested code)和product=$(requested code)。
包含我需要的資訊的代碼片段是:
<tr>
<th>
Vendor
</th>
<th>
Product
</th>
<th>
Vulnerable Versions
</th>
</tr>
<tr>
<td>
<a href="/vendor/45/Apache.html" title="Details for Apache">Apache</a> </td>
<td><a href="/product/66/Apache-Http-Server.html?vendor_id=45" title="Product Details Apache Http Server">Http Server</a></td>
<td class="num">
34 </td>
</tr>
</table>
有了這個,我需要的資訊是 Vendor=Apache 和 Product=HTTP Server,但我自己能夠做的最接近的代碼是:
wget https://www.cvedetails.com/cve/CVE-2017-3169 &>/dev/null; grep -C 6 "Vulnerable Versions" CVE-2017-3169
關于如何獲取此類資訊的任何想法?提前致謝!
uj5u.com熱心網友回復:
請參閱使用 API 和適當的決議器時它是多么簡單的示例:
#!/usr/bin/env bash
API_URL='https://cve.circl.lu/api'
cve_id='CVE-2017-3169'
# Read parsed JSON data
IFS=: read -r _ _ _ vendor product _ < <(
# Perform API request
curl -s "$API_URL/cve/$cve_id" |
# Parse JSON data returned by the API to get only what we need
jq -r '.vulnerable_product[0]'
)
# Demo what we got
printf 'CVE ID: %s\n' "$cve_id"
printf 'Vendor: %s\n' "${vendor^}"
printf 'Product: %s\n' "${product}"
示例輸出:
CVE ID: CVE-2017-3169
Vendor: Apache
Product: http_server
uj5u.com熱心網友回復:
要處理像 HTML 和 JSON 這樣的結構化資料,您應該使用適當的決議器。sed, grep,awk和喜歡的不是。對于命令列工具,我強烈推薦西德爾,它既是 HTML 決議器,又是 JSON 決議器!
HTML 源代碼
$ xidel -s https://www.cvedetails.com/cve/CVE-2017-3169 -e '
//table[@id="vulnversconuttable"]//td[position() lt 3]/a
'
Apache
Http Server
([position() = (1,2)]也可以讓它回傳第一個和第二個<td>節點)
$ xidel -s https://www.cvedetails.com/cve/CVE-2017-3169 -e '
//table[@id="vulnversconuttable"]/(vendor:=.//td[1]/a,product:=.//td[2]/a)
'
vendor := Apache
product := Http Server
$ xidel -s https://www.cvedetails.com/cve/CVE-2017-3169 -e '
//table[@id="vulnversconuttable"]/(vendor:=.//td[1]/a,product:=.//td[2]/a)
' --output-format=bash
vendor='Apache'
product='Http Server'
$ eval "$(
xidel -s https://www.cvedetails.com/cve/CVE-2017-3169 -e '
//table[@id="vulnversconuttable"]/(vendor:=.//td[1]/a,product:=.//td[2]/a)
' --output-format=bash
)"
$ printf '%s\n' "$vendor" "$product"
Apache
Http Server
JSON API
$ xidel -s "https://cve.circl.lu/api/cve/CVE-2017-3169" -e '
$json/(vulnerable_product)(1)
'
cpe:2.3:a:apache:http_server:2.2.2:*:*:*:*:*:*:*
$ xidel -s "https://cve.circl.lu/api/cve/CVE-2017-3169" -e '
tokenize($json/(vulnerable_product)(1),":")
'
cpe
2.3
a
apache
http_server
2.2.2
*
*
*
*
*
*
*
$ xidel -s "https://cve.circl.lu/api/cve/CVE-2017-3169" -e '
tokenize($json/(vulnerable_product)(1),":")[position() = (4,5)]
'
apache
http_server
$ xidel -s "https://cve.circl.lu/api/cve/CVE-2017-3169" -e '
let $a:=tokenize($json/(vulnerable_product)(1),":") return (
vendor:=$a[4],product:=$a[5]
)
'
vendor := apache
product := http_server
$ eval "$(
xidel -s "https://cve.circl.lu/api/cve/CVE-2017-3169" -e '
let $a:=tokenize($json/(vulnerable_product)(1),":") return (
vendor:=$a[4],product:=$a[5]
)
' --output-format=bash
)"
$ printf '%s\n' "$vendor" "$product"
apache
http_server
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/334794.html
