@@ -4,16 +4,12 @@ import (
44 "bufio"
55 "context"
66 "crypto/rand"
7- "crypto/sha256"
8- "encoding/hex"
97 "errors"
108 "io"
119 "log/slog"
1210 "math/big"
1311 "net"
1412 "net/http"
15- "os"
16- "path/filepath"
1713 "regexp"
1814 "strings"
1915 "time"
@@ -95,46 +91,38 @@ func (s *telnetServer) read(conn net.Conn) (string, error) {
9591func (s * telnetServer ) getSample (cmd string , logger interfaces.Logger ) error {
9692 url := cmd [strings .Index (cmd , "http" ):]
9793 url = strings .Split (url , " " )[0 ]
94+ url = strings .TrimSpace (url )
9895 logger .Debug ("Fetching sample" , slog .String ("url" , url ), slog .String ("handler" , "telnet" ))
9996 resp , err := s .client .Get (url )
10097 if err != nil {
10198 return err
10299 }
103100 if resp .StatusCode != 200 {
104- return errors .New ("getSample read http: error: Non 200 status code on getSample" )
101+ return errors .New ("failed to fetch sample: " + resp . Status )
105102 }
106103 defer resp .Body .Close ()
107104 if resp .ContentLength <= 0 {
108- return errors .New ("getSample read http: error: Empty response body " )
105+ return errors .New ("content length is 0 " )
109106 }
110- bodyBuffer , err := io .ReadAll (resp .Body )
107+
108+ data , err := io .ReadAll (resp .Body )
111109 if err != nil {
112110 return err
113111 }
114- sum := sha256 .Sum256 (bodyBuffer )
115- // Ignoring errors for if the folder already exists
116- if err = os .MkdirAll ("samples" , os .ModePerm ); err != nil {
117- return err
118- }
119- sha256Hash := hex .EncodeToString (sum [:])
120- path := filepath .Join ("samples" , sha256Hash )
121- if _ , err = os .Stat (path ); err == nil {
122- logger .Debug ("getSample already known" , slog .String ("sha" , sha256Hash ), slog .String ("handler" , "telnet" ))
123- return nil
124- }
125- out , err := os .Create (path )
126- if err != nil {
127- return err
112+
113+ if len (data ) == 0 {
114+ return errors .New ("empty response body" )
128115 }
129- defer out . Close ()
130- _ , err = out . Write ( bodyBuffer )
116+
117+ sha256Hash , err := helpers . Store ( data , "samples" )
131118 if err != nil {
132119 return err
133120 }
121+
134122 logger .Info (
135- "new sample fetched from telnet " ,
123+ "New sample fetched" ,
136124 slog .String ("handler" , "telnet" ),
137- slog .String ("sha256 " , sha256Hash ),
125+ slog .String ("sample_hash " , sha256Hash ),
138126 slog .String ("source" , url ),
139127 )
140128 return nil
@@ -197,7 +185,12 @@ func HandleTelnet(ctx context.Context, conn net.Conn, md connection.Metadata, lo
197185 }
198186 for _ , cmd := range strings .Split (msg , ";" ) {
199187 if strings .Contains (strings .Trim (cmd , " " ), "wget http" ) {
200- go s .getSample (strings .Trim (cmd , " " ), logger )
188+ go func () {
189+ err := s .getSample (strings .Trim (cmd , " " ), logger )
190+ if err != nil {
191+ logger .Error ("Failed to get sample" , slog .String ("handler" , "telnet" ), producer .ErrAttr (err ))
192+ }
193+ }()
201194 }
202195 if strings .TrimRight (cmd , "" ) == " rm /dev/.t" {
203196 continue
@@ -226,7 +219,7 @@ func HandleTelnet(ctx context.Context, conn net.Conn, md connection.Metadata, lo
226219 }
227220 } else {
228221 // /bin/busybox YDKBI
229- re := regexp .MustCompile (`\/bin\/busybox (?P<applet>[A-Z ]+)` )
222+ re := regexp .MustCompile (`\/bin\/busybox (?P<applet>[A-Za-z ]+)` )
230223 match := re .FindStringSubmatch (cmd )
231224 if len (match ) > 1 {
232225 if err := s .write (conn , match [1 ]+ ": applet not found\r \n " ); err != nil {
0 commit comments