-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
275 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,128 +1,5 @@ | ||
import htmlgen | ||
import jester | ||
import psapi | ||
import nimja/parser | ||
import os | ||
import shell | ||
import environ | ||
import strtabs | ||
import net | ||
import injector | ||
import strutils | ||
when defined(wmi): | ||
import wmi | ||
when defined(dumper): | ||
import dumper | ||
|
||
const | ||
BINDADDR = "127.0.0.1" | ||
HTTPDIR = getScriptDir() / "http" | ||
BULMACSS = staticRead HTTPDIR / "bulma.min.css" | ||
HTMX = staticRead HTTPDIR / "htmx.min.js" | ||
|
||
proc pickPort(minPort = 5000, tries=64): Port {.inline.} = | ||
## Try to bind a port to determine if it can be used by http module. If port | ||
## is used, increase port number and try again. | ||
## | ||
## Race condition might happen here if port is bound in between check and | ||
## actual http server bind. | ||
for i in minPort..(minPort+tries): | ||
var server = newSocket() | ||
try: | ||
server.bindAddr(Port(i), address = BINDADDR) | ||
server.listen() | ||
server.close() | ||
except OSError: | ||
continue | ||
return Port(i) | ||
|
||
proc renderIndex(request: Request): string = | ||
let envInfo = getEnvInfo() | ||
compileTemplateFile HTTPDIR / "index.nwt" | ||
|
||
proc renderModules(request: Request, mods: ModuleList): string = | ||
let envInfo = getEnvInfo() | ||
compileTemplateFile HTTPDIR / "modules.nwt" | ||
|
||
proc renderProcesses(request: Request, ps: ProcessList): string = | ||
let envInfo = getEnvInfo() | ||
compileTemplateFile HTTPDIR / "processes.nwt" | ||
|
||
proc renderShell(request: Request, cmd = ""): string = # TODO: separate the api request (command execution) | ||
let envInfo = getEnvInfo() | ||
var | ||
output: string | ||
exCode: int | ||
if cmd != "": | ||
(output, exCode) = runShell(cmd) | ||
return output | ||
else: | ||
output = "Command output..." | ||
compileTemplateFile HTTPDIR / "shell.nwt" | ||
|
||
proc renderWmi(request: Request, namespace = "", query = ""): string = | ||
let envInfo = getEnvInfo() | ||
var output = "Query output..." | ||
when defined(wmi): | ||
if request.reqMethod == HttpPost: | ||
output = queryWmi(namespace, query) | ||
# echo namespace | ||
# echo query | ||
# echo output | ||
compileTemplateFile HTTPDIR / "wmi.nwt" | ||
|
||
router paraRoutes: | ||
get "/": | ||
resp renderIndex(request) | ||
get "/modules": | ||
let mods = getModulesInfo() | ||
resp renderModules(request, mods) | ||
get "/processes": | ||
let ps = getRunningProcesses() | ||
resp renderProcesses(request, ps) | ||
post "/modules/load": | ||
if loadModule(@"modname"): | ||
redirect "/modules" | ||
else: | ||
resp p("unable to load module.") | ||
delete "/modules/unload/@modName": | ||
if unloadModule(@"modName"): | ||
resp "" | ||
else: | ||
resp p("unable to unload module.") # TODO | ||
get "/processes/dump/@targetPid": | ||
when defined(dumper): | ||
discard dumpToFile(@"targetPid".parseInt(), r"C:\Temp\image_" & @"targetPid" & ".dmp") # EXPERIMENTAL | ||
redirect "/processes" | ||
get "/processes/inject/@targetPid": | ||
injectModule(@"targetPid".parseInt(), getMyPath()) | ||
redirect "/processes" | ||
delete "/processes/kill/@targetPid": | ||
terminatePid(@"targetPid".parseInt()) | ||
resp "" # TODO: check if success. | ||
get "/shell": | ||
resp renderShell(request) | ||
put "/shell": | ||
resp renderShell(request, @"cmd") | ||
get "/wmi": | ||
resp renderWmi(request) | ||
post "/wmi": | ||
resp renderWmi(request, @"namespace", @"query") | ||
get "/quit": | ||
terminateThread() | ||
get "/bulma.min.css": | ||
resp(content=BULMACSS, contentType="text/css;charset=utf=8") | ||
get "/htmx.min.js": | ||
resp(content=HTMX, contentType="application/javascript;charset=utf-8") | ||
|
||
proc runHttpServ*() {.stdcall.} = | ||
## Serve http module. | ||
let port = pickPort() | ||
let settings = newSettings(port=port, bindAddr=BINDADDR) | ||
var jester = initJester(paraRoutes, settings=settings) | ||
jester.serve() | ||
|
||
when isMainModule: | ||
import parasite/httpserv | ||
proc NimMain*() {.cdecl, importc, exportc, dynlib, used.} # psapi getMyPath() requires it | ||
|
||
runHttpServ() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import strutils | ||
import std/[paths, macros] | ||
import utils | ||
|
||
proc getVer(): string {.compileTime.} = | ||
## Returns parasite version string. | ||
let nimble = staticRead(Path(getProjectPath()).parentDir() / "parasite.nimble") | ||
for l in nimble.splitLines(): | ||
if l.strip().startsWith("version"): | ||
return l.split('"')[1] | ||
return "<unknown>" | ||
|
||
proc getCompileEnv(): string {.compileTime.} = | ||
## Returns compilation details for easier tracking. | ||
let user = staticExec("whoami").strip() | ||
let host = staticExec("hostname").strip() | ||
return user & "@" & host | ||
|
||
const | ||
BINDADDR* = "127.0.0.1" | ||
SCRIPTDIR* = getProjectPath() | ||
HTTPDIR* = SCRIPTDIR / "parasite" / "http" | ||
BULMACSS* = staticRead HTTPDIR / "bulma.min.css" | ||
HTMX* = staticRead HTTPDIR / "htmx.min.js" | ||
ARCH* = $(sizeof(int)*8) | ||
VER* = getVer() | ||
COMPILED* = getCompileEnv() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import winim | ||
|
||
type | ||
MINIDUMP_TYPE = enum | ||
MiniDumpWithFullMemory = 0x00000002 | ||
|
||
proc MiniDumpWriteDump*( | ||
hProcess: HANDLE, | ||
processId: DWORD, | ||
file: HANDLE, | ||
dumpType: MINIDUMP_TYPE, | ||
exceptionParam: PTR, | ||
userStreamParam: PTR, | ||
callbackParam: PTR | ||
): BOOL {.importc: "MiniDumpWriteDump", dynlib: r"C:\Windows\System32\dbghelp.dll", stdcall.} | ||
|
||
proc dumpToFile*(targetPid: int, dumpFile: string): bool = | ||
result = false | ||
var hProc = OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, false, targetPid.DWORD) | ||
if not bool(hProc): | ||
raise newException(OSError, "Unable to obtain process handle.") | ||
try: | ||
var df = open(dumpFile, fmWrite) | ||
result = bool(MiniDumpWriteDump( | ||
hProc, | ||
targetPid.DWORD, | ||
df.getOsFileHandle(), | ||
MiniDumpWithFullMemory, | ||
NULL, | ||
NULL, | ||
NULL | ||
)) | ||
df.close() | ||
finally: | ||
CloseHandle(hProc) |
Oops, something went wrong.