Skip to content

Commit 73ed9b6

Browse files
committed
[+新增扫描进度跟踪功能,现在会定期输出扫描进度
1 parent 4dd5fbc commit 73ed9b6

File tree

7 files changed

+181
-7
lines changed

7 files changed

+181
-7
lines changed

kscan_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ func Context() {
9898
if err := recover(); err != nil {
9999
}
100100
}()
101-
102101
time.Sleep(3 * time.Second)
103102
resChan <- ""
104103
}()

lib/gonmap

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Subproject commit 99d64e58dbc83919ac0648fcf4ed2f7af79416bc
1+
Subproject commit b41d2cda02b711ae0a7377185fe925871809cf2e

lib/misc/misc.go

+16
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,19 @@ func CloneIntMap(intMap map[int]string) map[int]string {
291291
}
292292
return newIntMap
293293
}
294+
295+
func RandomString(i ...int) string {
296+
var length int
297+
var str string
298+
if len(i) != 1 {
299+
length = 32
300+
} else {
301+
length = i[0]
302+
}
303+
Char := "01234567890abcdef"
304+
for range Xrange(length) {
305+
j := rand.Intn(len(Char) - 1)
306+
str += Char[j : j+1]
307+
}
308+
return str
309+
}

lib/pool/pool.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package pool
33
import (
44
"errors"
55
"fmt"
6+
"kscan/lib/misc"
67
"kscan/lib/slog"
8+
"kscan/lib/smap"
79
"sync"
810
)
911

@@ -43,6 +45,8 @@ type Pool struct {
4345
Out chan interface{}
4446
//size用来表明池的大小,不能超发。
4547
threads int
48+
//正在执行的任务清单
49+
JobsList *smap.SMap
4650
//jobs表示执行任务的通道用于作为队列,我们将任务从切片当中取出来,然后存放到通道当中,再从通道当中取出任务并执行。
4751
Jobs chan *Worker
4852
//用于阻塞
@@ -55,6 +59,7 @@ type Pool struct {
5559
func NewPool(threads int) *Pool {
5660
return &Pool{
5761
threads: threads,
62+
JobsList: smap.New(),
5863
wg: &sync.WaitGroup{},
5964
Out: make(chan interface{}),
6065
In: make(chan interface{}),
@@ -66,13 +71,23 @@ func NewPool(threads int) *Pool {
6671
//从jobs当中取出任务并执行。
6772
func (p *Pool) work() {
6873
//减少waitGroup计数器的值
69-
defer func() { p.wg.Done() }()
74+
defer func() {
75+
p.wg.Done()
76+
}()
7077
for param := range p.In {
7178
if p.Done {
7279
return
7380
}
81+
//获取任务唯一票据
82+
Tick := p.NewTick()
83+
//压入工作任务到工作清单
84+
p.JobsList.Set(Tick, param)
85+
//开始工作
7486
f := NewWorker(p.Function)
87+
//开始工作,输出工作结果
7588
out, err := f.Run(param)
89+
//工作结束,删除工作清单
90+
p.JobsList.Delete(Tick)
7691
if err == nil && out != nil {
7792
p.Out <- out
7893
}
@@ -108,3 +123,8 @@ func (p *Pool) OutDone() {
108123
func (p *Pool) Stop() {
109124
p.Done = true
110125
}
126+
127+
//生成工作票据
128+
func (p *Pool) NewTick() string {
129+
return misc.RandomString()
130+
}

lib/smap/smap.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package smap
2+
3+
import (
4+
"sync"
5+
)
6+
7+
type SMap struct {
8+
value sync.Map
9+
}
10+
11+
func New() *SMap {
12+
return &SMap{sync.Map{}}
13+
}
14+
15+
func (s *SMap) Set(key interface{}, value interface{}) {
16+
s.value.Store(key, value)
17+
}
18+
19+
func (s *SMap) Get(key interface{}) (value interface{}, ok bool) {
20+
return s.value.Load(key)
21+
}
22+
23+
func (s *SMap) Delete(key interface{}) {
24+
s.value.Delete(key)
25+
}
26+
27+
func (s *SMap) Length() int {
28+
var i int
29+
f := func(key, value interface{}) bool {
30+
i = i + 1
31+
return true
32+
}
33+
s.value.Range(f)
34+
return i
35+
}
36+
37+
func (s *SMap) Exist(key interface{}) bool {
38+
if _, ok := s.value.Load(key); ok {
39+
return true
40+
}
41+
return false
42+
}
43+
44+
func (s *SMap) Peek() interface{} {
45+
var i interface{}
46+
f := func(key, value interface{}) bool {
47+
i = value
48+
return false
49+
}
50+
s.value.Range(f)
51+
return i
52+
}
53+
54+
func (s *SMap) Range(f func(key interface{}, value interface{}) bool) {
55+
s.value.Range(f)
56+
}
57+
58+
//func handler(key, value interface{}) bool {
59+
// fmt.Printf("Name :%s %s\n", key, value)
60+
// return true
61+
//}
62+
63+
//遍历,传入一个函数,遍历的时候函数返回false则停止遍历
64+
//s.value.Range(handler)

lib/smap/smap_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package smap
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestSMap_Length(t *testing.T) {
9+
s := New()
10+
s.Set("aaaa", "bbbb")
11+
s.Set("aaaa", "bbbb")
12+
s.Set("aaaa", "bbbb")
13+
s.Set("aaaa", "bbbb")
14+
s.Set("aaaa", "bbbb")
15+
s.Set("BBBB", "bbbb")
16+
s.Delete("BBBB")
17+
s.Delete("cccc")
18+
fmt.Println(s.Peek())
19+
}

run/type-kscan.go

+60-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"path"
1717
"strings"
1818
"sync"
19+
"time"
1920
)
2021

2122
type kscan struct {
@@ -29,9 +30,10 @@ type kscan struct {
2930
appBanner *pool.Pool
3031
}
3132
watchDog struct {
32-
output chan interface{}
33-
hydra chan interface{}
34-
wg *sync.WaitGroup
33+
output chan interface{}
34+
hydra chan interface{}
35+
wg *sync.WaitGroup
36+
trigger bool
3537
}
3638
hydra struct {
3739
pool *pool.Pool
@@ -61,6 +63,7 @@ func New(config app.Config) *kscan {
6163
k.watchDog.hydra = make(chan interface{})
6264
k.watchDog.output = make(chan interface{})
6365
k.watchDog.wg = &sync.WaitGroup{}
66+
k.watchDog.trigger = false
6467

6568
k.hydra.pool = pool.NewPool(10)
6669
k.hydra.queue = queue.New()
@@ -258,6 +261,9 @@ func (k *kscan) Output() {
258261
}
259262
var disp string
260263
var write string
264+
//打开触发器,若长时间无输出,触发器会输出进度
265+
k.watchDog.trigger = true
266+
//输出结果
261267
switch out.(type) {
262268
case *gonmap.AppBanner:
263269
banner := out.(*gonmap.AppBanner)
@@ -299,11 +305,61 @@ func (k *kscan) Output() {
299305
func (k *kscan) WatchDog() {
300306

301307
k.watchDog.wg.Add(1)
302-
308+
//触发器校准,每隔60秒会将触发器关闭
309+
go func() {
310+
for true {
311+
time.Sleep(60 * time.Second)
312+
k.watchDog.trigger = false
313+
}
314+
}()
315+
//轮询触发器,每隔一段时间会检测触发器是否打开
316+
go func() {
317+
for true {
318+
time.Sleep(59 * time.Second)
319+
if k.watchDog.trigger == false {
320+
if num := k.pool.host.JobsList.Length(); num > 0 {
321+
i := k.pool.host.JobsList.Peek()
322+
info := i.(string)
323+
slog.Warningf("当前主机存活性检测任务未完成,其并发协程数为:%d,具体其中的一个协程信息为:%s", num, info)
324+
continue
325+
}
326+
if num := k.pool.port.JobsList.Length(); num > 0 {
327+
i := k.pool.port.JobsList.Peek()
328+
info := i.(string)
329+
slog.Warningf("当前端口存活性检测任务未完成,其并发协程数为:%d,具体其中的一个协程信息为:%s", num, info)
330+
continue
331+
}
332+
if num := k.pool.tcpBanner.JobsList.Length(); num > 0 {
333+
i := k.pool.tcpBanner.JobsList.Peek()
334+
info := i.(string)
335+
slog.Warningf("当前TCP层指纹识别任务未完成,其并发协程数为:%d,具体其中的一个协程信息为:%s", num, info)
336+
continue
337+
}
338+
if num := k.pool.appBanner.JobsList.Length(); num > 0 {
339+
i := k.pool.appBanner.JobsList.Peek()
340+
var info string
341+
switch i.(type) {
342+
case string:
343+
info = i.(string)
344+
case *gonmap.TcpBanner:
345+
tcpBanner := i.(*gonmap.TcpBanner)
346+
if tcpBanner == nil {
347+
continue
348+
}
349+
info = tcpBanner.Target.URI()
350+
}
351+
slog.Warningf("当前应用层指纹检测并发协程数为:%d,具体其中的一个协程信息为:%s", num, info)
352+
continue
353+
}
354+
}
355+
}
356+
}()
357+
//Hydra模块
303358
if app.Setting.Hydra {
304359
slog.Info("hydra模块已开启,开始监听暴力破解任务")
305360
k.watchDog.wg.Add(1)
306361
}
362+
307363
for out := range k.pool.appBanner.Out {
308364
k.watchDog.output <- out
309365
if app.Setting.Hydra {

0 commit comments

Comments
 (0)