Skip to content

Commit

Permalink
new: session scripts can now include other scripts via require('file')
Browse files Browse the repository at this point in the history
  • Loading branch information
evilsocket committed Apr 4, 2021
1 parent 2b1ff7d commit 8827a2a
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
*.sw*
*.tar.gz
*.prof*
betterbot.js
_example/config.js
pcaps
build
bettercap
Expand Down
3 changes: 3 additions & 0 deletions _example/config.example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var wifiInterface = 'put the wifi interface to put in monitor mode here';
var telegramToken = 'put your telegram bot token here';
var telegramChatId = 'put your telegram chat id here';
16 changes: 2 additions & 14 deletions example.js → _example/example.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
var wifiInterface = 'put the wifi interface to put in monitor mode here';
var telegramToken = 'put your telegram bot token here';
var telegramChatId = 'put your telegram chat id here';

function sendMessage(message) {
var url = 'https://api.telegram.org/bot' + telegramToken +
'/sendMessage?chat_id=' + telegramChatId +
'&text=' + http.Encode(message);

var resp = http.Get(url, {});
if( resp.Error ) {
log("error while running sending telegram message: " + resp.Error.Error());
}
}
require("config.js")
require("telegram.js")

function onDeauthentication(event) {
var data = event.Data;
Expand Down
10 changes: 10 additions & 0 deletions _example/telegram.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function sendMessage(message) {
var url = 'https://api.telegram.org/bot' + telegramToken +
'/sendMessage?chat_id=' + telegramChatId +
'&text=' + http.Encode(message);

var resp = http.Get(url, {});
if( resp.Error ) {
log("error while running sending telegram message: " + resp.Error.Error());
}
}
81 changes: 77 additions & 4 deletions session/script.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,93 @@
package session

import (
"fmt"
"github.com/bettercap/bettercap/caplets"
_ "github.com/bettercap/bettercap/js"
"github.com/evilsocket/islazy/fs"
"github.com/evilsocket/islazy/plugin"
"github.com/evilsocket/islazy/str"
"io/ioutil"
"path/filepath"
"regexp"
"strings"
)

// require("telegram.js")
var requireParser = regexp.MustCompile(`(?msi)^\s*require\s*\(\s*["']([^"']+)["']\s*\);?\s*$`)

type Script struct {
*plugin.Plugin
}

func LoadScript(fileName string, ses *Session) (*Script, error) {
if p, err := plugin.Load(fileName); err != nil {
// yo! we're doing c-like preprocessing on a javascript file from go :D
func preprocess(basePath string, code string, level int) (string, error) {
if level >= 255 {
return "", fmt.Errorf("too many nested includes")
}

matches := requireParser.FindAllStringSubmatch(code, -1)
for _, match := range matches {
expr := match[0]
fileName := str.Trim(match[1])

if fileName[0] != '/' {
searchPaths := []string{
filepath.Join(basePath, fileName),
filepath.Join(caplets.InstallBase, fileName),
}

if strings.Contains(fileName, ".js") == false {
searchPaths = append(searchPaths, []string{
filepath.Join(basePath, fileName) + ".js",
filepath.Join(caplets.InstallBase, fileName) + ".js",
}...)
}

found := false
for _, fName := range searchPaths {
if fs.Exists(fName) {
fileName = fName
found = true
break
}
}
if !found {
return "", fmt.Errorf("file %s not found in any of %#v", fileName, searchPaths)
}
}

raw, err := ioutil.ReadFile(fileName)
if err != nil {
return "", fmt.Errorf("%s: %v", fileName, err)
}

if includedBody, err := preprocess(filepath.Dir(fileName), string(raw), level+1); err != nil {
return "", fmt.Errorf("%s: %v", fileName, err)
} else {
code = strings.ReplaceAll(code, expr, includedBody)
}
}

return code, nil
}

func LoadScript(fileName string) (*Script, error) {
raw, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}

basePath := filepath.Dir(fileName)
if code, err := preprocess(basePath, string(raw), 0); err != nil {
return nil, err
} else if p, err := plugin.Parse(code); err != nil {
return nil, err
} else {
p.Path = fileName
p.Name = strings.Replace(basePath, ".js", "", -1)
return &Script{
Plugin: p,
Plugin: p,
}, nil
}
}
}
2 changes: 1 addition & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func (s *Session) Start() error {

// load the script here so the session and its internal objects are ready
if *s.Options.Script != "" {
if s.script, err = LoadScript(*s.Options.Script, s); err != nil {
if s.script, err = LoadScript(*s.Options.Script); err != nil {
return fmt.Errorf("error loading %s: %v", *s.Options.Script, err)
}
log.Debug("session script %s loaded", *s.Options.Script)
Expand Down

0 comments on commit 8827a2a

Please sign in to comment.