new: new c2 module, first draft

This commit is contained in:
Simone Margaritelli 2021-01-27 00:17:25 +01:00
commit 583a54c194
23 changed files with 579 additions and 163 deletions

View file

@ -37,6 +37,19 @@ func (e Event) Label() string {
return color + label + tui.RESET
}
type EventBus <-chan Event
type PrintCallback func(format string, args ...interface{})
type PrintWriter struct {
pool *EventPool
}
func (w PrintWriter) Write(p []byte) (n int, err error) {
w.pool.Printf("%s", string(p))
return len(p), nil
}
type EventPool struct {
*sync.Mutex
@ -44,19 +57,35 @@ type EventPool struct {
silent bool
events []Event
listeners []chan Event
printLock sync.Mutex
printCbs []PrintCallback
Stdout PrintWriter
}
func NewEventPool(debug bool, silent bool) *EventPool {
return &EventPool{
pool := &EventPool{
Mutex: &sync.Mutex{},
debug: debug,
silent: silent,
events: make([]Event, 0),
listeners: make([]chan Event, 0),
printCbs: make([]PrintCallback, 0),
}
pool.Stdout = PrintWriter{
pool: pool,
}
return pool
}
func (p *EventPool) Listen() <-chan Event {
func (p *EventPool) OnPrint(cb PrintCallback) {
p.printLock.Lock()
defer p.printLock.Unlock()
p.printCbs = append(p.printCbs, cb)
}
func (p *EventPool) Listen() EventBus {
p.Lock()
defer p.Unlock()
l := make(chan Event)
@ -76,7 +105,7 @@ func (p *EventPool) Listen() <-chan Event {
return l
}
func (p *EventPool) Unlisten(listener <-chan Event) {
func (p *EventPool) Unlisten(listener EventBus) {
p.Lock()
defer p.Unlock()
@ -123,6 +152,16 @@ func (p *EventPool) Add(tag string, data interface{}) {
}
}
func (p *EventPool) Printf(format string, a ...interface{}) {
p.printLock.Lock()
defer p.printLock.Unlock()
for _, cb := range p.printCbs {
cb(format, a...)
}
fmt.Printf(format, a...)
}
func (p *EventPool) Log(level log.Verbosity, format string, args ...interface{}) {
if level == log.DEBUG && !p.debug {
return

View file

@ -132,6 +132,10 @@ func (m *SessionModule) Fatal(format string, args ...interface{}) {
m.Session.Events.Log(log.FATAL, m.tag+format, args...)
}
func (m *SessionModule) Printf(format string, a ...interface{}) {
m.Session.Events.Printf(format, a...)
}
func (m *SessionModule) Requires(modName string) {
m.requires = append(m.requires, modName)
}

View file

@ -33,10 +33,10 @@ func (s *Session) generalHelp() {
pad := "%" + strconv.Itoa(maxLen) + "s"
for _, h := range s.CoreHandlers {
fmt.Printf(" "+tui.Yellow(pad)+" : %s\n", h.Name, h.Description)
s.Events.Printf(" "+tui.Yellow(pad)+" : %s\n", h.Name, h.Description)
}
fmt.Println(tui.Bold("\nModules\n"))
s.Events.Printf("%s\n", tui.Bold("\nModules\n"))
maxLen = 0
for _, m := range s.Modules {
@ -54,7 +54,7 @@ func (s *Session) generalHelp() {
} else {
status = tui.Red("not running")
}
fmt.Printf(" "+tui.Yellow(pad)+" > %s\n", m.Name(), status)
s.Events.Printf(" "+tui.Yellow(pad)+" > %s\n", m.Name(), status)
}
fmt.Println()
@ -73,7 +73,7 @@ func (s *Session) moduleHelp(filter string) error {
} else {
status = tui.Red("not running")
}
fmt.Printf("%s (%s): %s\n\n", tui.Yellow(m.Name()), status, tui.Dim(m.Description()))
s.Events.Printf("%s (%s): %s\n\n", tui.Yellow(m.Name()), status, tui.Dim(m.Description()))
maxLen := 0
handlers := m.Handlers()
@ -85,7 +85,7 @@ func (s *Session) moduleHelp(filter string) error {
}
for _, h := range handlers {
fmt.Print(h.Help(maxLen))
s.Events.Printf("%s", h.Help(maxLen))
}
fmt.Println()
@ -105,9 +105,9 @@ func (s *Session) moduleHelp(filter string) error {
return v[i].Name < v[j].Name
})
fmt.Print(" Parameters\n\n")
s.Events.Printf(" Parameters\n\n")
for _, p := range v {
fmt.Print(p.Help(maxLen))
s.Events.Printf("%s", p.Help(maxLen))
}
fmt.Println()
}
@ -138,13 +138,13 @@ func (s *Session) activeHandler(args []string, sess *Session) error {
continue
}
fmt.Printf("%s (%s)\n", tui.Bold(m.Name()), tui.Dim(m.Description()))
s.Events.Printf("%s (%s)\n", tui.Bold(m.Name()), tui.Dim(m.Description()))
params := m.Parameters()
if len(params) > 0 {
fmt.Println()
for _, p := range params {
_, val := s.Env.Get(p.Name)
fmt.Printf(" %s : %s\n", tui.Yellow(p.Name), val)
s.Events.Printf(" %s : %s\n", tui.Yellow(p.Name), val)
}
}
@ -206,13 +206,13 @@ func (s *Session) getHandler(args []string, sess *Session) error {
prev_ns = ns
}
fmt.Printf(" %"+strconv.Itoa(padding)+"s: '%s'\n", k, s.Env.Data[k])
s.Events.Printf(" %"+strconv.Itoa(padding)+"s: '%s'\n", k, s.Env.Data[k])
}
}
fmt.Println()
} else if found, value := s.Env.Get(key); found {
fmt.Println()
fmt.Printf(" %s: '%s'\n", key, value)
s.Events.Printf(" %s: '%s'\n", key, value)
fmt.Println()
} else {
return fmt.Errorf("%s not found", key)
@ -269,7 +269,7 @@ func (s *Session) includeHandler(args []string, sess *Session) error {
func (s *Session) shHandler(args []string, sess *Session) error {
out, err := core.Shell(args[0])
if err == nil {
fmt.Printf("%s\n", out)
s.Events.Printf("%s\n", out)
}
return err
}