Skip to content

Commit

Permalink
XML generated at the end. TBD: Usefull XML generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Micheloss committed Jan 30, 2016
1 parent 505f010 commit c54681d
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 107 deletions.
6 changes: 6 additions & 0 deletions Net-Report
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Report xmlns="Net">
<id>100</id>
<tcpPackets>0</tcpPackets>
<UDPackets>0</UDPackets>
<icmpPackets>0</icmpPackets>
</Report>
222 changes: 115 additions & 107 deletions raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
var (
device string = "en1"
snapshot_len int32 = 1024
promiscuous bool = true
promiscuous bool = false
err error
timeout time.Duration = 1
handle *pcap.Handle
Expand All @@ -29,7 +29,7 @@ var (
total_tcp_data = 0
total_icmp = 0
total_udp = 0
ip_dst_detected = ""
ip_dst_detected = " "
)

type Report struct {
Expand Down Expand Up @@ -57,125 +57,120 @@ func printPacketInfo(packet gopacket.Packet) {
ipLayer := packet.Layer(layers.LayerTypeIPv4)
// if ipLayer != nil {
// fmt.Println("IPv4 layer detected.")
ip, _ := ipLayer.(*layers.IPv4)

// // IP layer variables:
// // Version (Either 4 or 6)
// // IHL (IP Header Length in 32-bit words)
// // TOS, Length, Id, Flags, FragOffset, TTL, Protocol (TCP?),
// // Checksum, SrcIP, DstIP
// fmt.Printf("From %s to %s\n", ip.SrcIP, ip.DstIP)
// fmt.Println("Protocol: ", ip.Protocol)
// fmt.Println()
// }

if !strings.Contains(ip_dst_detected, string(ip.DstIP)) {
names, err4 := net.LookupAddr(string(ip.DstIP))
if err4 != nil {
names[0] = "error-resolving"
}
ip_dst_detected = ip_dst_detected + "|" + string(ip.DstIP) + " " + names[0]

}
total_packs++
// Let's see if the packet is TCP
tcpLayer := packet.Layer(layers.LayerTypeTCP)
if tcpLayer != nil {
total_tcp++
// fmt.Println("TCP layer detected.")
tcp, _ := tcpLayer.(*layers.TCP)
if tcp.SYN {
total_tcp_SYN++
} else {

if tcp.ACK {
total_tcp_ACK++
} else {

total_tcp_data++
ip, err_im := ipLayer.(*layers.IPv4)
if err_im != true {

}
}
// // TCP layer variables:
// // SrcPort, DstPort, Seq, Ack, DataOffset, Window, Checksum, Urgent
// // Bool flags: FIN, SYN, RST, PSH, ACK, URG, ECE, CWR, NS
// fmt.Printf("From port %d to %d\n", tcp.SrcPort, tcp.DstPort)
// fmt.Println("Sequence number: ", tcp.Seq)
// fmt.Println()
} else {
// // IP layer variables:
// // Version (Either 4 or 6)
// // IHL (IP Header Length in 32-bit words)
// // TOS, Length, Id, Flags, FragOffset, TTL, Protocol (TCP?),
// // Checksum, SrcIP, DstIP
// fmt.Printf("From %s to %s\n", ip.SrcIP, ip.DstIP)
// fmt.Println("Protocol: ", ip.Protocol)
// fmt.Println()
// }
if ip.DstIP != nil {

fmt.Println(ip_dst_detected)
fmt.Println(ip.DstIP)
if !strings.Contains(ip_dst_detected, string(ip.DstIP)) {
names, err4 := net.LookupAddr(string(ip.DstIP))
if err4 != nil {
fmt.Println(err4.Error())
} else {
ip_dst_detected = ip_dst_detected + "|" + string(ip.DstIP) + " " + names[0]
}

udpLayer := packet.Layer(layers.LayerTypeUDP)
if udpLayer != nil {
// fmt.Println("UDP layer detected.")
// udp, _ := udpLayer.(*layers.UDP)

// // TCP layer variables:
// // SrcPort, DstPort, Seq, Ack, DataOffset, Window, Checksum, Urgent
// // Bool flags: FIN, SYN, RST, PSH, ACK, URG, ECE, CWR, NS
// fmt.Printf("From port %d to %d\n", udp.SrcPort, udp.DstPort)
// fmt.Println("Content number: ", udp.Contents)
// fmt.Println()
total_udp++
} else {

icmpLayer := packet.Layer(layers.LayerTypeICMPv4)
if icmpLayer != nil {
total_icmp++
//fmt.Println("ICMP layer detected.")
// icmp, _ := icmpLayer.(*layers.ICMPv4)

}
total_packs++
// Let's see if the packet is TCP
tcpLayer := packet.Layer(layers.LayerTypeTCP)
if tcpLayer != nil {
total_tcp++
// fmt.Println("TCP layer detected.")
tcp, _ := tcpLayer.(*layers.TCP)
if tcp.SYN {
total_tcp_SYN++
} else {

if tcp.ACK {
total_tcp_ACK++
} else {

total_tcp_data++

}
}
// // TCP layer variables:
// // SrcPort, DstPort, Seq, Ack, DataOffset, Window, Checksum, Urgent
// // Bool flags: FIN, SYN, RST, PSH, ACK, URG, ECE, CWR, NS
// fmt.Printf("ICMP type: ", icmp.TypeCode.Code())
// fmt.Printf("From port %d to %d\n", tcp.SrcPort, tcp.DstPort)
// fmt.Println("Sequence number: ", tcp.Seq)
// fmt.Println()
} else {

fmt.Println()
udpLayer := packet.Layer(layers.LayerTypeUDP)
if udpLayer != nil {
// fmt.Println("UDP layer detected.")
// udp, _ := udpLayer.(*layers.UDP)

// // TCP layer variables:
// // SrcPort, DstPort, Seq, Ack, DataOffset, Window, Checksum, Urgent
// // Bool flags: FIN, SYN, RST, PSH, ACK, URG, ECE, CWR, NS
// fmt.Printf("From port %d to %d\n", udp.SrcPort, udp.DstPort)
// fmt.Println("Content number: ", udp.Contents)
// fmt.Println()
total_udp++
} else {

icmpLayer := packet.Layer(layers.LayerTypeICMPv4)
if icmpLayer != nil {
total_icmp++
//fmt.Println("ICMP layer detected.")
// icmp, _ := icmpLayer.(*layers.ICMPv4)

// // TCP layer variables:
// // SrcPort, DstPort, Seq, Ack, DataOffset, Window, Checksum, Urgent
// // Bool flags: FIN, SYN, RST, PSH, ACK, URG, ECE, CWR, NS
// fmt.Printf("ICMP type: ", icmp.TypeCode.Code())

fmt.Println()
}

}
}
// Iterate over all layers, printing out each layer type
// fmt.Println("All packet layers:")
// for _, layer := range packet.Layers() {
// fmt.Println("- ", layer.LayerType())
// }

// // When iterating through packet.Layers() above,
// // if it lists Payload layer then that is the same as
// // this applicationLayer. applicationLayer contains the payload
// applicationLayer := packet.ApplicationLayer()
// if applicationLayer != nil {
// fmt.Println("Application layer/Payload found.")
// fmt.Printf("%s\n", applicationLayer.Payload())

// // Search for a string inside the payload
// if strings.Contains(string(applicationLayer.Payload()), "HTTP") {
// fmt.Println("HTTP found!")
// }
// }

// Check for errors
if err := packet.ErrorLayer(); err != nil {
fmt.Println("Error decoding some part of the packet:", err)
}

}
}
// Iterate over all layers, printing out each layer type
// fmt.Println("All packet layers:")
// for _, layer := range packet.Layers() {
// fmt.Println("- ", layer.LayerType())
// }

// // When iterating through packet.Layers() above,
// // if it lists Payload layer then that is the same as
// // this applicationLayer. applicationLayer contains the payload
// applicationLayer := packet.ApplicationLayer()
// if applicationLayer != nil {
// fmt.Println("Application layer/Payload found.")
// fmt.Printf("%s\n", applicationLayer.Payload())

// // Search for a string inside the payload
// if strings.Contains(string(applicationLayer.Payload()), "HTTP") {
// fmt.Println("HTTP found!")
// }
// }

// Check for errors
if err := packet.ErrorLayer(); err != nil {
fmt.Println("Error decoding some part of the packet:", err)
}
}

func main() {
//done_icmp := make(chan bool)

v := &Report{ID: 100, TCPackets: total_tcp, UDPackets: total_udp, ICMPackets: total_icmp}

filename := "Net-Report"
file, _ := os.Create(filename)

xmlWriter := io.Writer(file)

enc := xml.NewEncoder(xmlWriter)
enc.Indent(" ", " ")
if err5 := enc.Encode(v); err5 != nil {
fmt.Println(err5)
}

handle, err = pcap.OpenLive(device, snapshot_len, promiscuous, timeout)
if err != nil {
log.Fatal(err)
Expand All @@ -186,7 +181,7 @@ func main() {
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
// Process packet here
go printPacketInfo(packet)
printPacketInfo(packet)

if total_packs > 600 {
break
Expand All @@ -205,6 +200,19 @@ func main() {
fmt.Println(" ----------------------------------- ")
fmt.Println(ip_dst_detected)

v := &Report{ID: 100, TCPackets: total_tcp, UDPackets: total_udp, ICMPackets: total_icmp}

filename := "Net-Report"
file, _ := os.Create(filename)

xmlWriter := io.Writer(file)

enc := xml.NewEncoder(xmlWriter)
enc.Indent(" ", " ")
if err5 := enc.Encode(v); err5 != nil {
fmt.Println(err5)
}

//<-done_icmp
//<-done_tcp
}

0 comments on commit c54681d

Please sign in to comment.