-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
554 lines (469 loc) · 14.3 KB
/
main.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
// IOTSIM is a simple IoT authentication simulator that demonstrates the BasIoT protocol.
// The simulator creates a blockchain system with devices and resources, and simulates different scenarios for authentication.
// The scenarios include legitimate authentication, hacker attempts, expired requests, and replay attacks.
// Made for seminar by exprays a.k.a surya
package main
import (
"bufio"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"log"
rn "math/rand"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/gorilla/websocket"
)
const (
ASCII_BANNER = `
[1;36m╔══════════════════════════════════════════╗
║ IOTSIM ║
║ Secure IoT Auth Demo ║
║ ║
║ made by exprays ║
╚══════════════════════════════════════════╝[0m
`
ColorReset = "\033[0m"
ColorRed = "\033[31m"
ColorGreen = "\033[32m"
ColorYellow = "\033[33m"
ColorBlue = "\033[34m"
ColorPurple = "\033[35m"
ColorCyan = "\033[36m"
)
// Device represents an IoT device
type Device struct {
ID string `json:"device_identifier"`
Descriptor string `json:"device_descriptor"`
Address string `json:"d_addr"`
PrivateKey *rsa.PrivateKey `json:"-"`
PublicKey *rsa.PublicKey `json:"-"`
}
// ResourceHolder represents a resource provider in the IoT network
type ResourceHolder struct {
ID string `json:"resource_id"`
Type string `json:"resource_type"`
PrivateKey *rsa.PrivateKey `json:"-"`
PublicKey *rsa.PublicKey `json:"-"`
}
// AuthRequest represents an authentication request
type AuthRequest struct {
DeviceAddr string `json:"device_addr"`
ResourceID string `json:"resource_id"`
Timestamp time.Time `json:"timestamp"`
Nonce string `json:"nonce"`
Signature string `json:"signature"`
}
// SimulationType represents different simulation scenarios
type SimulationType int
const (
LegitimateAuth SimulationType = iota
HackerAttempt
ExpiredRequest
ReplayAttack
)
func (st SimulationType) String() string {
return [...]string{
"Legitimate Authentication",
"Hacker Attempt",
"Expired Request",
"Replay Attack",
}[st]
}
// BlockchainSimulator simulates a basic blockchain for device registry
type BlockchainSimulator struct {
Devices map[string]*Device
Resources map[string]*ResourceHolder
}
// NewBlockchainSimulator creates a new blockchain simulator
func NewBlockchainSimulator() *BlockchainSimulator {
return &BlockchainSimulator{
Devices: make(map[string]*Device),
Resources: make(map[string]*ResourceHolder),
}
}
func (bc *BlockchainSimulator) RegisterDevice(device *Device) error {
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return fmt.Errorf("failed to generate RSA key: %v", err)
}
device.PrivateKey = privateKey
device.PublicKey = &privateKey.PublicKey
addr := make([]byte, 32)
rand.Read(addr)
device.Address = base64.StdEncoding.EncodeToString(addr)
bc.Devices[device.ID] = device
return nil
}
func (bc *BlockchainSimulator) RegisterResource(resource *ResourceHolder) error {
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return fmt.Errorf("failed to generate RSA key: %v", err)
}
resource.PrivateKey = privateKey
resource.PublicKey = &privateKey.PublicKey
bc.Resources[resource.ID] = resource
return nil
}
// AuthenticationProtocol implements the BasIoT authentication process
type AuthenticationProtocol struct {
blockchain *BlockchainSimulator
usedNonces map[string]bool
replayWindow time.Duration
nonceCleanup time.Time
}
func NewAuthenticationProtocol(bc *BlockchainSimulator) *AuthenticationProtocol {
return &AuthenticationProtocol{
blockchain: bc,
usedNonces: make(map[string]bool),
replayWindow: 5 * time.Minute,
nonceCleanup: time.Now(),
}
}
func (ap *AuthenticationProtocol) recordNonce(nonce string) {
ap.usedNonces[nonce] = true
}
func (ap *AuthenticationProtocol) isNonceUsed(nonce string) bool {
if time.Since(ap.nonceCleanup) > time.Hour {
ap.cleanupNonces()
}
return ap.usedNonces[nonce]
}
func (ap *AuthenticationProtocol) cleanupNonces() {
ap.usedNonces = make(map[string]bool)
ap.nonceCleanup = time.Now()
}
func (ap *AuthenticationProtocol) RequestAuthentication(deviceID, resourceID string) (*AuthRequest, error) {
device, exists := ap.blockchain.Devices[deviceID]
if !exists {
return nil, fmt.Errorf("device not found: %s", deviceID)
}
if _, exists := ap.blockchain.Resources[resourceID]; !exists {
return nil, fmt.Errorf("resource not found: %s", resourceID)
}
nonce := make([]byte, 32)
rand.Read(nonce)
nonceStr := base64.StdEncoding.EncodeToString(nonce)
request := &AuthRequest{
DeviceAddr: device.Address,
ResourceID: resourceID,
Timestamp: time.Now(),
Nonce: nonceStr,
}
message := fmt.Sprintf("%s%s%s%s", request.DeviceAddr, request.ResourceID,
request.Timestamp.Format(time.RFC3339), request.Nonce)
hashed := sha256.Sum256([]byte(message))
signature, err := rsa.SignPKCS1v15(rand.Reader, device.PrivateKey, crypto.SHA256, hashed[:])
if err != nil {
return nil, fmt.Errorf("failed to sign request: %v", err)
}
request.Signature = base64.StdEncoding.EncodeToString(signature)
return request, nil
}
func (ap *AuthenticationProtocol) SimulateHackerAttempt(request *AuthRequest) *AuthRequest {
hackedRequest := *request
switch rn.Intn(3) {
case 0:
hackedRequest.DeviceAddr = "hacked_" + hackedRequest.DeviceAddr
case 1:
hackedRequest.Timestamp = hackedRequest.Timestamp.Add(time.Hour)
case 2:
sig := []byte(hackedRequest.Signature)
sig[len(sig)-1] ^= 1
hackedRequest.Signature = string(sig)
}
return &hackedRequest
}
func (ap *AuthenticationProtocol) SimulateScenario(request *AuthRequest, simType SimulationType) *AuthRequest {
switch simType {
case HackerAttempt:
return ap.SimulateHackerAttempt(request)
case ExpiredRequest:
modifiedRequest := *request
modifiedRequest.Timestamp = time.Now().Add(-10 * time.Minute)
return &modifiedRequest
case ReplayAttack:
ap.recordNonce(request.Nonce)
return request
default:
return request
}
}
func (ap *AuthenticationProtocol) VerifyAuthentication(request *AuthRequest) (bool, error) {
if time.Since(request.Timestamp) > 5*time.Minute {
return false, fmt.Errorf("request expired: timestamp too old")
}
if ap.isNonceUsed(request.Nonce) {
return false, fmt.Errorf("nonce already used: possible replay attack")
}
var device *Device
for _, d := range ap.blockchain.Devices {
if d.Address == request.DeviceAddr {
device = d
break
}
}
if device == nil {
return false, fmt.Errorf("device not found with address: %s", request.DeviceAddr)
}
message := fmt.Sprintf("%s%s%s%s", request.DeviceAddr, request.ResourceID,
request.Timestamp.Format(time.RFC3339), request.Nonce)
hashed := sha256.Sum256([]byte(message))
signature, err := base64.StdEncoding.DecodeString(request.Signature)
if err != nil {
return false, fmt.Errorf("failed to decode signature: %v", err)
}
err = rsa.VerifyPKCS1v15(device.PublicKey, crypto.SHA256, hashed[:], signature)
if err != nil {
return false, nil
}
ap.recordNonce(request.Nonce)
return true, nil
}
type MonitorData struct {
Devices []*Device `json:"devices"`
AuthMetrics AuthMetrics `json:"authMetrics"`
Events []SecurityEvent `json:"events"`
}
type AuthMetrics struct {
LegitimateAuth int `json:"legitimateAuth"`
HackerAttempts int `json:"hackerAttempts"`
ExpiredRequests int `json:"expiredRequests"`
ReplayAttacks int `json:"replayAttacks"`
}
type SecurityEvent struct {
Timestamp time.Time `json:"timestamp"`
Type SimulationType `json:"type"`
DeviceID string `json:"deviceId"`
Success bool `json:"success"`
}
type Monitor struct {
blockchain *BlockchainSimulator
auth *AuthenticationProtocol
clients map[*websocket.Conn]bool
broadcast chan MonitorData
mutex sync.Mutex
metrics AuthMetrics
events []SecurityEvent
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true // Allow all origins for demo
},
}
func NewMonitor(bc *BlockchainSimulator, auth *AuthenticationProtocol) *Monitor {
return &Monitor{
blockchain: bc,
auth: auth,
clients: make(map[*websocket.Conn]bool),
broadcast: make(chan MonitorData),
events: make([]SecurityEvent, 0),
}
}
func (m *Monitor) StartServer() {
// Start HTTP server for WebSocket connections
http.HandleFunc("/ws", m.handleConnections)
// Start broadcasting goroutine
go m.handleBroadcasts()
// Start the server in a goroutine
go func() {
log.Println("Starting WebSocket server on :8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}()
}
func (m *Monitor) handleConnections(w http.ResponseWriter, r *http.Request) {
// Upgrade initial GET request to a WebSocket
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("Error upgrading connection: %v", err)
return
}
defer ws.Close()
// Register new client
m.mutex.Lock()
m.clients[ws] = true
m.mutex.Unlock()
// Send initial data
initialData := m.getMonitorData()
if err := ws.WriteJSON(initialData); err != nil {
log.Printf("Error sending initial data: %v", err)
return
}
// Keep connection alive and handle disconnection
for {
if _, _, err := ws.ReadMessage(); err != nil {
m.mutex.Lock()
delete(m.clients, ws)
m.mutex.Unlock()
break
}
}
}
func (m *Monitor) handleBroadcasts() {
for data := range m.broadcast {
m.mutex.Lock()
for client := range m.clients {
if err := client.WriteJSON(data); err != nil {
log.Printf("Error broadcasting to client: %v", err)
client.Close()
delete(m.clients, client)
}
}
m.mutex.Unlock()
}
}
func (m *Monitor) RecordAuthEvent(simType SimulationType, deviceID string, success bool) {
m.mutex.Lock()
defer m.mutex.Unlock()
// Record event
event := SecurityEvent{
Timestamp: time.Now(),
Type: simType,
DeviceID: deviceID,
Success: success,
}
m.events = append(m.events, event)
// Update metrics
switch simType {
case LegitimateAuth:
m.metrics.LegitimateAuth++
case HackerAttempt:
m.metrics.HackerAttempts++
case ExpiredRequest:
m.metrics.ExpiredRequests++
case ReplayAttack:
m.metrics.ReplayAttacks++
}
// Broadcast updated data
m.broadcast <- m.getMonitorData()
}
func (m *Monitor) getMonitorData() MonitorData {
devices := make([]*Device, 0)
for _, device := range m.blockchain.Devices {
devices = append(devices, device)
}
return MonitorData{
Devices: devices,
AuthMetrics: m.metrics,
Events: m.events,
}
}
func printColored(color, message string) {
fmt.Printf("%s%s%s\n", color, message, ColorReset)
}
func getUserInput(prompt string) string {
printColored(ColorYellow, prompt)
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
return strings.TrimSpace(input)
}
func printResult(success bool, message string) {
if success {
printColored(ColorGreen, "✓ SUCCESS: "+message)
} else {
printColored(ColorRed, "✗ FAILURE: "+message)
}
}
func printDivider() {
printColored(ColorCyan, strings.Repeat("=", 50))
}
func printHeader(title string) {
printDivider()
printColored(ColorPurple, title)
printDivider()
}
func colorBool(b bool) string {
if b {
return ColorGreen + "✓" + ColorReset
}
return ColorRed + "✗" + ColorReset
}
func main() {
fmt.Println(ASCII_BANNER)
blockchain := NewBlockchainSimulator()
auth := NewAuthenticationProtocol(blockchain)
// Initialize and start the monitor
monitor := NewMonitor(blockchain, auth)
monitor.StartServer()
device := &Device{
ID: "device001",
Descriptor: "Temperature Sensor",
}
resource := &ResourceHolder{
ID: "resource001",
Type: "Data Storage",
}
if err := blockchain.RegisterDevice(device); err != nil {
log.Fatalf("Failed to register device: %v", err)
}
if err := blockchain.RegisterResource(resource); err != nil {
log.Fatalf("Failed to register resource: %v", err)
}
printColored(ColorGreen, "Blockchain System Initialized Successfully!")
printColored(ColorGreen, "Device and Resource Registered!")
for {
printColored(ColorYellow, "\nAvailable Simulation Scenarios:")
printColored(ColorCyan, "1. Legitimate Authentication")
printColored(ColorCyan, "2. Hacker Attempt")
printColored(ColorCyan, "3. Expired Request")
printColored(ColorCyan, "4. Replay Attack")
printColored(ColorCyan, "5. Exit")
choice := getUserInput("\nSelect scenario (1-5): ")
if choice == "5" {
printColored(ColorGreen, "Exiting simulator. Goodbye!")
break
}
var simType SimulationType
switch choice {
case "1":
simType = LegitimateAuth
case "2":
simType = HackerAttempt
case "3":
simType = ExpiredRequest
case "4":
simType = ReplayAttack
default:
printColored(ColorRed, "Invalid choice. Please try again.")
continue
}
printHeader(fmt.Sprintf("Simulating: %s", simType))
request, err := auth.RequestAuthentication(device.ID, resource.ID)
if err != nil {
printColored(ColorRed, fmt.Sprintf("Error creating request: %v", err))
continue
}
simulatedRequest := auth.SimulateScenario(request, simType)
requestJSON, _ := json.MarshalIndent(simulatedRequest, "", " ")
printColored(ColorBlue, "Authentication Request:")
fmt.Println(string(requestJSON))
verified, err := auth.VerifyAuthentication(simulatedRequest)
monitor.RecordAuthEvent(simType, device.ID, verified)
if verified {
printResult(true, "Authentication successful")
} else {
if err != nil {
printResult(false, fmt.Sprintf("Authentication failed: %v", err))
} else {
printResult(false, "Authentication failed: Invalid signature")
}
}
printHeader("Security Analysis")
printColored(ColorCyan, "Checks Performed:")
fmt.Printf("• Signature Verification: %s\n", colorBool(verified))
fmt.Printf("• Timestamp Valid: %s\n", colorBool(time.Since(simulatedRequest.Timestamp) <= 5*time.Minute))
fmt.Printf("• Nonce Unique: %s\n", colorBool(!auth.isNonceUsed(simulatedRequest.Nonce)))
fmt.Printf("• Device Verified: %s\n", colorBool(device.Address == simulatedRequest.DeviceAddr))
}
}