-
Notifications
You must be signed in to change notification settings - Fork 5
/
LogicBomb.go
90 lines (74 loc) · 2.07 KB
/
LogicBomb.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
package puffgo
/*
// Author: Aliasgar Khimani (NovusEdge)
// Project: github.com/ARaChn3/puffgo
//
// Copyright: GNU LGPLv3
// See the LICENSE file for more info.
*/
import (
"crypto/rand"
"encoding/hex"
"sync"
)
//LogicBomb: type implementing a logic-bomb for UNIX/Linux based systems.
type LogicBomb struct {
// Listener represents an event-listner defined in this library.
Listener *EventListener
// ExecutionFunction specifies the function
// which will be executed when the bomb is triggered and it goes off
ExecutionFunction func()
// BombId specifies a random hex string used to identify the bomb.
BombID string
// PID specifies the process-id of the persistent logic-bomb program running.
// It holds nil until the mainloop of the event-listner is started.
PID *int
}
// NewBomb returns an instance of LogicBomb, which can be
// implanted by Implant(), to be triggered when conditions
// for it to do so are met.
func NewBomb(listener EventListener, execFunc func()) *LogicBomb {
var lb LogicBomb
lb.Listener = &listener
lb.BombID, _ = randomHex(10)
lb.ExecutionFunction = execFunc
lb.PID = lb.Listener.PID
return &lb
}
// Arm() allows the activation of the bomb. If a bomb is not armed,
// it won't be triggered even if the event defined in Listener occurs.
func (lb *LogicBomb) Arm() {
var wg sync.WaitGroup
// Run listner's mainloop
go func() {
defer wg.Done()
lb.Listener.Mainloop()
}()
// Check for trigger...
go func() {
defer wg.Done()
for {
if isTriggered := <-lb.Listener.TriggerChannel; isTriggered {
lb.ExecutionFunction()
lb.Listener.Terminate()
break
}
}
}()
wg.Add(2)
wg.Wait()
}
// Disarm() allows the deactivation of the bomb. It passes a true into
// the TerminationChannel of Listener, thereby terminating the listener's
// mainloop.
func (lb *LogicBomb) Disarm() {
lb.Listener.Terminate()
}
// Used for generating the BombID on creation of LogicBombs
func randomHex(n int) (string, error) {
bytes := make([]byte, n)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}