Skip to content

Commit

Permalink
huuuuuge refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed May 11, 2020
1 parent b6561db commit 8b93cac
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 179 deletions.
106 changes: 0 additions & 106 deletions entropy.go

This file was deleted.

16 changes: 5 additions & 11 deletions inspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package peng

import (
"fmt"
"github.com/alessio-perugini/peng/pkg/portbitmap"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
_ "github.com/google/gopacket/layers" //Used to init internal struct
Expand Down Expand Up @@ -34,23 +35,16 @@ func (p *Peng) inspect(packet gopacket.Packet) {
if tcp.SYN && !tcp.ACK {
fmt.Println(tcp.DstPort)
if packetDestToMyPc {
p.ServerFlowBtmp.addPortToBitmap(uint16(tcp.DstPort))
addPortToBitmap(uint16(tcp.DstPort), p.ServerTraffic)
} else {
p.ClientFlowBtmp.addPortToBitmap(uint16(tcp.DstPort))
addPortToBitmap(uint16(tcp.DstPort), p.ClientTraffic)
}
}
}
}

func (cf *ClientTraffic) addPortToBitmap(port uint16) {
err := cf.Portbitmap.AddPort(port)
if err != nil {
log.Println(err.Error())
}
}

func (sf *ServerTraffic) addPortToBitmap(port uint16) {
err := sf.Portbitmap.AddPort(port)
func addPortToBitmap(port uint16, pBitmap *portbitmap.PortBitmap) {
err := pBitmap.AddPort(port)
if err != nil {
log.Println(err.Error())
}
Expand Down
90 changes: 31 additions & 59 deletions peng.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,8 @@ import (
)

type Peng struct {
Config *Config
//Portbitmap *portbitmap.PortBitmap
ClientFlowBtmp ClientTraffic
ServerFlowBtmp ServerTraffic
}

type ClientTraffic struct {
Portbitmap *portbitmap.PortBitmap
peng *Peng //TODO u sure?
}

type ServerTraffic struct {
Portbitmap *portbitmap.PortBitmap
peng *Peng //TODO u sure?
Config *Config
ClientTraffic, ServerTraffic *portbitmap.PortBitmap
}

type Config struct {
Expand All @@ -52,18 +40,11 @@ func New(cfg *Config) *Peng {
NumberOfBits: cfg.NumberOfBits,
}
var peng = Peng{
Config: cfg,
ClientFlowBtmp: ClientTraffic{
Portbitmap: portbitmap.New(bitmapConfig),
},
ServerFlowBtmp: ServerTraffic{
Portbitmap: portbitmap.New(bitmapConfig),
},
Config: cfg,
ClientTraffic: portbitmap.New(bitmapConfig),
ServerTraffic: portbitmap.New(bitmapConfig),
}

peng.ServerFlowBtmp.peng = &peng //TODO ugly stuff here
peng.ClientFlowBtmp.peng = &peng //TODO ugly stuff here

return &peng
}

Expand All @@ -85,7 +66,7 @@ func (p *Peng) Start() {

packet := gopacket.NewPacketSource(pHandle, pHandle.LinkType())

time.AfterFunc(p.Config.TimeFrame, p.printAllInfo)
time.AfterFunc(p.Config.TimeFrame, p.PrintAllInfo)
for packet := range packet.Packets() {
p.inspect(packet)
//TODO maybe use custom layers to avoid realloc for each packets (memory improvment)
Expand All @@ -102,56 +83,47 @@ func (p *Peng) Start() {
pHandle.Close()
}

func (cf *ClientTraffic) printInfo() {
var p = cf
func printInfo(bitmap *portbitmap.PortBitmap) {
p := bitmap
binsEntropy := p.EntropyOfEachBin()
totalEntropy := p.EntropyTotal(binsEntropy)
influxField := map[string]interface{}{
"out": totalEntropy,
}
p.peng.PushToInfluxDb(influxField)

//Print some stats
fmt.Println(p.Portbitmap) //Print all bitmap
fmt.Println(p) //Print all bitmap
fmt.Println("Bit set: ")
for i := 0; i < len(p.Portbitmap.InnerBitmap); i++ {
fmt.Println("bin number [", i, "] num (bit at 1): ", p.Portbitmap.InnerBitmap[i].GetBitSets())
for i := 0; i < len(p.InnerBitmap); i++ {
fmt.Println("bin number [", i, "] num (bit at 1): ", p.InnerBitmap[i].GetBitSets())
}
fmt.Println("EntropyOfEachBin: ", binsEntropy)
fmt.Println("EntropyTotal: ", totalEntropy)

p.Portbitmap.ClearAll()
p.ClearAll()
}

func (sf *ServerTraffic) printInfo() {
var p = sf
binsEntropy := p.EntropyOfEachBin()
totalEntropy := p.EntropyTotal(binsEntropy)
func (p *Peng) PrintAllInfo() {
fmt.Println("#[CLIENT]#")
printInfo(p.ClientTraffic)
fmt.Println("\n#------------------------------------------------#\n#[SERVER]#")
printInfo(p.ServerTraffic)

//TODO generalizzare influx
//Server
binsEntropy := p.ServerTraffic.EntropyOfEachBin()
totalEntropy := p.ServerTraffic.EntropyTotal(binsEntropy)
influxField := map[string]interface{}{
"in": totalEntropy,
"out": totalEntropy,
}
p.PushToInfluxDb(influxField)

p.peng.PushToInfluxDb(influxField)

//Print some stats
fmt.Println(p.Portbitmap) //Print all bitmap
fmt.Println("Bit set: ")
for i := 0; i < len(p.Portbitmap.InnerBitmap); i++ {
fmt.Println("bin number [", i, "] num (bit at 1): ", p.Portbitmap.InnerBitmap[i].GetBitSets())
//Client
binsEntropy = p.ClientTraffic.EntropyOfEachBin()
totalEntropy = p.ClientTraffic.EntropyTotal(binsEntropy)
influxField = map[string]interface{}{
"in": totalEntropy,
}
fmt.Println("EntropyOfEachBin: ", binsEntropy)
fmt.Println("EntropyTotal: ", totalEntropy)
p.PushToInfluxDb(influxField)

p.Portbitmap.ClearAll()
}

func (p *Peng) printAllInfo() {
fmt.Println("#[CLIENT]#")
p.ClientFlowBtmp.printInfo()
fmt.Println("\n#------------------------------------------------#")
fmt.Println("#[SERVER]#")
p.ServerFlowBtmp.printInfo()
time.AfterFunc(p.Config.TimeFrame, p.printAllInfo)
time.AfterFunc(p.Config.TimeFrame, p.PrintAllInfo)
}

func (p *Peng) PushToInfluxDb(fields map[string]interface{}) {
Expand Down
45 changes: 42 additions & 3 deletions pkg/portbitmap/port_bitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package portbitmap
import (
"errors"
b "github.com/alessio-perugini/peng/pkg/bitmap"
"math"
)

type PortBitmap struct {
Expand All @@ -17,7 +18,8 @@ type Config struct {
NumberOfBits uint
}

//TODO levare il puntatore a config
var epsilon = math.Nextafter(1.0, 2.0) - 1.0

func New(cfg *Config) *PortBitmap {
var InnerBitmap = make([]b.Bitmap, cfg.NumberOfBin)
cfg.NumberOfBits = cfg.SizeBitmap / cfg.NumberOfBin
Expand All @@ -39,13 +41,14 @@ func New(cfg *Config) *PortBitmap {
}
}

//TODO check bitBin overflow
func (p *PortBitmap) AddPort(port uint16) error {
indexBin, bitBin := p.HashFunc(port)
if indexBin >= uint16(len(p.InnerBitmap)) {
return errors.New("index to access the bin is invalid")
}
p.InnerBitmap[indexBin].SetBit(bitBin, true)
if insert := p.InnerBitmap[indexBin].SetBit(bitBin, true); !insert {
return errors.New("bit offset too big")
}
return nil
}

Expand All @@ -54,3 +57,39 @@ func (p *PortBitmap) ClearAll() {
p.InnerBitmap[i].ResetAllBits()
}
}

func (p *PortBitmap) EntropyOfEachBin() []float64 {
var total = float64(p.Config.NumberOfBits) //number of bits in the bin
var sum float64 //used to compute the entropy
allEntropy := make([]float64, 0, p.Config.NumberOfBin) //used to calculate entropy of each bin

for i := 0; i < len(p.InnerBitmap); i++ {
bitsAt1 := float64(p.InnerBitmap[i].GetBitSets()) / total
bitsAt0 := float64(uint64(p.Config.NumberOfBits)-p.InnerBitmap[i].GetBitSets()) / total

if bitsAt1 > epsilon && bitsAt0 > epsilon {
sum -= bitsAt1 * math.Log(bitsAt1)
sum -= bitsAt0 * math.Log(bitsAt0)
}
sum = sum / math.Log(2.0)
//this helps me to identifies the number of scanned port in entropy form
if bitsAt1 > bitsAt0 { //so i can distinguish if i'm in the range of [0-1] or [1-0] in term of standard gaussian
sum = 2 - sum //used to allow growth of entropy in wider range [0-2]
}

allEntropy = append(allEntropy, sum)
sum = 0
}

return allEntropy
}

func (p *PortBitmap) EntropyTotal(binsEntropy []float64) float64 {
var totalEntropy float64

for _, v := range binsEntropy {
totalEntropy += v
}

return totalEntropy / float64(p.Config.NumberOfBin)
}

0 comments on commit 8b93cac

Please sign in to comment.