Skip to content

Commit c1204c6

Browse files
committed
no more math random
1 parent bf7c869 commit c1204c6

File tree

4 files changed

+51
-20
lines changed

4 files changed

+51
-20
lines changed

protocols/tcp/smb/smb.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package smb
22

33
import (
44
"bytes"
5+
"crypto/rand"
56
"encoding/binary"
67
"errors"
7-
"math/rand"
8+
"math/big"
89
"time"
910

1011
"github.com/google/uuid"
@@ -87,9 +88,12 @@ func filetime(offset time.Duration) Filetime {
8788
}
8889
}
8990

90-
func random(min, max int) int {
91-
rand.Seed(time.Now().Unix())
92-
return rand.Intn(max-min) + min
91+
func random(min, max int) (int, error) {
92+
rn, err := rand.Int(rand.Reader, big.NewInt(int64(max-min)))
93+
if err != nil {
94+
return 0, err
95+
}
96+
return int(rn.Int64()) + min, nil
9397
}
9498

9599
func toBytes(smb interface{}) ([]byte, error) {
@@ -255,7 +259,11 @@ func MakeNegotiateProtocolResponse(header SMBHeader) (SMBHeader, []byte, error)
255259
smb.MaxTransactSize = [4]byte{0x04, 0x11}
256260
smb.MaxReadSize = [4]byte{0x00, 0x00, 0x01}
257261
smb.SystemTime = filetime(0)
258-
smb.ServerStartTime = filetime(time.Duration(random(1000, 2000)) * time.Hour)
262+
randomTime, err := random(1000, 2000)
263+
if err != nil {
264+
return SMBHeader{}, nil, err
265+
}
266+
smb.ServerStartTime = filetime(time.Duration(randomTime) * time.Hour)
259267

260268
data, err := toBytes(smb)
261269
return smb.Header, data, err

protocols/tcp/smtp.go

+24-11
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package tcp
33
import (
44
"bufio"
55
"context"
6+
"crypto/rand"
67
"fmt"
78
"log/slog"
8-
"math/rand"
9+
"math/big"
910
"net"
1011
"regexp"
1112
"strings"
@@ -34,13 +35,15 @@ func (c *Client) read() (string, error) {
3435
return c.bufin.ReadString('\n')
3536
}
3637

37-
func rwait() {
38-
// makes the process sleep for random time
39-
rand.Seed(time.Now().Unix())
38+
func randomSleep() error {
4039
// between 0.5 - 1.5 seconds
41-
rtime := rand.Intn(1500) + 500
42-
duration := time.Duration(rtime) * time.Millisecond
40+
rtime, err := rand.Int(rand.Reader, big.NewInt(1500))
41+
if err != nil {
42+
return err
43+
}
44+
duration := time.Duration(rtime.Int64()+500) * time.Millisecond
4345
time.Sleep(duration)
46+
return nil
4447
}
4548
func validateMail(query string) bool {
4649
email := regexp.MustCompile("^MAIL FROM:<.+@.+>$") // naive regex
@@ -64,7 +67,9 @@ func HandleSMTP(ctx context.Context, conn net.Conn, md connection.Metadata, logg
6467
bufin: bufio.NewReader(conn),
6568
bufout: bufio.NewWriter(conn),
6669
}
67-
rwait()
70+
if err := randomSleep(); err != nil {
71+
return err
72+
}
6873
client.w("220 Welcome!")
6974

7075
for {
@@ -78,13 +83,19 @@ func HandleSMTP(ctx context.Context, conn net.Conn, md connection.Metadata, logg
7883
query := strings.Trim(data, "\r\n")
7984
logger.Info(fmt.Sprintf("[smtp ] Payload : %q", query))
8085
if strings.HasPrefix(query, "HELO ") {
81-
rwait()
86+
if err := randomSleep(); err != nil {
87+
return err
88+
}
8289
client.w("250 Hello! Pleased to meet you.")
8390
} else if validateMail(query) {
84-
rwait()
91+
if err := randomSleep(); err != nil {
92+
return err
93+
}
8594
client.w("250 OK")
8695
} else if validateRCPT(query) {
87-
rwait()
96+
if err := randomSleep(); err != nil {
97+
return err
98+
}
8899
client.w("250 OK")
89100
} else if strings.Compare(query, "DATA") == 0 {
90101
client.w("354 End data with <CRLF>.<CRLF>")
@@ -104,7 +115,9 @@ func HandleSMTP(ctx context.Context, conn net.Conn, md connection.Metadata, logg
104115
break
105116
}
106117
}
107-
rwait()
118+
if err := randomSleep(); err != nil {
119+
return err
120+
}
108121
client.w("250 OK")
109122
} else if strings.Compare(query, "QUIT") == 0 {
110123
client.w("Bye")

protocols/tcp/tcp.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package tcp
22

33
import (
44
"context"
5+
"crypto/rand"
56
"crypto/sha256"
67
"encoding/hex"
78
"fmt"
89
"log/slog"
9-
"math/rand"
10+
"math/big"
1011
"net"
1112
"os"
1213
"path/filepath"
@@ -52,7 +53,11 @@ func storePayload(data []byte) (string, error) {
5253
}
5354

5455
func (s *tcpServer) sendRandom(conn net.Conn) error {
55-
randomBytes := make([]byte, 12+rand.Intn(500))
56+
randomInt, err := rand.Int(rand.Reader, big.NewInt(500))
57+
if err != nil {
58+
return err
59+
}
60+
randomBytes := make([]byte, 12+randomInt.Int64())
5661
if _, err := rand.Read(randomBytes); err != nil {
5762
return err
5863
}

protocols/tcp/telnet.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package tcp
33
import (
44
"bufio"
55
"context"
6+
"crypto/rand"
67
"crypto/sha256"
78
"encoding/hex"
89
"errors"
910
"fmt"
1011
"io"
1112
"log/slog"
12-
"math/rand"
13+
"math/big"
1314
"net"
1415
"net/http"
1516
"os"
@@ -211,7 +212,11 @@ func HandleTelnet(ctx context.Context, conn net.Conn, md connection.Metadata, lo
211212
}
212213

213214
if resp := miraiCom[strings.TrimSpace(cmd)]; len(resp) > 0 {
214-
if err := s.write(conn, resp[rand.Intn(len(resp))]+"\r\n"); err != nil {
215+
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(resp))))
216+
if err != nil {
217+
return err
218+
}
219+
if err := s.write(conn, resp[n.Int64()]+"\r\n"); err != nil {
215220
return err
216221
}
217222
} else {

0 commit comments

Comments
 (0)