Projects STRLCPY reverse_ssh Commits 8cf67bb6
🤬
  • Make listen be able to listen server forwards on clients

  • Loading...
  • NHAS committed 11 months ago
    8cf67bb6
    1 parent c995f086
Revision indexing in progress... (symbol navigation in revisions will be accurate after indexed)
  • ■ ■ ■ ■ ■ ■
    internal/client/client.go
    skipped 1 lines
    2 2   
    3 3  import (
    4 4   "bytes"
     5 + "encoding/json"
    5 6   "fmt"
    6 7   "io"
    7 8   "log"
    skipped 164 lines
    172 173   log.Println("Successfully connnected", addr)
    173 174   
    174 175   go func() {
     176 + 
    175 177   for req := range reqs {
    176 178   
    177 179   switch req.Type {
    skipped 14 lines
    192 194   case "tcpip-forward":
    193 195   go handlers.StartRemoteForward(nil, req, sshConn)
    194 196   
     197 + case "query-tcpip-forwards":
     198 + serverRemoteForwards := handlers.GetServerRemoteForwards()
     199 + result, err := json.Marshal(&serverRemoteForwards)
     200 + if err != nil {
     201 + req.Reply(false, []byte(err.Error()))
     202 + continue
     203 + }
     204 + 
     205 + req.Reply(true, result)
     206 + 
    195 207   case "cancel-tcpip-forward":
    196 208   var rf internal.RemoteForwardRequest
    197 209   
    skipped 4 lines
    202 214   }
    203 215   
    204 216   go func(r *ssh.Request) {
     217 + 
    205 218   err := handlers.StopRemoteForward(rf)
    206 219   if err != nil {
    207 220   r.Reply(false, []byte(err.Error()))
    skipped 37 lines
  • ■ ■ ■ ■ ■
    internal/client/handlers/remoteforward.go
    skipped 11 lines
    12 12   "golang.org/x/crypto/ssh"
    13 13  )
    14 14   
     15 +type remoteforward struct {
     16 + Listener net.Listener
     17 + User *internal.User
     18 +}
     19 + 
    15 20  var (
    16 21   currentRemoteForwardsLck sync.RWMutex
    17  - currentRemoteForwards = map[internal.RemoteForwardRequest]net.Listener{}
     22 + currentRemoteForwards = map[internal.RemoteForwardRequest]remoteforward{}
    18 23  )
    19 24   
     25 +func GetServerRemoteForwards() (out []internal.RemoteForwardRequest) {
     26 + currentRemoteForwardsLck.RLock()
     27 + defer currentRemoteForwardsLck.RUnlock()
     28 + 
     29 + for a, c := range currentRemoteForwards {
     30 + if c.User == nil {
     31 + out = append(out, a)
     32 + }
     33 + }
     34 + 
     35 + return out
     36 +}
     37 + 
    20 38  func StopRemoteForward(rf internal.RemoteForwardRequest) error {
    21 39   currentRemoteForwardsLck.Lock()
    22 40   defer currentRemoteForwardsLck.Unlock()
    skipped 2 lines
    25 43   return fmt.Errorf("Unable to find remote forward request")
    26 44   }
    27 45   
    28  - currentRemoteForwards[rf].Close()
     46 + currentRemoteForwards[rf].Listener.Close()
    29 47   delete(currentRemoteForwards, rf)
    30 48   
    31 49   log.Println("Stopped listening on: ", rf.BindAddr, rf.BindPort)
    skipped 38 lines
    70 88   log.Println("Started listening on: ", l.Addr())
    71 89   
    72 90   currentRemoteForwardsLck.Lock()
    73  - currentRemoteForwards[rf] = l
     91 + currentRemoteForwards[rf] = remoteforward{
     92 + Listener: l,
     93 + User: user,
     94 + }
    74 95   currentRemoteForwardsLck.Unlock()
    75 96   
    76 97   for {
    skipped 69 lines
  • ■ ■ ■ ■ ■ ■
    internal/server/commands/listen.go
    1 1  package commands
    2 2   
    3 3  import (
     4 + "encoding/json"
    4 5   "errors"
    5 6   "fmt"
    6 7   "io"
    skipped 4 lines
    11 12   "github.com/NHAS/reverse_ssh/internal/server/clients"
    12 13   "github.com/NHAS/reverse_ssh/internal/server/multiplexer"
    13 14   "github.com/NHAS/reverse_ssh/internal/terminal"
     15 + "github.com/NHAS/reverse_ssh/internal/terminal/autocomplete"
    14 16   "github.com/NHAS/reverse_ssh/pkg/logger"
    15 17   "golang.org/x/crypto/ssh"
    16 18  )
    skipped 69 lines
    86 88   }
    87 89   
    88 90   if len(foundClients) == 0 {
    89  - return fmt.Errorf("No clients matched '%s'", client)
     91 + return fmt.Errorf("No clients matched '%s'", specifier)
     92 + }
     93 + 
     94 + if line.IsSet("l") {
     95 + 
     96 + for id, cc := range foundClients {
     97 + result, message, _ := cc.SendRequest("query-tcpip-forwards", true, nil)
     98 + if !result {
     99 + fmt.Fprintf(tty, "%s does not support querying server forwards\n", id)
     100 + continue
     101 + }
     102 + 
     103 + var remoteforwards []internal.RemoteForwardRequest
     104 + err := json.Unmarshal(message, &remoteforwards)
     105 + if err != nil {
     106 + fmt.Fprintf(tty, "%s sent an incompatiable message: %s\n", id, err)
     107 + continue
     108 + }
     109 + 
     110 + fmt.Fprintf(tty, "%s (%s %s): \n", id, clients.NormaliseHostname(cc.User()), cc.RemoteAddr().String())
     111 + for _, rf := range remoteforwards {
     112 + fmt.Fprintf(tty, "\t%s:%d\n", rf.BindAddr, rf.BindPort)
     113 + }
     114 + 
     115 + }
     116 + 
     117 + return nil
    90 118   }
    91 119   
    92 120   on := line.IsSet("on")
    skipped 103 lines
    196 224  }
    197 225   
    198 226  func (W *listen) Expect(line terminal.ParsedLine) []string {
     227 + 
     228 + if line.Section != nil {
     229 + switch line.Section.Value() {
     230 + case "c", "client":
     231 + return []string{autocomplete.RemoteId}
     232 + }
     233 + }
     234 + 
    199 235   return nil
    200 236  }
    201 237   
    skipped 22 lines
Please wait...
Page is in error, reload to recover