diff --git a/Makefile b/Makefile index c2d4f96..ff6c0d0 100644 --- a/Makefile +++ b/Makefile @@ -1,20 +1,19 @@ CXX = g++ CXXFLAGS = -O3 -std=c++11 -Wextra -LDFLAGS = -lcrypto -LIBS = -lcrypto +LDFLAGS = -lcrypto -lcurl -ljsoncpp +LIBS = -lcrypto -lcurl -ljsoncpp INCS = -I./include PROG = get_flag SRCS = $(wildcard *.cpp) -OBJS = $(patsubst %.cpp, %.o, $(SRCS)) -SUBDIRS = all clean +SUBDIRS = single multi clean -all: $(OBJS) - $(CXX) $(CXXFLAGS) -o $(PROG) $(OBJS) $(INCS) $(LDFLAGS) +single: + @$(CXX) $(SRCS) $(CXXFLAGS) -o $(PROG) $(INCS) $(LDFLAGS) -%.o: %.cpp - $(CXX) $(CXXFLAGS) -c $< $(LIBS) $(INCS) -o $@ +multi: + @$(CXX) $(SRCS) $(CXXFLAGS) -o $(PROG) $(INCS) $(LDFLAGS) -DMULTISERVICE .PHONY: $(SUBDIRS) clean: - rm -f *.o $(PROG) + @rm -f *.o $(PROG) diff --git a/get_flag.cpp b/get_flag.cpp index ced6270..b38091c 100644 --- a/get_flag.cpp +++ b/get_flag.cpp @@ -5,7 +5,18 @@ #define INTERFACE "ens33" +#ifdef MULTISERVICE +#define FLAGSUBMIT_URL "http://192.168.1.5:8888/flag/submit/" +#include +#include +#include +#include +#include +#include "services.h" +#else #include "crypto.hpp" +#endif + #include "util.hpp" int main(int argc, char **argv) @@ -14,6 +25,15 @@ int main(int argc, char **argv) byte *key = (byte *) "c3bb356d542b36ca5a352cd3a6276014"; EVP_add_cipher(EVP_aes_256_cbc()); +#ifdef MULTISERVICE + Json::Value jsonData; + Json::Reader jsonReader; + + uid_t uid = getuid(); + std::string flag = util::getServiceFlag(uid); + std::cout << flag; +#else + crypto::gen_params(iv); secure_string ptext = util::getLocalIP(); @@ -28,6 +48,7 @@ int main(int argc, char **argv) secure_string b64text = crypto::base64_encode(ctext); std::cout << b64text; +#endif exit(EXIT_SUCCESS); } diff --git a/include/services.h b/include/services.h new file mode 100644 index 0000000..ff17acd --- /dev/null +++ b/include/services.h @@ -0,0 +1,18 @@ +#ifndef SERVICES_H +#define SERVICES_H + +/* + * uid - to match with the sqlite services uid + */ +struct services_info +{ + int uid; +}; + +struct services_info service[] = +{ + {1000}, + {1001} +}; + +#endif diff --git a/include/util.hpp b/include/util.hpp index 79424c9..88e4850 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -3,6 +3,49 @@ namespace util { +#ifdef MULTISERVICE + std::size_t callback(const char *in, std::size_t size, std::size_t num, std::string *out) + { + const std::size_t totalBytes(size * num); + out->append(in, totalBytes); + return totalBytes; + } + + std::string getServiceFlag(uid_t u, std::string local_ip, int s) + { + std::string uid = std::to_string(u); + const std::string flagsubmit_url(FLAGSUBMIT_URL + uid); + + CURL* curl = curl_easy_init(); + CURLcode res; + + curl_easy_setopt(curl, CURLOPT_URL, flagsubmit_url.c_str()); + curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); + curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + + long httpCode(0); + std::unique_ptr httpData(new std::string()); + + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, util::callback); + + curl_easy_setopt(curl, CURLOPT_WRITEDATA, httpData.get()); + + if ((res = curl_easy_perform(curl)) != CURLE_OK) + { + exit(EXIT_FAILURE); + } + + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode); + curl_easy_cleanup(curl); + + if (httpCode == 200) + return *httpData.get(); + + return NULL; + } +#else + extern "C" { #include @@ -42,6 +85,7 @@ namespace util } } } +#endif } #endif