Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ne0nd0g committed Jul 30, 2018
2 parents 75a38cf + 9fecabe commit 61552e0
Show file tree
Hide file tree
Showing 258 changed files with 49,302 additions and 571 deletions.
37 changes: 21 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# !!!MAKE SURE YOUR GOPATH ENVIRONMENT VARIABLE IS SET FIRST!!!

# Merlin Server & Agent version number
VERSION=0.5.0
VERSION=0.6.0

MSERVER=merlinServer
MAGENT=merlinAgent
PASSWORD=merlin
BUILD=$(shell git rev-parse HEAD)
DIR=data/temp/v${VERSION}/
DIR=data/temp/v${VERSION}/${BUILD}
BIN=data/bin/
LDFLAGS=-ldflags "-s -X main.build=${BUILD}"
WINAGENTLDFLAGS=-ldflags "-s -X main.build=${BUILD} -H=windowsgui"
PACKAGE=7za a -p${PASSWORD} -mhe -mx=9
F=README.MD LICENSE data/modules docs data/README.MD data/agents/README.MD data/db/ data/log/README.MD data/x509 data/src data/bin
F=README.MD LICENSE data/modules docs data/README.MD data/agents/README.MD data/db/ data/log/README.MD data/x509 data/src data/bin data/html
F2=LICENSE
W=Windows-x64
L=Linux-x64
Expand All @@ -24,7 +24,7 @@ D=Darwin-x64
$(shell mkdir -p ${DIR})

# Change default to just make for the host OS and add MAKE ALL to do this
default: server-windows agent-windows server-linux agent-linux server-darwin agent-darwin agent-dll
default: server-windows agent-windows server-linux agent-linux server-darwin agent-darwin agent-dll agent-javascript

all: default

Expand Down Expand Up @@ -56,7 +56,7 @@ agent-dll:
export GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ CGO_ENABLED=1; \
go build -buildmode=c-archive -o ${DIR}/main.a cmd/merlinagentdll/main.go; \
cp data/bin/dll/merlin.c ${DIR}; \
x86_64-w64-mingw32-gcc -shared -pthread -o ${DIR}merlin.dll ${DIR}merlin.c ${DIR}main.a -lwinmm -lntdll -lws2_32
x86_64-w64-mingw32-gcc -shared -pthread -o ${DIR}/merlin.dll ${DIR}/merlin.c ${DIR}/main.a -lwinmm -lntdll -lws2_32

# Compile Server - Linux x64
server-linux:
Expand All @@ -82,38 +82,43 @@ server-darwin:
agent-darwin:
export GOOS=darwin;export GOARCH=amd64;go build ${LDFLAGS} -o ${DIR}/${MAGENT}-${D}.dmg cmd/merlinagent/main.go

# Update JavaScript Information
agent-javascript:
sed -i 's/var build = ".*"/var build = "${BUILD}"/' data/html/scripts/merlin.js
sed -i 's/var version = ".*"/var version = "${VERSION}"/' data/html/scripts/merlin.js

# Make directory 'data' and then agents, db, log, x509; Copy src folder, README, and requirements
package-server-windows:
${PACKAGE} ${DIR}${MSERVER}-${W}-v${VERSION}.7z ${F}
${PACKAGE} ${DIR}/${MSERVER}-${W}-v${VERSION}.7z ${F}
cd ${DIR};${PACKAGE} ${MSERVER}-${W}-v${VERSION}.7z ${MSERVER}-${W}.exe

package-server-linux:
${PACKAGE} ${DIR}${MSERVER}-${L}-v${VERSION}.7z ${F}
${PACKAGE} ${DIR}/${MSERVER}-${L}-v${VERSION}.7z ${F}
cd ${DIR};${PACKAGE} ${MSERVER}-${L}-v${VERSION}.7z ${MSERVER}-${L}

package-server-darwin:
${PACKAGE} ${DIR}${MSERVER}-${D}-v${VERSION}.7z ${F}
${PACKAGE} ${DIR}/${MSERVER}-${D}-v${VERSION}.7z ${F}
cd ${DIR};${PACKAGE} ${MSERVER}-${D}-v${VERSION}.7z ${MSERVER}-${D}.dmg

package-agent-windows:
${PACKAGE} ${DIR}${MAGENT}-${W}-v${VERSION}.7z ${F2}
${PACKAGE} ${DIR}/${MAGENT}-${W}-v${VERSION}.7z ${F2}
cd ${DIR};${PACKAGE} ${MAGENT}-${W}-v${VERSION}.7z ${MAGENT}-${W}.exe
cp ${DIR}${MAGENT}-${W}.exe ${BIN}windows
cp ${DIR}/${MAGENT}-${W}.exe ${BIN}windows

package-agent-linux:
${PACKAGE} ${DIR}${MAGENT}-${L}-v${VERSION}.7z ${F2}
${PACKAGE} ${DIR}/${MAGENT}-${L}-v${VERSION}.7z ${F2}
cd ${DIR};${PACKAGE} ${MAGENT}-${L}-v${VERSION}.7z ${MAGENT}-${L}
cp ${DIR}${MAGENT}-${L} ${BIN}linux
cp ${DIR}/${MAGENT}-${L} ${BIN}linux

package-agent-darwin:
${PACKAGE} ${DIR}${MAGENT}-${D}-v${VERSION}.7z ${F2}
${PACKAGE} ${DIR}/${MAGENT}-${D}-v${VERSION}.7z ${F2}
cd ${DIR};${PACKAGE} ${MAGENT}-${D}-v${VERSION}.7z ${MAGENT}-${D}.dmg
cp ${DIR}${MAGENT}-${D}.dmg ${BIN}darwin
cp ${DIR}/${MAGENT}-${D}.dmg ${BIN}darwin

package-agent-dll:
${PACKAGE} ${DIR}${MAGENT}-DLL-v${VERSION}.7z ${F2}
${PACKAGE} ${DIR}/${MAGENT}-DLL-v${VERSION}.7z ${F2}
cd ${DIR};${PACKAGE} ${MAGENT}-DLL-v${VERSION}.7z merlin.dll
cp ${DIR}merlin.dll ${BIN}dll
cp ${DIR}/merlin.dll ${BIN}dll

package-all: package-agent-windows package-agent-dll package-agent-linux package-agent-darwin package-server-windows package-server-linux package-server-darwin

Expand Down
29 changes: 12 additions & 17 deletions cmd/merlinagent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,35 @@ import (
)

// GLOBAL VARIABLES
var debug = false
var verbose = false
var url = "https://127.0.0.1:443/"
var waitTime = 30000 * time.Millisecond
var build = "nonRelease"
var version = false


func main() {

flag.BoolVar(&verbose, "v", false, "Enable verbose output")
flag.BoolVar(&version, "version", false, "Print the agent version and exit")
flag.BoolVar(&debug, "debug", false, "Enable debug output")
verbose := flag.Bool("v", false, "Enable verbose output")
version := flag.Bool("version", false, "Print the agent version and exit")
debug := flag.Bool("debug", false, "Enable debug output")
flag.StringVar(&url, "url", url, "Full URL for agent to connect to")
flag.DurationVar(&waitTime, "sleep", 30000*time.Millisecond, "Time for agent to sleep")
protocol := flag.String("proto", "h2", "Protocol for the agent to connect with [h2, hq]")
sleep := flag.Duration( "sleep", 30000*time.Millisecond, "Time for agent to sleep")
flag.Usage = usage
flag.Parse()

if version {
if *version {
color.Blue(fmt.Sprintf("Merlin Agent Version: %s", merlin.Version))
color.Blue(fmt.Sprintf("Merlin Agent Build: %s", build))
os.Exit(0)
}

a := agent.New(verbose, debug)
a.WaitTime = waitTime
a.Run(url, "h2")
// Setup and run agent
a := agent.New(*protocol, *verbose, *debug)
a.WaitTime = *sleep
a.Run(url)
}



// usage prints command line options
func usage() {
fmt.Printf("Merlin Agent\r\n")
flag.PrintDefaults()
os.Exit(0)
}

}
4 changes: 2 additions & 2 deletions cmd/merlinagentdll/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func main() {}

// run is a private function called by exported functions to instantiate/execute the Agent
func run(url string){
a := agent.New(false, false)
a.Run(url, "h2")
a := agent.New("h2",false, false)
a.Run(url)
}

// EXPORTED FUNCTIONS
Expand Down
16 changes: 12 additions & 4 deletions cmd/merlinserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
// Standard
"flag"
"path/filepath"
"strconv"

// 3rd Party
"github.com/fatih/color"
Expand All @@ -44,7 +43,8 @@ func main() {
flag.BoolVar(&core.Verbose, "v", false, "Enable verbose output")
flag.BoolVar(&core.Debug, "debug", false, "Enable debug output")
port := flag.Int("p", 443, "Merlin Server Port")
ip := flag.String("i", "0.0.0.0", "The IP address of the interface to bind to")
ip := flag.String("i", "127.0.0.1", "The IP address of the interface to bind to")
proto := flag.String("proto", "h2", "Protocol for the agent to connect with [h2, hq]")
crt := flag.String("x509cert", filepath.Join(string(core.CurrentDir), "data", "x509", "server.crt"),
"The x509 certificate for the HTTPS listener")
key := flag.String("x509key", filepath.Join(string(core.CurrentDir), "data", "x509", "server.key"),
Expand All @@ -62,8 +62,16 @@ func main() {
color.Blue("\t\t Version: %s", merlin.Version)
color.Blue("\t\t Build: %s", build)

go http2.StartListener(strconv.Itoa(*port), *ip, *crt, *key, "/")
cli.Shell()
// Start Merlin Command Line Interface
go cli.Shell()

// Start Merlin Server to listen for agents
server, err := http2.New(*ip, *port, *proto, *key, *crt)
if err != nil {
color.Red(err.Error())
} else {
server.Run()
}
}

// TODO Add session ID
Expand Down
2 changes: 1 addition & 1 deletion data/bin/powershell/Invoke-Merlin.ps1

Large diffs are not rendered by default.

67 changes: 48 additions & 19 deletions data/html/scripts/merlin.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var debug = false;
var verbose = true;
var initial = true;
var hostUUID = guid();
var version = "1.0";
var version = "0.0.0";
var build = "nonRelease";
var waitTime = 30000; // in milliseconds
var maxRetry = 7;
Expand All @@ -45,32 +45,44 @@ function s4() {
.substring(1);
}

// Base Message
var b = {
"version": version,
"id": hostUUID,
"type": null,
"padding": "RandomDataGoesHere", // Not implemented yet
"padding": "RandomDataGoesHere", // TODO Not implemented yet
"payload": null
};

function initialCheckIn (){
// SysInfo Message
var s = {
"platform": navigator.platform,
"architecture": navigator.appCodeName,
"username": navigator.userAgent,
"userguid": navigator.appVersion,
"hostname": document.title
};

function initialCheckIn (){
if (debug){console.log("[DEBUG]Entering into initialCheckIn function")}
var x = new XMLHttpRequest();
var p = {
"platform": navigator.platform,
"architecture": navigator.appCodeName,
"username": navigator.userAgent,
"userguid": navigator.appVersion,
"hostname": document.title
var a = {
"version": version,
"build": build,
"waittime": (waitTime.toString())+ "ms", // TODO fix hard coding the duration to milliseconds with ms
"paddingmax": paddingMax,
"maxretry": maxRetry,
"failedcheckin": failedCheckin,
// "skew": "", TODO implement skew
"proto": "h2",
"sysinfo": s
};
b.type = "InitialCheckIn";
b.payload = p;
b.payload = a;
if (verbose){verboseMessage("note", "Connecting to web server at " + url + " for initial check in.")}
x.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
initial = false;
agentInfo();
failedCheckin = 0;
}
};
Expand All @@ -79,8 +91,16 @@ function initialCheckIn (){
if (debug){console.log("[DEBUG]Sending InitialCheckIn XHR")}
x.onerror = function(e) {
failedCheckin++;
verboseMessage("warn", failedCheckin + " out of " + maxRetry + " total failed checkins")
verboseMessage("warn", failedCheckin + " out of " + maxRetry + " total failed checkins");
if (debug){
console.log("[DEBUG]initialCheckIn POST request error:");
console.log(e)
}
};
if (debug){
console.log("[DEBUG]Sending initialCheckIn XHR payload:");
console.log(b)
}
x.send(JSON.stringify(b));
}

Expand All @@ -90,10 +110,12 @@ function agentInfo (){
var a = {
"version": version,
"build": build,
"waittime": waitTime.toString(),
"waittime": (waitTime.toString())+ "ms", // TODO fix hard coding the duration to milliseconds with ms
"paddingmax": paddingMax,
"maxretry": maxRetry,
"failedcheckin": failedCheckin
"failedcheckin": failedCheckin,
"proto": "h2",
"sysinfo": s
};
b.type = "AgentInfo";
b.payload = a;
Expand All @@ -106,7 +128,10 @@ function agentInfo (){
};
x.open('POST', url, true);
x.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
if (debug){console.log("[DEBUG]Sending AgentInfo XHR")}
if (debug){
console.log("[DEBUG]Sending AgentInfo XHR:");
console.log(b);
}
x.send(JSON.stringify(b));
}

Expand All @@ -127,7 +152,8 @@ function statusCheckIn (){
if (debug){console.log("[DEBUG]Sending StatusCheckIn XHR")}
x.onerror = function(e) {
failedCheckin++;
verboseMessage("warn", failedCheckin + " out of " + maxRetry + " total failed checkins")
verboseMessage("warn", failedCheckin + " out of " + maxRetry + " total failed checkins");
verboseMessage("warn", "Error: " + e.message)
};
x.send(JSON.stringify(b));
}
Expand All @@ -147,12 +173,14 @@ function cmdResults(job, stdOut, stdErr){
x.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
if (debug){console.log("[DEBUG]Sending cmdResults XHR")}
x.onerror = function(e) {
verboseMessage("warn", "There was an error sending the CmdResults message.")
verboseMessage("warn", "There was an error sending the CmdResults message.");
verboseMessage("warn", "Error: " + e.message)
};
x.send(JSON.stringify(b));
}

function verboseMessage(type, message){
if (debug){console.log("[DEBUG]Entering into verboseMessage function")}
if (verbose && log != null){
switch (type){
case "success":
Expand All @@ -173,6 +201,7 @@ function verboseMessage(type, message){
}

function processJSON(type, json){
if (debug){console.log("[DEBUG]Entering into processJSON function")}
verboseMessage("success", type + " message type received!");
switch (type){
case "ServerOk":
Expand Down Expand Up @@ -279,12 +308,12 @@ function main(){
today.toLocaleString("en-US", options))}
}

// Check for overide URL
// Check for override URL
if (typeof oURL == 'string'){url=oURL}

if (verbose){
verboseMessage("success", "Starting Merlin JavaScript Agent");
verboseMessage("note", "Agent version:" + version);
verboseMessage("note", "Agent version: " + version);
verboseMessage("note", "Agent build: " + build);
verboseMessage("note", "Agent UUID: " + hostUUID);
verboseMessage("note", "Platform: " + navigator.appCodeName);
Expand Down
Loading

0 comments on commit 61552e0

Please sign in to comment.