Skip to content

Commit 0eed5aa

Browse files
committed
Pass bind IP in Init()
This change allows us to initialize connections in Init as well as fail early in tunnel-icmp module in case of lack of permissions.
1 parent 05aee80 commit 0eed5aa

File tree

10 files changed

+59
-44
lines changed

10 files changed

+59
-44
lines changed

cmd/run/run.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ func run(sims []*Simulation, extIP net.IP, size int) error {
296296
fmt.Print("\n")
297297

298298
okHosts := 0
299-
err := sim.Init()
299+
err := sim.Init(extIP)
300300
if err != nil {
301301
printMsg(sim, msgPrefixErrorInit+fmt.Sprint(err))
302302
} else {
@@ -326,7 +326,7 @@ func run(sims []*Simulation, extIP net.IP, size int) error {
326326

327327
if !dryRun {
328328
ctx, cancel := context.WithTimeout(context.Background(), sim.Timeout)
329-
if err := sim.Module.Simulate(ctx, extIP, host); err != nil {
329+
if err := sim.Module.Simulate(ctx, host); err != nil {
330330
// TODO: some module can return custom messages (e.g. hijack)
331331
// and "ERROR" prefix shouldn't be printed then
332332
printMsg(sim, fmt.Sprintf("ERROR: %s: %s", host, err.Error()))

simulator/dga.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package simulator
22

33
import (
44
"math/rand"
5+
"net"
56
"strings"
67

78
"github.com/alphasoc/flightsim/utils"
@@ -19,8 +20,8 @@ func NewDGA() *DGA {
1920
return &DGA{}
2021
}
2122

22-
func (DGA) Init() error {
23-
return nil
23+
func (s *DGA) Init(bind net.IP) error {
24+
return s.DNSResolveSimulator.Init(bind)
2425
}
2526

2627
func (DGA) Cleanup() {

simulator/miner.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,27 @@ const miningSubscribeBody string = `{"jsonrpc": "2.0", "id": 1, "method": "minin
1010

1111
//StratumMiner simulator
1212
type StratumMiner struct {
13+
bind net.IP
1314
}
1415

1516
//NewStratumMiner creates new StratumMiner simulator
1617
func NewStratumMiner() *StratumMiner {
1718
return &StratumMiner{}
1819
}
1920

20-
func (StratumMiner) Init() error {
21+
func (s *StratumMiner) Init(bind net.IP) error {
22+
s.bind = bind
2123
return nil
2224
}
2325

2426
func (StratumMiner) Cleanup() {
2527
}
2628

2729
//Simulate connection to mining pool using Stratum protocol
28-
func (m StratumMiner) Simulate(ctx context.Context, bind net.IP, dst string) error {
30+
func (s *StratumMiner) Simulate(ctx context.Context, dst string) error {
2931
d := &net.Dialer{}
30-
if bind != nil {
31-
d.LocalAddr = &net.TCPAddr{IP: bind}
32+
if s.bind != nil {
33+
d.LocalAddr = &net.TCPAddr{IP: s.bind}
3234
}
3335
conn, err := d.DialContext(ctx, "tcp", dst)
3436
if conn != nil {

simulator/scan.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ func NewPortScan() *PortScan {
6161
return &PortScan{}
6262
}
6363

64-
func (PortScan) Init() error {
65-
return nil
64+
func (s *PortScan) Init(bind net.IP) error {
65+
return s.tcp.Init(bind)
6666
}
6767

6868
func (PortScan) Cleanup() {
@@ -100,7 +100,7 @@ func (s *PortScan) Hosts(scope string, size int) ([]string, error) {
100100
return hosts, nil
101101
}
102102

103-
func (s *PortScan) Simulate(ctx context.Context, bind net.IP, dst string) error {
103+
func (s *PortScan) Simulate(ctx context.Context, dst string) error {
104104
callTimeout := 200 * time.Millisecond
105105
// If deadline set, divide the global timeout across every call (port)
106106
if d, ok := ctx.Deadline(); ok {
@@ -110,7 +110,7 @@ func (s *PortScan) Simulate(ctx context.Context, bind net.IP, dst string) error
110110

111111
for _, port := range scanPorts {
112112
ctx, _ := context.WithTimeout(ctx, callTimeout)
113-
err := s.tcp.Simulate(ctx, bind, fmt.Sprintf("%s:%d", dst, port))
113+
err := s.tcp.Simulate(ctx, fmt.Sprintf("%s:%d", dst, port))
114114
if err != nil {
115115
return err
116116
}

simulator/simulator.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
)
1111

1212
type Simulator interface {
13-
Simulate(ctx context.Context, bind net.IP, host string) error
14-
Init() error
13+
Init(bind net.IP) error
14+
Simulate(ctx context.Context, host string) error
1515
Cleanup()
1616
}
1717

@@ -33,19 +33,21 @@ func CreateModule(src HostSource, sim Simulator) Module {
3333
}
3434

3535
type TCPConnectSimulator struct {
36+
bind net.IP
3637
}
3738

38-
func (TCPConnectSimulator) Init() error {
39+
func (s *TCPConnectSimulator) Init(bind net.IP) error {
40+
s.bind = bind
3941
return nil
4042
}
4143

4244
func (TCPConnectSimulator) Cleanup() {
4345
}
4446

45-
func (TCPConnectSimulator) Simulate(ctx context.Context, bind net.IP, dst string) error {
47+
func (s *TCPConnectSimulator) Simulate(ctx context.Context, dst string) error {
4648
d := &net.Dialer{}
47-
if bind != nil {
48-
d.LocalAddr = &net.TCPAddr{IP: bind}
49+
if s.bind != nil {
50+
d.LocalAddr = &net.TCPAddr{IP: s.bind}
4951
}
5052

5153
conn, err := d.DialContext(ctx, "tcp", dst)
@@ -60,24 +62,26 @@ func (TCPConnectSimulator) Simulate(ctx context.Context, bind net.IP, dst string
6062
}
6163

6264
type DNSResolveSimulator struct {
65+
bind net.IP
6366
}
6467

65-
func (DNSResolveSimulator) Init() error {
68+
func (s *DNSResolveSimulator) Init(bind net.IP) error {
69+
s.bind = bind
6670
return nil
6771
}
6872

6973
func (DNSResolveSimulator) Cleanup() {
7074
}
7175

72-
func (DNSResolveSimulator) Simulate(ctx context.Context, bind net.IP, dst string) error {
76+
func (s *DNSResolveSimulator) Simulate(ctx context.Context, dst string) error {
7377
host, _, _ := net.SplitHostPort(dst)
7478
if host == "" {
7579
host = dst
7680
}
7781

7882
d := &net.Dialer{}
79-
if bind != nil {
80-
d.LocalAddr = &net.UDPAddr{IP: bind}
83+
if s.bind != nil {
84+
d.LocalAddr = &net.UDPAddr{IP: s.bind}
8185
}
8286
r := &net.Resolver{
8387
PreferGo: true,

simulator/simulator_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77

88
func TestTCPConnectSimulator(t *testing.T) {
99
var s TCPConnectSimulator
10-
err := s.Simulate(context.Background(), nil, "google.com:80")
10+
err := s.Simulate(context.Background(), "google.com:80")
1111
t.Log(err)
1212
}
1313

1414
func TestDNSResolveSimulator(t *testing.T) {
1515
var s DNSResolveSimulator
16-
err := s.Simulate(context.Background(), nil, "dsfnsfadsfds.com")
16+
err := s.Simulate(context.Background(), "dsfnsfadsfds.com")
1717
t.Log(err)
1818
}

simulator/spambot.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ func NewSpambot() *Spambot {
6969
return &Spambot{}
7070
}
7171

72-
func (Spambot) Init() error {
73-
return nil
72+
func (s *Spambot) Init(bind net.IP) error {
73+
return s.TCPConnectSimulator.Init(bind)
7474
}
7575

7676
func (Spambot) Cleanup() {

simulator/tor.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ func NewTorSimulator() *TorSimulator {
3535
return &TorSimulator{}
3636
}
3737

38-
func (t *TorSimulator) Init() error {
38+
// Tor creates tor connector;
39+
// There is no way to pass the bind IP to tor, so we ignore it.
40+
func (t *TorSimulator) Init(_ net.IP) error {
3941
tor, err := tor.Start(nil, &tor.StartConf{
4042
TempDataDirBase: os.TempDir(),
4143
RetainTempDataDir: false,
@@ -71,7 +73,7 @@ func (t TorSimulator) Hosts(scope string, size int) ([]string, error) {
7173
}
7274

7375
//Simulate connection to tor network
74-
func (t TorSimulator) Simulate(ctx context.Context, bind net.IP, dst string) error {
76+
func (t TorSimulator) Simulate(ctx context.Context, dst string) error {
7577
dialer, err := t.tor.Dialer(ctx, nil)
7678
if err != nil {
7779
return err

simulator/tunnel-dns.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,27 @@ import (
1111
)
1212

1313
// Tunnel simulator.
14-
type Tunnel struct{}
14+
type Tunnel struct {
15+
bind net.IP
16+
}
1517

1618
// NewTunnel creates dns tunnel simulator.
1719
func NewTunnel() *Tunnel {
1820
return &Tunnel{}
1921
}
2022

21-
func (Tunnel) Init() error {
23+
func (s *Tunnel) Init(bind net.IP) error {
24+
s.bind = bind
2225
return nil
2326
}
2427

2528
func (Tunnel) Cleanup() {
2629
}
2730

2831
// Simulate lookups for txt records for give host.
29-
func (*Tunnel) Simulate(ctx context.Context, extIP net.IP, host string) error {
32+
func (s *Tunnel) Simulate(ctx context.Context, host string) error {
3033
d := &net.Dialer{
31-
LocalAddr: &net.UDPAddr{IP: extIP},
34+
LocalAddr: &net.UDPAddr{IP: s.bind},
3235
}
3336
r := &net.Resolver{
3437
PreferGo: true,

simulator/tunnel-icmp.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,27 @@ const payloadSize int = 1400
1515

1616
//ICMPtunnel simulator
1717
type ICMPtunnel struct {
18+
c *icmp.PacketConn
1819
}
1920

2021
//NewICMPtunnel Creates new IMCP tunnel simulator
2122
func NewICMPtunnel() *ICMPtunnel {
2223
return &ICMPtunnel{}
2324
}
2425

25-
func (ICMPtunnel) Init() error {
26+
func (s *ICMPtunnel) Init(bind net.IP) error {
27+
c, err := icmp.ListenPacket("ip4:icmp", bind.String())
28+
if err != nil {
29+
return err
30+
}
31+
s.c = c
2632
return nil
2733
}
2834

29-
func (ICMPtunnel) Cleanup() {
35+
func (s *ICMPtunnel) Cleanup() {
36+
if s.c != nil {
37+
s.c.Close()
38+
}
3039
}
3140

3241
//Hosts returns host used for tunneling
@@ -35,15 +44,9 @@ func (ICMPtunnel) Hosts(scope string, size int) ([]string, error) {
3544
}
3645

3746
//Simulate IMCP tunneling for given dst
38-
func (ICMPtunnel) Simulate(ctx context.Context, bind net.IP, dst string) error {
39-
c, err := icmp.ListenPacket("ip4:icmp", bind.String())
40-
if err != nil {
41-
return err
42-
}
43-
defer c.Close()
44-
47+
func (s *ICMPtunnel) Simulate(ctx context.Context, dst string) error {
4548
deadline, _ := ctx.Deadline()
46-
c.SetDeadline(deadline)
49+
s.c.SetDeadline(deadline)
4750

4851
for i := 0; i < pingCount; i++ {
4952
r := make([]byte, payloadSize)
@@ -61,12 +64,12 @@ func (ICMPtunnel) Simulate(ctx context.Context, bind net.IP, dst string) error {
6164
if err != nil {
6265
return err
6366
}
64-
if _, err := c.WriteTo(binmsg, &net.IPAddr{IP: net.ParseIP(dst)}); err != nil {
67+
if _, err := s.c.WriteTo(binmsg, &net.IPAddr{IP: net.ParseIP(dst)}); err != nil {
6568
return err
6669
}
6770

6871
rb := make([]byte, 1500)
69-
_, _, err = c.ReadFrom(rb)
72+
_, _, err = s.c.ReadFrom(rb)
7073
if err != nil {
7174
return err
7275
}

0 commit comments

Comments
 (0)