-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheap.go
120 lines (110 loc) · 3.43 KB
/
eap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"crypto/md5"
"fmt"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"net"
"os"
"time"
)
/* 发送EAPOL包 */
func sendEAPOL(Version byte, Type layers.EAPOLType, SrcMAC net.HardwareAddr, DstMAC net.HardwareAddr) {
buffer := gopacket.NewSerializeBuffer()
options := gopacket.SerializeOptions{}
gopacket.SerializeLayers(buffer, options,
&layers.Ethernet{EthernetType: layers.EthernetTypeEAPOL, SrcMAC: SrcMAC, DstMAC: DstMAC},
&myEAPOL{&layers.EAPOL{Version: 0x01, Type: Type}, 0},
)
//var err error
err := handle.WritePacketData(buffer.Bytes())
if err != nil {
fmt.Print(err)
os.Exit(0)
}
}
/* 发送EAP包 */
func sendEAP(Id uint8, Type layers.EAPType, TypeData []byte, Code layers.EAPCode, SrcMAC net.HardwareAddr, DstMAC net.HardwareAddr) {
buffer := gopacket.NewSerializeBuffer()
options := gopacket.SerializeOptions{}
gopacket.SerializeLayers(buffer, options,
&layers.Ethernet{EthernetType: layers.EthernetTypeEAPOL, SrcMAC: SrcMAC, DstMAC: DstMAC},
&myEAPOL{&layers.EAPOL{Version: 0x01, Type: layers.EAPOLTypeEAP}, uint16(len(TypeData)) + 5},
&layers.EAP{Id: Id, Type: Type, TypeData: TypeData, Code: Code, Length: uint16(len(TypeData) + 5)},
)
// err error
err := handle.WritePacketData(buffer.Bytes())
if err != nil {
fmt.Print(err)
os.Exit(0)
}
}
/* 读取新数据包 */
func readNewPacket(packetSrc *gopacket.PacketSource) {
for packet := range packetSrc.Packets() {
eapl := packet.Layer(layers.LayerTypeEAP)
if eapl != nil {
switch eapl.(*layers.EAP).Code {
case 0x03: //Success
fmt.Println("Success")
sendPingStart()
case 0x01: //Request
switch int8(eapl.(*layers.EAP).Type) {
case 0x04: //EAP-MD5-CHALLENGE
//fmt.Println(eapl.(*layers.EAP).TypeData)
go responseMd5Challenge(eapl.(*layers.EAP).TypeData[1:17])
case 0x02: //Notification
fmt.Println("Failed")
os.Exit(0)
case 0x01: //Identity
//fmt.Print("TRUE")
go responseIndentity(eapl.(*layers.EAP).Id)
}
case 0x04: //Failure
fmt.Println("Failed")
fmt.Println("Retry...")
time.Sleep(5*time.Second)
EAPAuth()
}
}
}
end <- true
}
/* EAP认证开始 */
func EAPAuth() {
fmt.Println(mac)
fmt.Println("EAP Start...")
sendEAPOL(0x01, layers.EAPOLTypeStart, mac, boardCastAddr)
}
/* EAP注销 */
func EAPLogoff() {
sendEAPOL(0x01, layers.EAPOLTypeLogOff, mac, boardCastAddr)
fmt.Println("Logoff...")
}
/* 回应身份(Indentity) */
func responseIndentity(id byte) {
dataPack := []byte{}
dataPack = append(dataPack, []byte(username)...) //用户名
dataPack = append(dataPack, []byte{0x00, 0x44, 0x61, 0x00, 0x00}...) //未知
dataPack = append(dataPack, clientip[:]...) //客户端IP
fmt.Println("Response Identity...")
sendEAP(id, 0x01, dataPack, 2, mac, boardCastAddr)
}
/* 回应MD5-Challenge */
func responseMd5Challenge(m []byte) {
mPack := []byte{}
mPack = append(mPack, 0)
mPack = append(mPack, []byte(password)...)
mPack = append(mPack, m...)
mCal := md5.New()
mCal.Write(mPack)
dataPack := []byte{}
dataPack = append(dataPack, 16)
dataPack = append(dataPack, mCal.Sum(nil)...)
dataPack = append(dataPack, []byte(username)...)
dataPack = append(dataPack, []byte{0x00, 0x44, 0x61, 0x26, 0x00}...)
dataPack = append(dataPack, []byte(clientip[:])...)
challenge = mCal.Sum(nil) //用于后面心跳包
fmt.Println("Response EAP-MD5-Challenge...")
sendEAP(0, 0x04, dataPack, 2, mac, boardCastAddr)
}