Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/byakugan/exts.cpp
Views: 11766
#include "byakugan.h"1#include "msfpattern.h"2#include "jutsu.h"3#include "tenketsu.h"4#include "mushishi.h"5#include "symPort.h"67#include "csv_parser.hpp"8#include <ios>9#include <iostream>10#include <sstream>1112char *registers[] = {13"eax",14"ebx",15"ecx",16"edx",17"esp",18"ebp",19"eip",20NULL21};2223HRESULT CALLBACK byakugan(PDEBUG_CLIENT4 Client, PCSTR args) {24INIT_API();25UNREFERENCED_PARAMETER(args);2627dprintf(HELPSTRING);28dprintf("!jutsu <command> <args> - Perform Jutsu: !jutsu help\n");29dprintf("!tenketsu - Begin realtime heap vizualization: !tenketsu help\n");30dprintf("!pattern_offset <length> <optional: addr>\n");31dprintf("!mushishi <detect|defeat>- Detect or defeat anti-debugging mechanisms\n");3233EXIT_API();34return (S_OK);35}3637HRESULT CALLBACK pattern_offset(PDEBUG_CLIENT4 Client, PCSTR args) {38char *arg1, **arg2, *holder[2], *context;39ULONG length, addr;40int offset, i;4142INIT_API();43UNREFERENCED_PARAMETER(args);4445arg1 = strtok((char *)args, " ");46arg2 = holder;47arg2[0] = strtok(NULL, " ");48arg2[1] = NULL;4950if (arg1 == NULL) {51dprintf("[Byakugan] Please provide a length.\n");52return (S_OK);53}5455length = strtoul(arg1, NULL, 10);5657if (arg2[0] == NULL)58arg2 = registers;5960for (i = 0; arg2[i] != NULL; i++) {61addr = GetExpression(arg2[i]);6263offset = msf_pattern_offset(length, addr);64if (offset != -1)65dprintf("[Byakugan] Control of %s at offset %d.\n", arg2[i], offset);66}6768EXIT_API();69return (S_OK);70}7172HRESULT CALLBACK mushishi(PDEBUG_CLIENT4 Client, PCSTR args) {73char *command;7475INIT_API();7677command = strtok((char *)args, " ");78if (command != NULL) {79if (!_stricmp(command, "detect")) {80mushishiDetect();81return (S_OK);82}83if (!_stricmp(command, "defeat")) {84mushishiDefeat();85return (S_OK);86}87}88dprintf("[Mushishi] Proper commands are: 'detect' 'defeat'\n");8990EXIT_API();91return (S_OK);92}9394HRESULT CALLBACK symport(PDEBUG_CLIENT4 Client, PCSTR args) {95char *command, *module, *path;9697INIT_API();9899module = strtok((char *)args, " ");100path = strtok(NULL, " ");101if (module != NULL && path != NULL) {102addMapFile(module, path);103return (S_OK);104} else {105dprintf("[symPort] Proper format is: !symport <moduleName> <map file path>\n");106}107EXIT_API();108return (S_OK);109}110111HRESULT CALLBACK jutsu(PDEBUG_CLIENT4 Client, PCSTR args) {112char *command, *bufName, *bufPatt, *bindPort, *bufSize, *bufType, *bufAddr;113using namespace std;114INIT_API();115116command = strtok((char *)args, " ");117if (command != NULL) {118if (!_stricmp(command, "help")) {119helpJutsu();120return (S_OK);121}122if (!_stricmp(command, "moduleInfo")) {123124}125if (!_stricmp(command, "memDiff")) {126bufType = strtok(NULL, " ");127bufSize = strtok(NULL, " ");128bufPatt = strtok(NULL, " ");129bufAddr = strtok(NULL, " ");130if (!bufAddr) {131dprintf("[J] Format: memDiff <type> <size> <value> <address>\n");132dprintf("Valid Types:\n\thex: Value is any hex characters\n");133dprintf("\tfile: Buffer is read in from file at path <value>\n");134dprintf("\tbuf: Buffer is taken from known tracked Buffers\n");135return (S_OK);136}137memDiffJutsu(bufType, strtoul(bufSize, NULL, 10),138bufPatt, strtoul(bufAddr, NULL, 0x10));139}140if (!_stricmp(command, "trackVal")) {141bufName = strtok(NULL, " ");142bufSize = strtok(NULL, " ");143bufPatt = strtok(NULL, " ");144145if (bufName == NULL) {146listTrackedVals();147} else if (bufSize == NULL) {148listTrackedValByName(bufName);149} else150trackValJutsu(bufName, strtoul(bufSize, NULL, 10),151strtoul(bufPatt, NULL, 0x10));152}153if (!_stricmp(command, "searchOpcode")) {154char *instructions;155156instructions = (char *) args + strlen(command) + 1;157searchOpcodes(instructions);158return (S_OK);159}160if (!_stricmp(command, "searchVtptr")) {161char *instructions, *offsetString;162DWORD offset;163164offsetString = strtok(NULL, " ");165offset = strtoul(offsetString, NULL, 16);166instructions = offsetString + strlen(offsetString) + 1;167searchVtptr(offset, instructions);168return (S_OK);169}170if (!_stricmp(command, "listen")) {171bindPort = strtok(NULL, " ");172if (bindPort == NULL)173bindPort = DEFAULT_PORT;174bindJutsu(bindPort);175return (S_OK);176}177if (!_stricmp(command, "listBuf")) {178listTrackedBufJutsu();179return (S_OK);180}181if (!_stricmp(command, "listReqs")) {182showRequestsJutsu();183return (S_OK);184}185if (!_stricmp(command, "rmBuf")) {186bufName = strtok(NULL, " ");187if (bufName == NULL) {188dprintf("[Byakugan] This command requires a buffer name\n");189return (S_OK);190}191rmBufJutsu(bufName);192return (S_OK);193}194if (!_stricmp(command, "identBuf")) {195196bufType = strtok(NULL, " ");197bufName = strtok(NULL, " ");198bufPatt = strtok(NULL, " ");199bufSize = strtok(NULL, " ");200if (bufPatt == NULL) {201dprintf("[Byakugan] This command requires a buffer type, name, (sometimes) value, and size\n");202return (S_OK);203}204if (bufSize == NULL)205identBufJutsu(bufType, bufName, bufPatt, 0, 0);206else207identBufJutsu(bufType, bufName, bufPatt, strtoul(bufSize, NULL, 10), 0);208return (S_OK);209}210if (!_stricmp(command, "identBufFile")) {211char *bufFile, *bufMap;212bufFile = strtok(NULL, " ");213bufMap = strtok(NULL, " ");214bufType = "smartFile";215216if (bufFile == NULL) {217dprintf("[Byakugan] This command requires a path to an input file and map (CSV) from 010\n");218return (S_OK);219}220221//these settings are explicting for 010 CSV export222const char field_terminator = ',';223const char line_terminator = '\n';224const char enclosure_char = '"';225226//create parse object227csv_parser file_parser;228229/* Define how many records we're gonna skip. This could be used to skip the column definitions. */230file_parser.set_skip_lines(1);231232/* Specify the file to parse */233file_parser.init(bufMap);234235/* Here we tell the parser how to parse the file */236file_parser.set_enclosed_char(enclosure_char, ENCLOSURE_OPTIONAL);237238file_parser.set_field_term_char(field_terminator);239240file_parser.set_line_term_char(line_terminator);241242/* Check to see if there are more records, then grab each row one at a time */243while(file_parser.has_more_rows())244{245csv_row fileRecord = file_parser.get_row();246247//the miracle of STL hex string conversion :)248istringstream stFileOffset(fileRecord[2].c_str());249istringstream stOffSetSize(fileRecord[3].c_str());250unsigned int offset;251unsigned int size;252stFileOffset >> hex >> offset;253stOffSetSize >> hex >> size;254255//dprintf("Allocating Buffer Name:%s at offset: %d with size: %d\n", fileRecord[0].c_str(), offset, size);256257//create individual buffers with the record type as a name and using the offset and size258identBufJutsu(bufType, (char *)fileRecord[0].c_str(), bufFile, size, offset);259}260return (S_OK);261}262if (!_stricmp(command, "hunt")) {263hunterJutsu();264}265266if (!_stricmp(command, "findReturn")) {267returnAddressHuntJutsu();268}269}270EXIT_API();271return (S_OK);272}273HRESULT CALLBACK tenketsu(PDEBUG_CLIENT4 Client, PCSTR args) {274char *command, *heapName, *logName;275PVOID heapHandle;276277INIT_API();278279command = strtok((char *)args, " ");280281if (command == NULL) {282tenkHelp();283return (S_OK);284}285else if (!_stricmp(command, "model")) {286if(hookRtlHeap(1, NULL)) {287dprintf("[Byakugan] Unable to begin realtime heap modeling.\n");288EXIT_API();289return (S_FALSE);290}291}292else if (!_stricmp(command, "log")) {293logName = strtok(NULL, " ");294if (logName == NULL) {295dprintf("[Byakugan] Please provide a log name.\n");296return (S_FALSE);297}298if(hookRtlHeap(2, logName)) {299dprintf("[Byakugan] Unable to begin realtime heap modeling.\n");300EXIT_API();301return (S_FALSE);302}303}304else if (!_stricmp(command, "help")) {305tenkHelp();306return (S_OK);307}308else if (!_stricmp(command, "validate")) {309heapName = strtok(NULL, " ");310if (heapName == NULL) {311dprintf("[Byakugan] Please provide a heap handle.\n");312return (S_OK);313}314heapHandle = (PVOID) strtoul(heapName, NULL, 16);315tenkValidate(heapHandle);316return (S_OK);317}318else if (!_stricmp(command, "listHeaps")) {319tenkListHeaps();320return (S_OK);321}322else if (!_stricmp(command, "listChunks")) {323heapName = strtok(NULL, " ");324if (heapName == NULL) {325dprintf("[Byakugan] Please provide a heap handle.\n");326return (S_OK);327}328heapHandle = (PVOID) strtoul(heapName, NULL, 16);329tenkListChunks(heapHandle);330return (S_OK);331}332333EXIT_API();334335return (S_OK);336}337338339