Skip to content

Commit de0536c

Browse files
Merge pull request #90 from step-security/int
Add logging
2 parents 1dc7f42 + 1ad703e commit de0536c

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

agent.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net/http"
77
"os"
8+
"sync"
89
"time"
910

1011
"github.com/florianl/go-nflog/v2"
@@ -46,6 +47,8 @@ type IPTables interface {
4647
ClearChain(table, chain string) error
4748
}
4849

50+
var fileMutex sync.Mutex
51+
4952
// Run the agent
5053
// TODO: move all inputs into a struct
5154
func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
@@ -90,6 +93,23 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
9093

9194
dnsConfig := DnsConfig{}
9295

96+
var ipAddressEndpoints []ipAddressEndpoint
97+
if config.EgressPolicy == EgressPolicyBlock {
98+
endpoints := addImplicitEndpoints(config.Endpoints)
99+
for _, endpoint := range endpoints {
100+
// this will cause domain, IP mapping to be cached
101+
ipAddress, err := dnsProxy.getIPByDomain(endpoint.domainName)
102+
if err != nil {
103+
writeLog(fmt.Sprintf("Error resolving allowed domain %v", err))
104+
RevertChanges(iptables, nflog, cmd, resolvdConfigPath, dockerDaemonConfigPath, dnsConfig)
105+
return err
106+
}
107+
108+
// create list of ip address to be added to firewall
109+
ipAddressEndpoints = append(ipAddressEndpoints, ipAddressEndpoint{ipAddress: ipAddress, port: fmt.Sprintf("%d", endpoint.port)})
110+
}
111+
}
112+
93113
// Change DNS config on host, causes processes to use agent's DNS proxy
94114
if err := dnsConfig.SetDNSServer(cmd, resolvdConfigPath, tempDir); err != nil {
95115
writeLog(fmt.Sprintf("Error setting DNS server %v", err))
@@ -130,7 +150,6 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
130150

131151
writeLog("added audit rules")
132152
} else if config.EgressPolicy == EgressPolicyBlock {
133-
var ipAddressEndpoints []ipAddressEndpoint
134153

135154
writeLog(fmt.Sprintf("Allowed domains:%v", config.Endpoints))
136155

@@ -143,19 +162,6 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
143162

144163
// Start network monitor
145164
go netMonitor.MonitorNetwork(nflog, errc) // listens for NFLOG messages
146-
endpoints := addImplicitEndpoints(config.Endpoints)
147-
for _, endpoint := range endpoints {
148-
// this will cause domain, IP mapping to be cached
149-
ipAddress, err := dnsProxy.getIPByDomain(endpoint.domainName)
150-
if err != nil {
151-
writeLog(fmt.Sprintf("Error resolving allowed domain %v", err))
152-
RevertChanges(iptables, nflog, cmd, resolvdConfigPath, dockerDaemonConfigPath, dnsConfig)
153-
return err
154-
}
155-
156-
// create list of ip address to be added to firewall
157-
ipAddressEndpoints = append(ipAddressEndpoints, ipAddressEndpoint{ipAddress: ipAddress, port: fmt.Sprintf("%d", endpoint.port)})
158-
}
159165

160166
if err := addBlockRulesForGitHubHostedRunner(ipAddressEndpoints); err != nil {
161167
writeLog(fmt.Sprintf("Error setting firewall for allowed domains %v", err))
@@ -212,6 +218,9 @@ func RevertChanges(iptables *Firewall, nflog AgentNflogger,
212218
}
213219

214220
func writeLog(message string) {
221+
fileMutex.Lock()
222+
defer fileMutex.Unlock()
223+
215224
f, _ := os.OpenFile("/home/agent/agent.log",
216225
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
217226

dnsproxy.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ func (proxy *DNSProxy) getIPByDomain(domain string) (string, error) {
108108
if answer.Type == 1 {
109109
proxy.Cache.Set(domain, answer.Data)
110110

111+
go writeLog(fmt.Sprintf("domain resolved: %s, ip address: %s", domain, answer.Data))
112+
111113
go proxy.ApiClient.sendDNSRecord(proxy.CorrelationId, proxy.Repo, domain, answer.Data)
112114

113115
return answer.Data, nil

netmon.go

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

33
import (
44
"context"
5+
"fmt"
56
"sync"
67
"time"
78

@@ -94,6 +95,10 @@ func (netMonitor *NetworkMonitor) handlePacket(attrs nflog.Attribute) {
9495
if isSYN {
9596
netMonitor.ApiClient.sendNetConnection(netMonitor.CorrelationId, netMonitor.Repo,
9697
ipv4.DstIP.String(), port, netMonitor.Status, timestamp, Tool{Name: Unknown, SHA256: Unknown})
98+
99+
if netMonitor.Status == "Dropped" {
100+
go writeLog(fmt.Sprintf("ip address dropped: %s", ipv4.DstIP.String()))
101+
}
97102
}
98103
}
99104
netMonitor.netMutex.Unlock()

0 commit comments

Comments
 (0)