!257 添加进度条&&修改fastsacn输出格式

Merge pull request !257 from 宋帮诚晋/master
This commit is contained in:
宋帮诚晋 2023-04-04 05:59:10 +00:00 committed by Gitee
commit ef8b4fd9db
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 121 additions and 160 deletions

View File

@ -0,0 +1,39 @@
{
"RCServerUUID": "aea559b6-f17c-439d-875d-e4ab32458381",
"RCServerName": "123",
"RCFamily": "RCFamily",
"RCRelease": "RCRelease",
"RCContainer": "RCContainer",
"RCExploredType": "BaseLine",
"RCExploredTimeAt": "2023-04-04T13:52:27.290528233+08:00",
"RCExploredMode": "RCExploredMode",
"RCExploredVersion": "RCExploredVersion",
"RCExploredRevision": "RCExploredRevision",
"RCExploredBy": "RCExploredBy",
"RCExploredVia": "RCExploredVia",
"RCExploredIPv4Addrs": [
"0.0.0.0"
],
"RCExploredIPv6Addrs": [
"0.0.0.0"
],
"RCReportedAt": "2023-04-04T13:52:27.292744018+08:00",
"RCReportedVersion": "RCReportedVersion",
"RCReportedBy": "RCReportedBy",
"RCElapsedTime": 2215958,
"RCErrors": "RCErrors",
"RCWarnings": "RCWarnings",
"RCExploredVulns": null,
"RCReunningKernelInfo": {
"RKRelease": "0.0",
"RKVersion": "0.0",
"RKRebootRequired": false
},
"RCPackages": "RCPackages",
"RCSrcPackages": "RCSrcPackages",
"RCOptional": "RCOptional",
"RCExecPocNums": 10,
"RCRepairedNums": 6,
"RCNotFixedNums": 0,
"RCNotExecPocNums": 4
}

BIN
src/main

Binary file not shown.

View File

@ -1,165 +1,41 @@
package main
import (
"fmt"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
"io/ioutil"
"net"
"os"
"path"
"path/filepath"
"time"
"fmt"
"time"
)
type currentDirInfo struct {
currentDirs []string
currentFiles []string
}
func getCurrentDir(path string) (currentDirInfo, error) {
infos, err := ioutil.ReadDir(path)
if err != nil {
return currentDirInfo{}, nil
}
var currentDirs []string
var currentFiles []string
for _, info := range infos {
fmt.Printf("Name:%-30s Size:%-10d字节 Modtime:%s IsDir:%v\n", info.Name(), info.Size(), info.ModTime().Format("2006-01-02 03:04:05"), info.IsDir())
abs, err := filepath.Abs(info.Name())
if err != nil {
return currentDirInfo{}, nil
}
if info.IsDir() {
currentDirs = append(currentDirs, abs)
} else {
currentFiles = append(currentFiles, abs)
}
}
fmt.Printf("=======================当前文件夹==============================\n")
for _, v := range currentDirs {
fmt.Printf("当前文件夹:%s\n", v)
}
fmt.Printf("========================当前文件=============================\n")
for _, v := range currentFiles {
fmt.Printf("当前文件:%s\n", v)
}
return currentDirInfo{currentDirs: currentDirs, currentFiles: currentFiles}, nil
}
func hostKeyCallback(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
}
//获取一个STFP客户端连接
func connect(user, password, host string, port int) (*sftp.Client, error) {
auth := []ssh.AuthMethod{ssh.Password(password)}
config := &ssh.ClientConfig{
User: user,
Auth: auth,
Timeout: 30 * time.Second,
HostKeyCallback: hostKeyCallback,
}
// connect to ssh
addr := fmt.Sprintf("%s:%d", host, port)
client, err := ssh.Dial("tcp", addr, config)
if err != nil {
return nil, err
}
// create sftp client
sftpClient, err := sftp.NewClient(client)
if err != nil {
return nil, err
}
fmt.Printf("连接%s@%s:%d成功\n", user, host, port)
return sftpClient, nil
}
//上传文件
func uploadFile(sftpClient *sftp.Client, localFilename string, remotePath string, ch chan string) {
file, err := os.Open(localFilename)
if err != nil {
ch <- fmt.Sprintf("上传失败, path:%s, err:%s", filepath.Base(localFilename), err)
}
defer func(srcFile *os.File) {
err := srcFile.Close()
if err != nil {
}
}(file)
var remoteFileName = path.Join(remotePath, filepath.Base(localFilename))
err = sftpClient.MkdirAll(remotePath)
if err != nil {
ch <- fmt.Sprintf("上传失败, path:%s, err:%s", filepath.Base(localFilename), err)
return
}
dstFile, err := sftpClient.Create(remoteFileName)
if err != nil {
ch <- fmt.Sprintf("上传失败, path:%s, err:%s", filepath.Base(localFilename), err)
return
}
defer func(dstFile *sftp.File) {
err := dstFile.Close()
if err != nil {
}
}(dstFile)
ff, err := ioutil.ReadAll(file)
if err != nil {
ch <- fmt.Sprintf("上传失败, path:%s, err:%s", filepath.Base(localFilename), err)
return
}
_, err = dstFile.Write(ff)
if err != nil {
ch <- fmt.Sprintf("上传失败, path:%s, err:%s", filepath.Base(localFilename), err)
return
}
ch <- fmt.Sprintf("上传文件成功, 保存到:%s\n", remoteFileName)
}
func main() {
user := "root"
password := ""
host := "127.0.0.1"
port := 22
remoteFilePath := "/home/tmp"
// 进度条长度
barLength := 50
info, err := getCurrentDir(".")
if err != nil {
return
}
currentFiles := info.currentFiles
currentDirs := info.currentDirs
fmt.Println(currentDirs,currentFiles)
client, err := connect(user, password, host, port)
if err != nil {
fmt.Println(fmt.Sprintf("连接%s:%d失败, err:%s", host, port, err))
return
}
// 循环打印进度条
for i := 0; i <= barLength; i++ {
// 计算进度条百分比
percent := i * 100 / barLength
var chs = make(chan string, len(currentFiles))
for _, file := range currentFiles {
fmt.Println(file)
go uploadFile(client, file, remoteFilePath, chs)
}
count := 0
for c := range chs {
count++
fmt.Printf("上传情况:%s\n", c)
if count == len(currentFiles) {
return
}
}
// 计算已完成和未完成进度条的长度
completedLength := i
uncompletedLength := barLength - i
// 使用ANSI转义序列清空当前行
fmt.Print("\r\x1b[K")
// 输出进度条
fmt.Printf("[%s%s] %d%%", getBar(completedLength), getBar(uncompletedLength), percent)
// 等待一段时间
time.Sleep(time.Millisecond * 100)
}
fmt.Println()
}
// getBar 返回指定长度的进度条字符串
func getBar(length int) string {
bar := ""
for i := 0; i < length; i++ {
bar += "="
}
return bar
}

View File

@ -11,13 +11,14 @@ import (
"os/exec"
"strconv"
"regexp"
"time"
// "reflect"
)
var (
dbhostsip = "172.17.20.121:3306"
dbusername = "root"
dbpassword = ""
dbpassword = "Kylin123-"
dbname = "kylincve"
)
@ -34,6 +35,11 @@ var (
version0 = 0
version1 = 1
version2 = 2
VbNums = 0
VbLevel_hight = 0
VbLevel_mid = 0
VbLevel_low = 0
VbLevel_ignore =0
)
func StringStrip(str string) int {
if str == "" {
@ -155,7 +161,9 @@ func fastScan(dpkgInfo []string){
stmt, err := db.Prepare(sqlStr)
checkerr(err)
defer stmt.Close()
for i:=0;i<len(dpkgInfo);i++{
for i:=0;i<=len(dpkgInfo)-1;i++{
barLength := len(dpkgInfo)-1
ProgressBar(barLength,i)
if i & 1 == 0 {
rows, err := stmt.Query(dpkgInfo[i])
checkerr(err)
@ -166,13 +174,33 @@ func fastScan(dpkgInfo []string){
dpkgInfoStr:=dpkgInfo[i+1]
result:=PushCompareVersion(s.cve_no,dpkgInfoStr,s.ubuntu_v10_1_edition)
if result==2{
FastScanResult:="ID:"+s.cve_no+" level:"+s.package_name+" Role:"+s.role_level+" Security_Version:"+s.ubuntu_v10_1_edition+" Current_Version:"+dpkgInfoStr
fmt.Printf("%c[%d;%d;%dm%s%c[0m\n", 0x1B, 0, 0, 32, FastScanResult, 0x1B)
// FastScanResult:="ID:"+s.cve_no+" level:"+s.package_name+" Role:"+s.role_level+" Security_Version:"+s.ubuntu_v10_1_edition+" Current_Version:"+dpkgInfoStr
// fmt.Printf("%c[%d;%d;%dm%s%c[0m\n", 0x1B, 0, 0, 32, FastScanResult, 0x1B)
VbNums = VbNums+1
if s.package_name == "高危"{
VbLevel_hight = VbLevel_hight + 1
}
if s.package_name == "中危"{
VbLevel_mid = VbLevel_mid + 1
}
if s.package_name == "低危"{
VbLevel_low = VbLevel_low + 1
}
if s.package_name == "已忽略"{
VbLevel_ignore = VbLevel_ignore + 1
}
}
}
rows.Close()
}
}
fmt.Println()
fmt.Printf("%c[%d;%d;%dm%s%c[0m\n", 0x1B, 0, 0, 33,"=====================Scan Results======================\n" , 0x1B)
fmt.Println("共计漏洞:",VbNums)
fmt.Println("高危漏洞:",VbLevel_hight)
fmt.Println("中危漏洞:",VbLevel_mid)
fmt.Println("低危漏洞:",VbLevel_low)
fmt.Println("忽略漏洞:",VbLevel_ignore)
}
@ -215,4 +243,22 @@ func GetdpkgInfo(){
dpkgInfo = append(dpkgInfo[5:])
fastScan(dpkgInfo)
}
}
func ProgressBar(barLength int ,i int){
// 计算进度条百分比
percent := i * 100 / barLength
fmt.Print("\r\x1b[K")
// 输出进度条
fmt.Printf("[%s%s] %d%%", getBar(20), getBar(20), percent)
// 等待一段时间
time.Sleep(time.Millisecond * 100)
}
// getBar 返回指定长度的进度条字符串
func getBar(length int) string {
bar := ""
for i := 0; i < length; i++ {
bar += "="
}
return bar
}