Skip to content

Commit

Permalink
fix masscan 2022-09-25
Browse files Browse the repository at this point in the history
  • Loading branch information
hktalent committed Sep 25, 2022
1 parent daaaf3b commit 95d6cab
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 34 deletions.
9 changes: 5 additions & 4 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,17 @@ func (e *Engine) DoEvent(ed *EventData) {
fnCall := e.DoCase(ed)
if nil != fnCall {
in := inject.New()
a := ed.EventData
for _, i := range a {
for _, i := range ed.EventData {
in.Map(i)
}
v, err := in.Invoke(fnCall)
if nil != err {
log.Printf("DoEvent is error: %v %+v \n", err, ed.EventData)
log.Printf("DoEvent %s is error: %v %+v \n", ed.EventType, err, ed.EventData)
} else if nil != v {
log.Printf("DoEvent result %v\n", v)
log.Printf("DoEvent result %s %v\n", ed.EventType, v)
}
} else {
log.Printf("can case func %v\n", ed)
}
}
}
Expand Down
23 changes: 18 additions & 5 deletions lib/util/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package util
import (
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/logger"
"log"
"os"
Expand Down Expand Up @@ -52,7 +53,11 @@ func InitDb(dst ...interface{}) *gorm.DB {
}
log.Println("DbName ", szDf)
xx01 := sqlite.Open("file:" + szDf + ".db?cache=shared&mode=rwc&_journal_mode=WAL&Synchronous=Off&temp_store=memory&mmap_size=30000000000")
db, err := gorm.Open(xx01, &gorm.Config{PrepareStmt: true, Logger: logger.Default.LogMode(logger.Silent)})
db, err := gorm.Open(xx01, &gorm.Config{
PrepareStmt: true,
//Logger: logger.Default.LogMode(logger.Silent),
Logger: logger.Default.LogMode(logger.Info),
})
if err == nil { // no error
db1, _ := db.DB()
if err := db1.Ping(); nil == err {
Expand Down Expand Up @@ -84,7 +89,7 @@ func GetTableName[T any](mod T) string {
// 指定id更新T类型mod数据
func Update[T any](mod *T, query string, args ...interface{}) int64 {
var t1 *T = mod
xxxD := dbCC.Table(GetTableName(mod)).Model(t1)
xxxD := dbCC.Table(GetTableName(mod)).Model(&t1)
xxxD.AutoMigrate(t1)
rst := xxxD.Where(query, args...).Updates(mod)
xxxD.Commit()
Expand All @@ -96,10 +101,18 @@ func Update[T any](mod *T, query string, args ...interface{}) int64 {

// 更新失败再插入新数据,确保只有一条数据
func UpInsert[T any](mod *T, query string, args ...interface{}) int64 {
if 1 >= Update[T](mod, query, args...) {
return Create[T](mod)
// 在冲突时,更新除主键以外的所有列
if 1 > Update[T](mod, query, args...) { // &&
if 1 > Create[T](mod) {
xx1 := dbCC.Clauses(clause.OnConflict(clause.OnConflict{
Columns: []clause.Column{{Name: "addr"}}, // key colume
UpdateAll: true})).Create(mod)
return xx1.RowsAffected
} else {
return 1
}
}
return 0
return 1
}

// 通用,insert
Expand Down
56 changes: 33 additions & 23 deletions projectdiscovery/nuclei_Yaml/masscan/masscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,22 @@ import (
"encoding/xml"
_ "github.com/codegangsta/inject"
"github.com/hktalent/scan4all/lib/util"
"gorm.io/gorm"
"io"
"log"
"regexp"
)

// 地址
type Address struct {
Addr string `xml:"addr,attr" json:"addr"`
AddrType string `xml:"addrtype,attr" json:"addrType"`
Addr string `xml:"addr,attr" json:"addr" gorm:"primaryKey;"`
AddrType string `xml:"addrtype,attr" json:"addr_type"`
}

// 状态
type State struct {
State string `xml:"state,attr" json:"state"`
Reason string `xml:"reason,attr" json:"reason"`
ReasonTTL string `xml:"reason_ttl,attr" json:"reasonTTL"`
ReasonTTL string `xml:"reason_ttl,attr" json:"reason_ttl"`
}

// nmap 模式
Expand All @@ -37,25 +36,29 @@ type Nmaprun struct {
// foreignKey should name the model-local key field that joins to the foreign entity.
// references should name the foreign entity's primary or unique key.
type Host struct {
gorm.Model
//StartTime string
//Endtime string `xml:"endtime,attr"`
Ip string `json:"-"`
Port string `json:"-"`
Address Address `json:",inline" xml:"address" gorm:"foreignKey:Ip;references:Addr"`
Ports Ports `json:"ports" xml:"ports>port" gorm:"foreignKey:Port;references:Portid"`
//LastScanTime int `json:"lastScanTime"`
//LastScanEndTime int `json:"lastScanEndTime"`
Address Address `json:"address" xml:"address" gorm:"embedded;"`
// association_autoupdate:true;association_autocreate:true;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;
Ports []Ports `json:"ports" xml:"ports>port" gorm:"foreignKey:addr;References:addr;"` // association_autocreate:true; // many2many:Host_Ports;foreignKey:ID;References:ID;
}

// `xml:",innerxml"`
//func (cm Host) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
// if cm.Address != nil {
// err := e.EncodeToken(cm.comment)
// if err != nil {
// return err
// }
// }
// return e.Encode(cm.Member)
//}

// 端口信息
type Ports []struct {
Protocol string `xml:"protocol,attr" json:"protocol"`
Portid string `xml:"portid,attr" json:"portid"`
State1 string `json:"-"`
State State `json:",inline" xml:"state" gorm:"foreignKey:State1;references:State"`
Name string `json:"-"`
Service Service `json:",inline" xml:"service" gorm:"foreignKey:Name;references:Name"`
type Ports struct {
Addr string `json:"addr" gorm:"unique_index:S_R"`
Protocol string `xml:"protocol,attr" json:"protocol" gorm:"unique_index:S_R"`
PortId string `xml:"portid,attr" json:"port_id" gorm:"unique_index:S_R"`
State State `json:"state" xml:"state" gorm:"embedded;"`
Service Service `json:"service" xml:"service" gorm:"embedded;"`
}

// 服务信息
Expand Down Expand Up @@ -124,9 +127,15 @@ func (m *Masscan) Run() error {
return
}
for _, i := range x1 {
nR := util.UpInsert[Host](&i, "address=? and ports=?", i.Address, i.Ports)
if 1 >= nR {
log.Println("util.UpInsert fail")
//log.Printf("%+v\n", i)
if 0 < len(i.Ports) {
for _, x9 := range i.Ports {
x9.Addr = i.Address.Addr
}
nR := util.UpInsert[Host](&i, "addr=?", i.Address.Addr)
if 1 > nR {
log.Println("util.UpInsert fail \n", line)
}
}
}
}, m.SystemPath, m.Args...)
Expand All @@ -153,6 +162,7 @@ func (m *Masscan) ParseLine(s string) ([]Host, error) {
if err == io.EOF || err != nil {
break
}
//host.Ip = host.Address.Addr
hosts = append(hosts, host)
}
default:
Expand Down
4 changes: 2 additions & 2 deletions projectdiscovery/nuclei_Yaml/masscan/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func main() {
m := &masscan.Host{}
util.InitModle(m)
//util.InitModle(masscan.Ports{}, masscan.Address{}, masscan.Service{}, masscan.State{}, masscan.Host{})
util.InitModle(&masscan.Ports{}, &masscan.Host{})
masscan.ScanTarget("192.168.0.111")
}

0 comments on commit 95d6cab

Please sign in to comment.