diff --git a/global/version.go b/global/version.go index 487e4f6..df08f28 100644 --- a/global/version.go +++ b/global/version.go @@ -1,7 +1,7 @@ package global const ( - Version = "v3.3.5" //当前版本号 - Releasenotes = "优化并发逻辑,增加调试开关,增加识别web指纹" //版本说明 + Version = "v3.3.6" //当前版本号 + Releasenotes = "优化并发逻辑,增加调试开关" //版本说明 RepoUrl = "https://api.github.com/repos/selinuxg/Golin/releases/latest" //仓库最新版本 ) diff --git a/port/crack/ftp.go b/port/crack/ftp.go index 46f2130..331ec6f 100644 --- a/port/crack/ftp.go +++ b/port/crack/ftp.go @@ -9,16 +9,6 @@ import ( ) func ftpcon(ctx context.Context, cancel context.CancelFunc, ip, user, passwd string, port, timeout int, ch <-chan struct{}, wg *sync.WaitGroup) { - defer func() { - wg.Done() - <-ch - }() - select { - case <-ctx.Done(): - return - default: - } - c, err := ftp.Dial(fmt.Sprintf("%s:%d", ip, port), ftp.DialWithTimeout(time.Duration(timeout)*time.Second)) if err == nil { err = c.Login(user, passwd) diff --git a/port/crack/mysql.go b/port/crack/mysql.go index d7eb958..268487e 100644 --- a/port/crack/mysql.go +++ b/port/crack/mysql.go @@ -10,15 +10,6 @@ import ( ) func mySql(ctx context.Context, cancel context.CancelFunc, ip, user, passwd string, port, timeout int, ch <-chan struct{}, wg *sync.WaitGroup) { - defer func() { - wg.Done() - <-ch - }() - select { - case <-ctx.Done(): - return - default: - } dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local&timeout=%ds", user, passwd, ip, port, "mysql", timeout) _, err := gorm.Open(mysql.Open(dsn), &gorm.Config{ Logger: logger.Default.LogMode(logger.Silent), // 设置日志级别为 silent diff --git a/port/crack/oracle.go b/port/crack/oracle.go index 4ebc488..1d937f2 100644 --- a/port/crack/oracle.go +++ b/port/crack/oracle.go @@ -10,16 +10,6 @@ import ( ) func oraclecon(ctx context.Context, cancel context.CancelFunc, ip, user, passwd string, port, timeout int, ch <-chan struct{}, wg *sync.WaitGroup) { - defer func() { - wg.Done() - <-ch - }() - select { - case <-ctx.Done(): - return - default: - } - dataSourceName := fmt.Sprintf("oracle://%s:%s@%s:%d/orcl", user, passwd, ip, port) db, err := sql.Open("oracle", dataSourceName) if err == nil { diff --git a/port/crack/pgsql.go b/port/crack/pgsql.go index c63e8cc..64e810e 100644 --- a/port/crack/pgsql.go +++ b/port/crack/pgsql.go @@ -10,12 +10,6 @@ import ( ) func pgsql(ctx context.Context, cancel context.CancelFunc, ip, user, passwd string, port, timeout int, ch <-chan struct{}, wg *sync.WaitGroup) { - defer done(ch, wg) - select { - case <-ctx.Done(): - return - default: - } dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=postgres port=%d sslmode=disable TimeZone=Asia/Shanghai connect_timeout=%d", ip, user, passwd, port, timeout) _, err := gorm.Open(postgres.Open(dsn), &gorm.Config{ Logger: logger.Default.LogMode(logger.Silent), diff --git a/port/crack/rdp.go b/port/crack/rdp.go index 18613e7..9c6e6a0 100644 --- a/port/crack/rdp.go +++ b/port/crack/rdp.go @@ -33,12 +33,6 @@ type Client struct { } func rdpcon(ctx context.Context, cancel context.CancelFunc, ip, user, passwd string, port, timeout int, ch <-chan struct{}, wg *sync.WaitGroup) { - defer done(ch, wg) - select { - case <-ctx.Done(): - return - default: - } glog.SetLevel(5) //禁止日志输出 logger := log.New(os.Stdout, "", 0) glog.SetLogger(logger) diff --git a/port/crack/redis.go b/port/crack/redis.go index 3683403..ce0ae4e 100644 --- a/port/crack/redis.go +++ b/port/crack/redis.go @@ -11,16 +11,6 @@ import ( var ctx = context.Background() func rediscon(ctx context.Context, cancel context.CancelFunc, ip, user, passwd string, port, timeout int, ch <-chan struct{}, wg *sync.WaitGroup) { - defer func() { - wg.Done() - <-ch - }() - select { - case <-ctx.Done(): - return - default: - } - client := redis.NewClient(&redis.Options{ Addr: fmt.Sprintf("%s:%d", ip, port), Username: user, diff --git a/port/crack/run.go b/port/crack/run.go index 3914b23..1312171 100644 --- a/port/crack/run.go +++ b/port/crack/run.go @@ -4,8 +4,10 @@ import ( "context" "fmt" "github.com/fatih/color" + "golin/global" "strconv" "sync" + "time" ) // ConnectionFunc 定义一个函数类型 @@ -29,7 +31,7 @@ var connectionFuncs = map[string]ConnectionFunc{ func Run(host, port string, Timeout, chanCount int, mode string) { ch := make(chan struct{}, chanCount) wg := sync.WaitGroup{} - ctx, cancel := context.WithCancel(context.Background()) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(Timeout)) defer cancel() //确保所有的goroutine都已经退出 newport, _ := strconv.Atoi(port) @@ -41,7 +43,7 @@ func Run(host, port string, Timeout, chanCount int, mode string) { ch <- struct{}{} wg.Add(1) if connFunc, ok := connectionFuncs[mode]; ok { - go connFunc(ctx, cancel, host, user, passwd, newport, Timeout, ch, &wg) + go crackOnce(ctx, cancel, host, user, passwd, newport, Timeout, ch, &wg, connFunc, mode) } else { wg.Done() <-ch @@ -53,6 +55,9 @@ func Run(host, port string, Timeout, chanCount int, mode string) { } func end(host, user, passwd string, port int, mode string) { + global.PrintLock.Lock() + defer global.PrintLock.Unlock() + fmt.Printf("\033[2K\r") // 擦除整行 fmt.Printf("\r| %-2s | %-15s | %-4d |%-6s|%-4s|%-50s \n", fmt.Sprintf("%s", color.GreenString("%s", "✓")), @@ -64,7 +69,28 @@ func end(host, user, passwd string, port int, mode string) { ) fmt.Printf("\033[2K\r") // 擦除整行 } + func done(ch <-chan struct{}, wg *sync.WaitGroup) { <-ch wg.Done() } + +func crackOnce(ctx context.Context, cancel context.CancelFunc, host, user, passwd string, newport, timeout int, ch <-chan struct{}, wg *sync.WaitGroup, connFunc ConnectionFunc, key string) { + defer done(ch, wg) + + hasDone := make(chan struct{}, 1) + go func() { + connFunc(ctx, cancel, host, user, passwd, newport, timeout, ch, wg) + hasDone <- struct{}{} + }() + + select { + case <-hasDone: + return + case <-ctx.Done(): + if global.Debug { + fmt.Println(key, host, user, passwd, "time out") + } + return + } +} diff --git a/port/crack/smb.go b/port/crack/smb.go index 236ddec..3749469 100644 --- a/port/crack/smb.go +++ b/port/crack/smb.go @@ -7,16 +7,6 @@ import ( ) func smbcon(ctx context.Context, cancel context.CancelFunc, ip, user, passwd string, port, timeout int, ch <-chan struct{}, wg *sync.WaitGroup) { - defer func() { - wg.Done() - <-ch - }() - select { - case <-ctx.Done(): - return - default: - } - options := smb.Options{ Host: ip, Port: port, diff --git a/port/crack/sqlserver.go b/port/crack/sqlserver.go index 1b30df1..b245d03 100644 --- a/port/crack/sqlserver.go +++ b/port/crack/sqlserver.go @@ -10,16 +10,6 @@ import ( ) func sqlservercon(ctx context.Context, cancel context.CancelFunc, ip, user, passwd string, port, timeout int, ch <-chan struct{}, wg *sync.WaitGroup) { - defer func() { - wg.Done() - <-ch - }() - select { - case <-ctx.Done(): - return - default: - } - dsn := fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=master&timeout=%ds", user, passwd, ip, port, timeout) _, err := gorm.Open(sqlserver.Open(dsn), &gorm.Config{ Logger: logger.Default.LogMode(logger.Silent), diff --git a/port/crack/ssh.go b/port/crack/ssh.go index fc387b5..1cfd667 100644 --- a/port/crack/ssh.go +++ b/port/crack/ssh.go @@ -9,15 +9,6 @@ import ( ) func SSH(ctx context.Context, cancel context.CancelFunc, ip, user, passwd string, port, timeout int, ch <-chan struct{}, wg *sync.WaitGroup) { - defer func() { - wg.Done() - <-ch - }() - select { - case <-ctx.Done(): - return - default: - } configssh := &ssh.ClientConfig{ Timeout: time.Duration(timeout) * time.Second, // ssh连接timeout时间 User: user, diff --git a/port/crack/telnet.go b/port/crack/telnet.go index f6ea56f..ffd2b53 100644 --- a/port/crack/telnet.go +++ b/port/crack/telnet.go @@ -11,16 +11,6 @@ import ( ) func telnetcon(ctx context.Context, cancel context.CancelFunc, ip, user, passwd string, port, timeout int, ch <-chan struct{}, wg *sync.WaitGroup) { - defer func() { - wg.Done() - <-ch - }() - select { - case <-ctx.Done(): - return - default: - } - conn, err := telnet.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, port), time.Duration(timeout)) if err != nil { return diff --git a/port/crack/tomcat.go b/port/crack/tomcat.go index 0b0a85c..9a3a9fe 100644 --- a/port/crack/tomcat.go +++ b/port/crack/tomcat.go @@ -12,16 +12,6 @@ import ( ) func tomcat(ctx context.Context, cancel context.CancelFunc, ip, user, passwd string, port, timeout int, ch <-chan struct{}, wg *sync.WaitGroup) { - defer func() { - wg.Done() - <-ch - }() - select { - case <-ctx.Done(): - return - default: - } - url := fmt.Sprintf("%s:%d", ip, port) base64passwd := fmt.Sprintf("%s:%s", user, passwd) base64passwd = base64.StdEncoding.EncodeToString([]byte(base64passwd))