Skip to content
This repository has been archived by the owner on Mar 29, 2023. It is now read-only.

Commit

Permalink
refactor: runner
Browse files Browse the repository at this point in the history
  • Loading branch information
niudaii committed Sep 6, 2022
1 parent a25edd3 commit 428a889
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 45 deletions.
4 changes: 0 additions & 4 deletions cmd/crack/crack.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"github.com/niudaii/crack/internal/runner"
"github.com/projectdiscovery/gologger"
"time"
)

func main() {
Expand All @@ -12,8 +11,5 @@ func main() {
if err != nil {
gologger.Fatal().Msgf("Could not create runner: %v", err)
}
start := time.Now()
gologger.Info().Msgf("当前时间: %v", start.Format("2006-01-02 15:04:05"))
newRunner.Run()
gologger.Info().Msgf("运行时间: %v", time.Since(start))
}
24 changes: 19 additions & 5 deletions internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/niudaii/crack/pkg/crack"
"github.com/projectdiscovery/gologger"
"time"
)

type Runner struct {
Expand All @@ -12,32 +13,45 @@ type Runner struct {
}

func NewRunner(options *Options) (*Runner, error) {
crackRunner, err := crack.NewRunner(options.Threads, options.Timeout, options.Delay, options.CrackAll, options.Silent)
crackOptions := &crack.Options{
Threads: options.Threads,
Timeout: options.Timeout,
Delay: options.Delay,
CrackAll: options.CrackAll,
Silent: options.Silent,
}
crackRunner, err := crack.NewRunner(crackOptions)
if err != nil {
return nil, fmt.Errorf("NewRunner err, %v", err)
return nil, fmt.Errorf("crack.NewRunner() err, %v", err)
}
runner := &Runner{
return &Runner{
options: options,
crackRunner: crackRunner,
}
return runner, nil
}, nil
}

func (r *Runner) Run() {
start := time.Now()
gologger.Info().Msgf("当前时间: %v", start.Format("2006-01-02 15:04:05"))
// 解析目标
addrs := crack.ParseTargets(r.options.Targets)
addrs = crack.FilterModule(addrs, r.options.Module)
if len(addrs) == 0 {
gologger.Info().Msgf("目标为空")
return
}
// 存活探测
gologger.Info().Msgf("存活探测")
addrs = r.crackRunner.CheckAlive(addrs)
gologger.Info().Msgf("存活数量: %v", len(addrs))
// 服务爆破
results := r.crackRunner.Run(addrs, r.options.UserDict, r.options.PassDict)
if len(results) > 0 {
gologger.Info().Msgf("爆破成功: %v", len(results))
for _, result := range results {
gologger.Print().Msgf("%v -> %v %v", result.Protocol, result.Addr, result.UserPass)
}
}
// 运行时间
gologger.Info().Msgf("运行时间: %v", time.Since(start))
}
20 changes: 11 additions & 9 deletions pkg/crack/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
)

// CheckAlive 存活检测
func (e *Runner) CheckAlive(addrs []*IpAddr) (results []*IpAddr) {
func (r *Runner) CheckAlive(addrs []*IpAddr) (results []*IpAddr) {
// RunTask
mutex := &sync.Mutex{}
wg := &sync.WaitGroup{}
taskChan := make(chan *IpAddr, e.threads*2)
for i := 0; i < e.threads; i++ {
taskChan := make(chan *IpAddr, r.options.Threads)
for i := 0; i < r.options.Threads; i++ {
go func() {
for task := range taskChan {
if e.conn(task) {
if r.conn(task) {
mutex.Lock()
results = append(results, task)
mutex.Unlock()
Expand All @@ -27,11 +27,13 @@ func (e *Runner) CheckAlive(addrs []*IpAddr) (results []*IpAddr) {
}()
}

if e.silent {
if r.options.Silent {
for _, task := range addrs {
wg.Add(1)
taskChan <- task
}
close(taskChan)
wg.Wait()
} else {
bar := pb.StartNew(len(addrs))
for _, task := range addrs {
Expand All @@ -40,16 +42,16 @@ func (e *Runner) CheckAlive(addrs []*IpAddr) (results []*IpAddr) {
taskChan <- task
}
close(taskChan)
wg.Wait()
bar.Finish()
}
close(taskChan)
wg.Wait()

return
}

// conn 建立tcp连接
func (e *Runner) conn(ipAddr *IpAddr) (alive bool) {
_, err := net.DialTimeout("tcp", fmt.Sprintf("%v:%v", ipAddr.Ip, ipAddr.Port), time.Duration(e.timeout)*time.Second)
func (r *Runner) conn(ipAddr *IpAddr) (alive bool) {
_, err := net.DialTimeout("tcp", fmt.Sprintf("%v:%v", ipAddr.Ip, ipAddr.Port), time.Duration(r.options.Timeout)*time.Second)
if err == nil {
alive = true
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/crack/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var (
"mongodb": {"admin", "root"},
}

TemplatePass = []string{"{user}", "{user}!@#123", "{user}!@#456", "{user}#123", "{user}*PWD", "{user}1", "{user}11", "{user}12#$", "{user}123", "{user}123456", "{user}@111", "{user}@123", "{user}@123#4", "{user}@2016", "{user}@2017", "{user}@2018", "{user}@2019", "{user}@2020", "{user}@2021", "{user}_123"}
TemplatePass = []string{"{user}", "{user}!@#123", "{user}!@#456", "{user}#123", "{user}*PWD", "{user}1", "{user}11", "{user}12#$", "{user}123", "{user}123456", "{user}@111", "{user}@123", "{user}@123#4", "{user}@2016", "{user}@2017", "{user}@2018", "{user}@2019", "{user}@2020", "{user}@2021", "{user}@2022", "{user}_123"}

CommonPass = []string{"", "!QAZ2wsx", "000000", "1", "111111", "123", "123123", "12313", "123321", "1234", "12345!@#$%abc", "123456", "12345678", "123456789", "1234567890", "12345678;abc", "123456Aa", "123qwe!@#", "123qweASD", "1q2w3e", "1qaz2wsx", "1QAZ2wsx", "1qaz@WSX", "1QAZ@WSX", "1qazxsw2", "654321", "666666", "8888888", "a11111", "a123123", "a12345", "a123456", "a123456", "a123456.", "Aa123123", "Aa1234", "Aa1234.", "Aa12345", "Aa12345.", "Aa123456", "Aa123456!", "Aa123456789", "abc+123", "abc123", "abc123456", "abc@123", "admin", "admin123", "Admin123", "admin123!@#", "admin888", "admin@123", "Admin@123", "Admin@1234", "admin@888", "adminadmin", "adminPwd", "Asdfg@123", "Charge123", "P@ssw0rd", "P@ssw0rd!", "P@ssword", "p@ssword", "pass123", "pass@123", "Passw0rd", "password", "qwe123", "qwe123!@#", "root", "sysadmin", "system", "test", "test123", "xcv@123", "zxc1qaz", "Zxcvb123"}
)
42 changes: 22 additions & 20 deletions pkg/crack/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ import (
"time"
)

type Options struct {
Threads int
Timeout int
Delay int
CrackAll bool
Silent bool
}

type Runner struct {
threads int
timeout int
delay int
crackAll bool
silent bool
options *Options
}

func NewRunner(threads, timeout, delay int, crackAll, silent bool) (*Runner, error) {
func NewRunner(options *Options) (*Runner, error) {
return &Runner{
threads: threads,
timeout: timeout,
delay: delay,
crackAll: crackAll,
silent: silent,
options: options,
}, nil
}

Expand Down Expand Up @@ -78,16 +78,16 @@ func (r *Runner) Crack(addr *IpAddr, userDict []string, passDict []string) (resu
Protocol: addr.Protocol,
User: user,
Pass: pass,
Timeout: r.timeout,
Timeout: r.options.Timeout,
})
}
}
// RunTask
stopHashMap := map[string]bool{}
mutex := &sync.Mutex{}
wg := &sync.WaitGroup{}
taskChan := make(chan plugins.Service, r.threads)
for i := 0; i < r.threads; i++ {
taskChan := make(chan plugins.Service, r.options.Threads)
for i := 0; i < r.options.Threads; i++ {
go func() {
for task := range taskChan {
addrStr := fmt.Sprintf("%v:%v", addr.Ip, addr.Port)
Expand All @@ -106,7 +106,7 @@ func (r *Runner) Crack(addr *IpAddr, userDict []string, passDict []string) (resu
resp := scanFunc(&task)
switch resp {
case plugins.CrackSuccess:
if !r.crackAll {
if !r.options.CrackAll {
mutex.Lock()
stopHashMap[addrHash] = true
mutex.Unlock()
Expand All @@ -123,30 +123,32 @@ func (r *Runner) Crack(addr *IpAddr, userDict []string, passDict []string) (resu
mutex.Unlock()
case plugins.CrackFail:
}
if r.delay > 0 {
time.Sleep(time.Duration(r.delay) * time.Second)
if r.options.Delay > 0 {
time.Sleep(time.Duration(r.options.Delay) * time.Second)
}
wg.Done()
}
}()
}

if r.silent {
if r.options.Silent {
for _, task := range tasks {
wg.Add(1)
taskChan <- task
}
close(taskChan)
wg.Wait()
} else {
bar := pb.StartNew(len(tasks))
for _, task := range tasks {
bar.Increment()
wg.Add(1)
taskChan <- task
}
close(taskChan)
wg.Wait()
bar.Finish()
}
close(taskChan)
wg.Wait()

gologger.Info().Msgf("爆破结束")

Expand Down
16 changes: 10 additions & 6 deletions pkg/crack/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ func TestCrackAll(t *testing.T) {
*/
tests := map[string]*Runner{
"false": {
threads: 2,
timeout: 10,
crackAll: false,
options: &Options{
Threads: 1,
Timeout: 10,
CrackAll: false,
},
},
"true": {
threads: 2,
timeout: 10,
crackAll: true,
options: &Options{
Threads: 1,
Timeout: 10,
CrackAll: true,
},
},
}
addrs := []*IpAddr{
Expand Down

0 comments on commit 428a889

Please sign in to comment.