|
| 1 | +package gobustertftp |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "strings" |
| 9 | + "text/tabwriter" |
| 10 | + |
| 11 | + "github.com/OJ/gobuster/v3/libgobuster" |
| 12 | + |
| 13 | + "github.com/pin/tftp/v3" |
| 14 | +) |
| 15 | + |
| 16 | +// GobusterTFTP is the main type to implement the interface |
| 17 | +type GobusterTFTP struct { |
| 18 | + globalopts *libgobuster.Options |
| 19 | + options *OptionsTFTP |
| 20 | +} |
| 21 | + |
| 22 | +// NewGobusterTFTP creates a new initialized NewGobusterTFTP |
| 23 | +func NewGobusterTFTP(globalopts *libgobuster.Options, opts *OptionsTFTP) (*GobusterTFTP, error) { |
| 24 | + if globalopts == nil { |
| 25 | + return nil, fmt.Errorf("please provide valid global options") |
| 26 | + } |
| 27 | + |
| 28 | + if opts == nil { |
| 29 | + return nil, fmt.Errorf("please provide valid plugin options") |
| 30 | + } |
| 31 | + |
| 32 | + g := GobusterTFTP{ |
| 33 | + options: opts, |
| 34 | + globalopts: globalopts, |
| 35 | + } |
| 36 | + return &g, nil |
| 37 | +} |
| 38 | + |
| 39 | +// Name should return the name of the plugin |
| 40 | +func (d *GobusterTFTP) Name() string { |
| 41 | + return "TFTP enumeration" |
| 42 | +} |
| 43 | + |
| 44 | +// PreRun is the pre run implementation of gobustertftp |
| 45 | +func (d *GobusterTFTP) PreRun(ctx context.Context) error { |
| 46 | + _, err := tftp.NewClient(d.options.Server) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + return nil |
| 51 | +} |
| 52 | + |
| 53 | +// ProcessWord is the process implementation of gobustertftp |
| 54 | +func (d *GobusterTFTP) ProcessWord(ctx context.Context, word string, progress *libgobuster.Progress) error { |
| 55 | + c, err := tftp.NewClient(d.options.Server) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + c.SetTimeout(d.options.Timeout) |
| 60 | + wt, err := c.Receive(word, "octet") |
| 61 | + if err != nil { |
| 62 | + // file not found |
| 63 | + if d.globalopts.Verbose { |
| 64 | + progress.ResultChan <- Result{ |
| 65 | + Filename: word, |
| 66 | + Found: false, |
| 67 | + ErrorMessage: err.Error(), |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return nil |
| 72 | + } |
| 73 | + result := Result{ |
| 74 | + Filename: word, |
| 75 | + Found: true, |
| 76 | + } |
| 77 | + if n, ok := wt.(tftp.IncomingTransfer).Size(); ok { |
| 78 | + result.Size = n |
| 79 | + } |
| 80 | + progress.ResultChan <- result |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +func (d *GobusterTFTP) AdditionalWords(word string) []string { |
| 85 | + return []string{} |
| 86 | +} |
| 87 | + |
| 88 | +// GetConfigString returns the string representation of the current config |
| 89 | +func (d *GobusterTFTP) GetConfigString() (string, error) { |
| 90 | + var buffer bytes.Buffer |
| 91 | + bw := bufio.NewWriter(&buffer) |
| 92 | + tw := tabwriter.NewWriter(bw, 0, 5, 3, ' ', 0) |
| 93 | + o := d.options |
| 94 | + |
| 95 | + if _, err := fmt.Fprintf(tw, "[+] Server:\t%s\n", o.Server); err != nil { |
| 96 | + return "", err |
| 97 | + } |
| 98 | + |
| 99 | + if _, err := fmt.Fprintf(tw, "[+] Threads:\t%d\n", d.globalopts.Threads); err != nil { |
| 100 | + return "", err |
| 101 | + } |
| 102 | + |
| 103 | + if d.globalopts.Delay > 0 { |
| 104 | + if _, err := fmt.Fprintf(tw, "[+] Delay:\t%s\n", d.globalopts.Delay); err != nil { |
| 105 | + return "", err |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if _, err := fmt.Fprintf(tw, "[+] Timeout:\t%s\n", o.Timeout.String()); err != nil { |
| 110 | + return "", err |
| 111 | + } |
| 112 | + |
| 113 | + wordlist := "stdin (pipe)" |
| 114 | + if d.globalopts.Wordlist != "-" { |
| 115 | + wordlist = d.globalopts.Wordlist |
| 116 | + } |
| 117 | + if _, err := fmt.Fprintf(tw, "[+] Wordlist:\t%s\n", wordlist); err != nil { |
| 118 | + return "", err |
| 119 | + } |
| 120 | + |
| 121 | + if d.globalopts.PatternFile != "" { |
| 122 | + if _, err := fmt.Fprintf(tw, "[+] Patterns:\t%s (%d entries)\n", d.globalopts.PatternFile, len(d.globalopts.Patterns)); err != nil { |
| 123 | + return "", err |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if d.globalopts.Verbose { |
| 128 | + if _, err := fmt.Fprintf(tw, "[+] Verbose:\ttrue\n"); err != nil { |
| 129 | + return "", err |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if err := tw.Flush(); err != nil { |
| 134 | + return "", fmt.Errorf("error on tostring: %w", err) |
| 135 | + } |
| 136 | + |
| 137 | + if err := bw.Flush(); err != nil { |
| 138 | + return "", fmt.Errorf("error on tostring: %w", err) |
| 139 | + } |
| 140 | + |
| 141 | + return strings.TrimSpace(buffer.String()), nil |
| 142 | +} |
0 commit comments