Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
[优化] 使用 interactsh 反连随机选择可用的服务器
Browse files Browse the repository at this point in the history
  • Loading branch information
4ra1n committed Sep 10, 2024
1 parent b4c63a6 commit f73dce9
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
更新日志:

- [BUG] 修复某些情况 `path` 开头包含多个 `/` 问题
- [优化] 使用 `interactsh` 反连随机选择可用的服务器
- [优化] 开启 `debug` 时请求响应限制打印长度

感谢以下用户的贡献:
Expand Down
53 changes: 52 additions & 1 deletion reverse/interact.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ import (
"crypto/rand"
"crypto/rsa"
"encoding/json"
"errors"
"fmt"
"strings"
"sync"
"time"

"github.com/4ra1n/poc-runner/client"
"github.com/4ra1n/poc-runner/log"
"github.com/4ra1n/poc-runner/util"
"github.com/4ra1n/poc-runner/xerr"
)

Expand Down Expand Up @@ -75,7 +78,55 @@ type Interact struct {

func NewInteract(c *client.HttpClient, server string) (*Interact, error) {
if server == "" {
server = randomPick(defaultServers)
// 随机选取一个 default server
picker := util.NewPicker(defaultServers)

var (
ok bool
tryTimes int
)

for {
tryTimes++

if tryTimes > 5 {
return nil, xerr.Wrap(errors.New("cannot connect to interact.sh server"))
}

server, ok = picker.RandomPick()
if !ok {
// 只有所有的 server 被选完没得选才会返回 false
return nil, xerr.Wrap(errors.New("all default server is invalid"))
}

var (
httpErr error
httpsErr error
)

// 并发测试 http/https 只要有一个可用即可
wg := new(sync.WaitGroup)

wg.Add(2)

go func() {
defer wg.Done()
_, httpErr = c.Get("http://" + server)
}()

go func() {
defer wg.Done()
_, httpsErr = c.Get("https://" + server)
}()

wg.Wait()

// 任何一个不报错就使用它
if httpErr == nil || httpsErr == nil {
log.Infof("use reverse server: %s", server)
break
}
}
}
correlationID := randLower(20)
secretKey := randomUUID()
Expand Down
5 changes: 0 additions & 5 deletions reverse/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ func randomUUID() string {
return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:])
}

func randomPick(choices []string) string {
randomIndex := rand.Intn(len(choices))
return choices[randomIndex]
}

func randUpper(n int) string {
letterRunes := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
Expand Down
55 changes: 55 additions & 0 deletions util/picker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* poc-runner project
* Copyright (C) 2024 4ra1n
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package util

import (
"math/rand"
"time"
)

func init() {
rand.New(rand.NewSource(time.Now().UnixNano()))
}

type Picker struct {
choices []string
used map[int]bool
}

func NewPicker(choices []string) *Picker {
return &Picker{
choices: choices,
used: make(map[int]bool),
}
}

func (p *Picker) RandomPick() (string, bool) {
if len(p.used) == len(p.choices) {
return "", false
}
var index int
for {
index = rand.Intn(len(p.choices))
if !p.used[index] {
break
}
}
p.used[index] = true
return p.choices[index], true
}

0 comments on commit f73dce9

Please sign in to comment.