-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding multi port listening option + adding readme details
- Loading branch information
binchick.in
committed
May 3, 2023
1 parent
d5297e5
commit 57bf16b
Showing
5 changed files
with
148 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,58 @@ | ||
package honey | ||
|
||
import ( | ||
"fmt" | ||
"context" | ||
"os" | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"cloud.google.com/go/pubsub" | ||
"google.golang.org/api/option" | ||
"cloud.google.com/go/pubsub" | ||
"google.golang.org/api/option" | ||
) | ||
|
||
var ctx = context.Background() | ||
|
||
|
||
type hostMetaData map[string]string | ||
|
||
type HoneyClient struct { | ||
client *pubsub.Client | ||
topic *pubsub.Topic | ||
hostMetaData hostMetaData | ||
client *pubsub.Client | ||
topic *pubsub.Topic | ||
hostMetaData hostMetaData | ||
} | ||
|
||
func (h *HoneyClient) Publish(data []byte) { | ||
msg := pubsub.Message{ | ||
Data: data, | ||
Attributes: h.hostMetaData, | ||
} | ||
r := h.topic.Publish(ctx, &msg) // Leave this unassigned for now. We might want to handle this later though. | ||
s, err := r.Get(ctx) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println(s) // Remove this line | ||
msg := pubsub.Message{ | ||
Data: data, | ||
Attributes: h.hostMetaData, | ||
} | ||
r := h.topic.Publish(ctx, &msg) // Leave this unassigned for now. We might want to handle this later though. | ||
s, err := r.Get(ctx) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println(s) // Remove this line | ||
} | ||
|
||
|
||
func NewHoneyClientFromEnv() HoneyClient { | ||
creds, projectid := checkEnvForCreds() | ||
pubSubClient, pubSubClientErr := pubsub.NewClient( | ||
ctx, | ||
projectid, | ||
option.WithCredentialsJSON(creds), | ||
) | ||
if pubSubClientErr != nil { | ||
panic(pubSubClientErr) | ||
} | ||
|
||
hostname, hostnameErr := os.Hostname() | ||
if hostnameErr != nil { | ||
panic(hostnameErr) | ||
} | ||
|
||
return HoneyClient{ | ||
client: pubSubClient, | ||
topic: pubSubClient.Topic("honey"), // TODO: Put topic string in environment | ||
hostMetaData: hostMetaData{ | ||
"hostname": hostname, | ||
}, | ||
} | ||
} | ||
creds, projectid := checkEnvForCreds() | ||
pubSubClient, pubSubClientErr := pubsub.NewClient( | ||
ctx, | ||
projectid, | ||
option.WithCredentialsJSON(creds), | ||
) | ||
if pubSubClientErr != nil { | ||
panic(pubSubClientErr) | ||
} | ||
|
||
hostname, hostnameErr := os.Hostname() | ||
if hostnameErr != nil { | ||
panic(hostnameErr) | ||
} | ||
|
||
return HoneyClient{ | ||
client: pubSubClient, | ||
topic: pubSubClient.Topic("honey"), // TODO: Put topic string in environment | ||
hostMetaData: hostMetaData{ | ||
"hostname": hostname, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,47 @@ | ||
package honey | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"strings" | ||
"time" | ||
"encoding/json" | ||
"net/http" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type LoggedRequest struct { | ||
Host string `json:"host"` | ||
Method string `json:"method"` | ||
Path string `json:"path"` | ||
RemoteAddress string `json:"remote_address"` | ||
UserAgent string `json:"user_agent"` | ||
QueryParams map[string]string `json:"query_params"` | ||
Headers map[string]string `json:"headers"` | ||
Body string `json:"body"` | ||
Time uint64 `json:"time"` | ||
Host string `json:"host"` | ||
Method string `json:"method"` | ||
Path string `json:"path"` | ||
RemoteAddress string `json:"remote_address"` | ||
UserAgent string `json:"user_agent"` | ||
QueryParams map[string]string `json:"query_params"` | ||
Headers map[string]string `json:"headers"` | ||
Body string `json:"body"` | ||
Time uint64 `json:"time"` | ||
} | ||
|
||
func (lr *LoggedRequest) ToJson() string { | ||
j, err := json.Marshal(lr) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return string(j) | ||
j, err := json.Marshal(lr) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return string(j) | ||
} | ||
|
||
func NewLoggedRequest(r http.Request) LoggedRequest { | ||
var decodedBody string | ||
if r.Method == http.MethodPost { | ||
decodedBody = decodeRequestBody(r.Body) | ||
} | ||
remoteHost := strings.Split(r.RemoteAddr, ":") | ||
return LoggedRequest{ | ||
Host: r.Host, | ||
Method: r.Method, | ||
Path: r.URL.Path, | ||
RemoteAddress: remoteHost[0], | ||
UserAgent: r.UserAgent(), | ||
QueryParams: processMapOfSlices(r.URL.Query()), | ||
Headers: processMapOfSlices(r.Header), | ||
Body: decodedBody, | ||
Time: uint64(time.Now().Unix()), | ||
} | ||
var decodedBody string | ||
if r.Method == http.MethodPost { | ||
decodedBody = decodeRequestBody(r.Body) | ||
} | ||
remoteHost := strings.Split(r.RemoteAddr, ":") | ||
return LoggedRequest{ | ||
Host: r.Host, | ||
Method: r.Method, | ||
Path: r.URL.Path, | ||
RemoteAddress: remoteHost[0], | ||
UserAgent: r.UserAgent(), | ||
QueryParams: processMapOfSlices(r.URL.Query()), | ||
Headers: processMapOfSlices(r.Header), | ||
Body: decodedBody, | ||
Time: uint64(time.Now().Unix()), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"flag" | ||
"fmt" | ||
"net/http" | ||
"sync" | ||
|
||
"honey-collector/honey" | ||
"honey-collector/honey" | ||
) | ||
|
||
var honeyClient honey.HoneyClient | ||
var ( | ||
honeyClient honey.HoneyClient | ||
ports string | ||
) | ||
|
||
func ReqHandler(resp http.ResponseWriter, req *http.Request) { | ||
lr := honey.NewLoggedRequest(*req) | ||
honeyClient.Publish([]byte(lr.ToJson())) | ||
fmt.Fprintf(resp, "\\( ^ o ^)/") | ||
lr := honey.NewLoggedRequest(*req) | ||
honeyClient.Publish([]byte(lr.ToJson())) | ||
fmt.Fprintf(resp, "\\( ^ o ^)/") | ||
} | ||
|
||
func main() { | ||
defaultPort := 80 | ||
honeyClient = honey.NewHoneyClientFromEnv() | ||
fmt.Printf("Starting honey pot on port: %d\n", defaultPort) | ||
http.HandleFunc("/", ReqHandler) | ||
serverStartErr := http.ListenAndServe(fmt.Sprintf(":%d", defaultPort), nil) | ||
if serverStartErr != nil { | ||
panic(serverStartErr) | ||
} | ||
flag.StringVar(&ports, "ports", "", "Wrap your port list in double quotes") | ||
flag.Parse() | ||
|
||
var wg sync.WaitGroup | ||
preppedPorts := honey.PreparePorts(ports) | ||
honeyClient = honey.NewHoneyClientFromEnv() | ||
http.HandleFunc("/", ReqHandler) | ||
|
||
for _, p := range preppedPorts { | ||
wg.Add(1) | ||
addr := fmt.Sprintf(":%s", p) | ||
fmt.Printf("Starting honey pot on port: %s\n", p) | ||
go http.ListenAndServe(addr, nil) | ||
} | ||
wg.Wait() | ||
} |