Skip to content

Commit

Permalink
Merge pull request #34 from synfinatic/str2pcap
Browse files Browse the repository at this point in the history
Add str2pcap tool for taking debug logs and making a pcap
  • Loading branch information
synfinatic authored Oct 16, 2020
2 parents 05c4d80 + 46a1ad4 commit 02e3cdc
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Added:

- Support for Wireguard (LinkType RAW) interfaces #29
- Add str2pcap for improved debugging of logs

## v0.0.4 - 02-10-2020

Expand Down
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ BUILDINFOS := $(shell date +%FT%T%z)$(BUILDINFOSDET)
HOSTNAME := $(shell hostname)
LDFLAGS := -X "main.Version=$(PROJECT_VERSION)" -X "main.Buildinfos=$(BUILDINFOS)" -X "main.Tag=$(PROJECT_TAG)" -X "main.CommitID=$(PROJECT_COMMIT)"
OUTPUT_NAME := $(DIST_DIR)$(PROJECT_NAME)-$(PROJECT_VERSION)-$(GOOS)-$(GOARCH)
STR2PCAP_NAME := $(DIST_DIR)str2pcap-$(PROJECT_VERSION)-$(GOOS)-$(GOARCH)

ALL: $(OUTPUT_NAME) ## Build binary

ALL: $(OUTPUT_NAME) str2pcap ## Build binary

str2pcap: $(STR2PCAP_NAME)

$(STR2PCAP_NAME): str2pcap/*.go
go build -o $(STR2PCAP_NAME) str2pcap/*.go

include help.mk # place after ALL target and before all other targets

Expand Down
74 changes: 74 additions & 0 deletions str2pcap/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

// Code to take a file with each line representing a packet in hex
// Intended to be used with the log output from udp-proxy-2020

import (
"bufio"
"encoding/hex"
"os"
"time"

"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcapgo"
log "github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
)

func main() {
var out = flag.String("out", "", "Pcap file to create")
var in = flag.String("in", "", "Input file name with packet data to read")
var dlt = flag.Uint8("dlt", 1, "DLT value")
var debug = flag.Bool("debug", false, "Enable debugging")

flag.Parse()
if *debug == true {
log.SetReportCaller(true)
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.WarnLevel)
}

if len(*out) == 0 || len(*in) == 0 {
log.Fatal("Please specify --in, --out and --dlt")
}

infile, err := os.Open(*in)
if err != nil {
log.Fatalf("--in %s: %s", *in, err)
}
inScanner := bufio.NewScanner(infile)
inScanner.Split(bufio.ScanLines)

fh, err := os.Create(*out)
if err != nil {
log.Fatalf("--out %s: %s", *out, err)
}

var linktype = layers.LinkType(*dlt)
pcap := pcapgo.NewWriterNanos(fh)
pcap.WriteFileHeader(65535, linktype)
var i = 0
for inScanner.Scan() {
i += 1
bytes, err := hex.DecodeString(inScanner.Text())
if err != nil {
log.Fatalf("reading line %d: %s", i, err)
}

ci := gopacket.CaptureInfo{
Timestamp: time.Time{},
CaptureLength: len(bytes),
Length: len(bytes),
InterfaceIndex: 0,
}
err = pcap.WritePacket(ci, bytes)
if err != nil {
log.Fatal(err)
}
}

infile.Close()
// no method to close a gopcap Writer???? WTF?
}

0 comments on commit 02e3cdc

Please sign in to comment.