Skip to content

Force Proxy to any Windows/MacOS/Linux Application TCP and UDP traffic

optimized iptable commands, GUI, gui compiler

anof-cyberf750ecdc32f6
diff --git a/Linux/build.sh b/Linux/build.sh
index ed07efc..7d8c800 100755
--- a/Linux/build.sh
+++ b/Linux/build.sh
@@ -22,9 +22,9 @@ make clean 2>/dev/null || true
make
if [ -f "libproxybridge.so" ]; then
- echo "✓ Library build successful"
+ echo "Library build successful"
else
- echo "✗ Library build failed!"
+ echo "Library build failed!"
exit 1
fi
echo ""
@@ -36,9 +36,9 @@ make clean 2>/dev/null || true
make
if [ -f "ProxyBridge" ]; then
- echo "✓ CLI build successful"
+ echo "CLI build successful"
else
- echo "✗ CLI build failed!"
+ echo "CLI build failed!"
exit 1
fi
echo ""
@@ -48,12 +48,16 @@ echo "=== Building GUI ==="
cd "$SCRIPT_DIR"
rm -f ProxyBridgeGUI
if pkg-config --exists gtk+-3.0; then
- gcc -Wall -Wno-unused-parameter -O3 -Isrc -fstack-protector-strong -D_FORTIFY_SOURCE=2 -fPIE -Wformat -Wformat-security -Werror=format-security -fno-strict-overflow -fno-delete-null-pointer-checks -fwrapv -c gui/main.c -o gui_main.o $(pkg-config --cflags gtk+-3.0)
- gcc -o ProxyBridgeGUI gui_main.o -Lsrc -pie -Wl,-z,relro,-z,now -Wl,-z,noexecstack -s -Wl,-rpath,'$ORIGIN/.' -lproxybridge -lpthread $(pkg-config --libs gtk+-3.0) -export-dynamic
+ GUI_CFLAGS="-Wall -Wno-unused-parameter -O3 -Isrc -D_GNU_SOURCE -fstack-protector-strong -D_FORTIFY_SOURCE=2 -fPIE -Wformat -Wformat-security -Werror=format-security -fno-strict-overflow -fno-delete-null-pointer-checks -fwrapv $(pkg-config --cflags gtk+-3.0)"
+ GUI_LDFLAGS="-Lsrc -pie -Wl,-z,relro,-z,now -Wl,-z,noexecstack -s -Wl,-rpath,'$ORIGIN/.' -lproxybridge -lpthread $(pkg-config --libs gtk+-3.0) -export-dynamic"
+
+ gcc $GUI_CFLAGS -c gui/main.c -o gui_main.o
+ gcc -o ProxyBridgeGUI gui_main.o $GUI_LDFLAGS
+
rm -f gui_main.o
- echo "✓ GUI build successful"
+ echo "GUI build successful"
else
- echo "⚠ GTK3 not found. Skipping GUI build."
+ echo "GTK3 not found. Skipping GUI build."
echo " Install with: sudo apt install libgtk-3-dev (Debian/Ubuntu/Mint)"
echo " sudo dnf install gtk3-devel (Fedora)"
fi
@@ -65,7 +69,7 @@ mv "$SCRIPT_DIR/cli/ProxyBridge" "$OUTPUT_DIR/"
if [ -f ProxyBridgeGUI ]; then
mv ProxyBridgeGUI "$OUTPUT_DIR/"
fi
-echo "✓ Binaries moved to output"
+echo "Binaries moved to output"
echo ""
# Cleanup build files
@@ -76,7 +80,7 @@ make clean 2>/dev/null || true
cd "$SCRIPT_DIR/cli"
rm -f *.o
make clean 2>/dev/null || true
-echo "✓ Cleanup complete"
+echo "Cleanup complete"
echo ""
# Show results
diff --git a/Linux/gui/main.c b/Linux/gui/main.c
index 72e8530..eec5c9a 100644
--- a/Linux/gui/main.c
+++ b/Linux/gui/main.c
@@ -6,8 +6,20 @@
#include <time.h>
#include <pthread.h>
#include <signal.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
#include "ProxyBridge.h"
+static long safe_strtol(const char *nptr) {
+ if (!nptr) return 0; // Handle NULL
+ char *endptr;
+ long val = strtol(nptr, &endptr, 10);
+ // You could check errno here if needed, but for GUI fields 0 is often a safe fallback or filtered before
+ if (endptr == nptr) return 0; // No digits found
+ return val;
+}
+
// --- Global UI Widgets ---
static GtkWidget *window;
static GtkTextView *conn_view;
@@ -290,7 +302,7 @@ static void on_start_test_clicked(GtkWidget *widget, gpointer data) {
// 2. Set Proxy Config
ProxyType type = (gtk_combo_box_get_active(GTK_COMBO_BOX(info->type_combo)) == 0) ? PROXY_TYPE_HTTP : PROXY_TYPE_SOCKS5;
- int port = atoi(port_text);
+ int port = (int)safe_strtol(port_text);
const char *user = gtk_entry_get_text(GTK_ENTRY(info->user_entry));
const char *pass = gtk_entry_get_text(GTK_ENTRY(info->pass_entry));
@@ -301,7 +313,7 @@ static void on_start_test_clicked(GtkWidget *widget, gpointer data) {
const char *t_port_s = gtk_entry_get_text(GTK_ENTRY(info->test_port));
if (!t_host || strlen(t_host) == 0) t_host = "google.com";
- int t_port = atoi(t_port_s);
+ int t_port = (int)safe_strtol(t_port_s);
if (t_port <= 0) t_port = 80;
// 4. Update UI
@@ -362,8 +374,8 @@ static void on_proxy_configure(GtkWidget *widget, gpointer data) {
gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Port:"), 0, 2, 1, 1);
info.port_entry = gtk_entry_new();
if (g_proxy_port != 0) {
- char port_str[8];
- sprintf(port_str, "%d", g_proxy_port);
+ char port_str[16];
+ snprintf(port_str, sizeof(port_str), "%d", g_proxy_port);
gtk_entry_set_text(GTK_ENTRY(info.port_entry), port_str);
}
gtk_grid_attach(GTK_GRID(grid), info.port_entry, 1, 2, 3, 1);
@@ -444,7 +456,7 @@ static void on_proxy_configure(GtkWidget *widget, gpointer data) {
continue;
}
- int p = atoi(port_text);
+ int p = (int)safe_strtol(port_text);
if (p < 1 || p > 65535) {
GtkWidget *err = gtk_message_dialog_new(GTK_WINDOW(info.dialog), GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Port must be between 1 and 65535.");
gtk_dialog_run(GTK_DIALOG(err));
@@ -1049,7 +1061,7 @@ static void refresh_rules_ui() {
gtk_grid_attach(GTK_GRID(grid), act_box, 2, row, 1, 1);
// SR
- char sr_str[8]; sprintf(sr_str, "%d", sr_counter++);
+ char sr_str[16]; snprintf(sr_str, sizeof(sr_str), "%d", sr_counter++);
gtk_grid_attach(GTK_GRID(grid), gtk_label_new(sr_str), 3, row, 1, 1);
// Process
@@ -1194,14 +1206,40 @@ static void on_create_update_script_and_run() {
ProxyBridge_Stop();
const char *script_url = "https://raw.githubusercontent.com/InterceptSuite/ProxyBridge/refs/heads/master/Linux/deploy.sh";
- const char *script_path = "/tmp/proxybridge_deploy.sh";
- char dl_cmd[512];
- snprintf(dl_cmd, sizeof(dl_cmd), "curl -s -o %s %s && chmod +x %s", script_path, script_url, script_path);
+ // Secure temp directory
+ char tmp_dir_tpl[] = "/tmp/pb_update_XXXXXX";
+ char *tmp_dir = mkdtemp(tmp_dir_tpl);
+ if (!tmp_dir) {
+ fprintf(stderr, "Failed to create temp directory for update.\n");
+ exit(1);
+ }
+
+ char script_path[512];
+ snprintf(script_path, sizeof(script_path), "%s/deploy.sh", tmp_dir);
- if (system(dl_cmd) != 0) {
- // This likely won't be seen if we are shutting down, but worth a try (or stderr)
- fprintf(stderr, "Failed to download update script.\n");
+ // Download using fork/exec of curl, safer than system()
+ pid_t pid = fork();
+ if (pid == -1) {
+ fprintf(stderr, "Fork failed.\n");
+ exit(1);
+ } else if (pid == 0) {
+ // Child
+ execlp("curl", "curl", "-s", "-o", script_path, script_url, NULL);
+ _exit(127);
+ } else {
+ // Parent
+ int status;
+ waitpid(pid, &status, 0);
+ if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
+ fprintf(stderr, "Failed to download update script.\n");
+ exit(1);
+ }
+ }
+
+ // chmod +x
+ if (chmod(script_path, S_IRWXU) != 0) {
+ perror("chmod failed");
exit(1);
}
@@ -1212,30 +1250,44 @@ static void on_create_update_script_and_run() {
static void on_check_update(GtkWidget *widget, gpointer data) {
const char *url = "https://api.github.com/repos/InterceptSuite/ProxyBridge/releases/latest";
- char cmd[512];
- snprintf(cmd, sizeof(cmd), "curl -s -H \"User-Agent: ProxyBridge-Linux\" %s", url);
-
- FILE *fp = popen(cmd, "r");
- if (!fp) {
- GtkWidget *msg = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Failed to contact update server.");
+ char *cmd = g_strdup_printf("curl -s -H \"User-Agent: ProxyBridge-Linux\" %s", url);
+
+ char *standard_output = NULL;
+ char *standard_error = NULL;
+ GError *error = NULL;
+ int exit_status = 0;
+
+ // Use GLib spawn instead of popen for safety and correct signal handling
+ gboolean result = g_spawn_command_line_sync(cmd,
+ &standard_output,
+ &standard_error,
+ &exit_status,
+ &error);
+ g_free(cmd);
+
+ if (!result) {
+ GtkWidget *msg = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Failed to launch update check: %s", error ? error->message : "Unknown error");
gtk_dialog_run(GTK_DIALOG(msg));
gtk_widget_destroy(msg);
+ if (error) g_error_free(error);
return;
}
-
- char buffer[4096];
- size_t n = fread(buffer, 1, sizeof(buffer)-1, fp);
- buffer[n] = 0;
- pclose(fp);
-
- if (n == 0) {
- GtkWidget *msg = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Empty response from update server.");
+
+ if (exit_status != 0 || !standard_output || strlen(standard_output) == 0) {
+ GtkWidget *msg = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Update check failed (Exit: %d).", exit_status);
gtk_dialog_run(GTK_DIALOG(msg));
gtk_widget_destroy(msg);
+ g_free(standard_output);
+ g_free(standard_error);
return;
}
- char *tag_name = extract_sub_json_str(buffer, "tag_name");
+ // Copy to buffer for existing logic (simplified)
+ // Or just use standard_output directly
+ char *tag_name = extract_sub_json_str(standard_output, "tag_name");
+ g_free(standard_output);
+ g_free(standard_error);
+
if (!tag_name) {
GtkWidget *msg = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, "Could not parse version info.\nResponse might be rate limited.");
gtk_dialog_run(GTK_DIALOG(msg));
diff --git a/Linux/src/ProxyBridge.c b/Linux/src/ProxyBridge.c
index 829b3c7..9b4cad0 100644
--- a/Linux/src/ProxyBridge.c
+++ b/Linux/src/ProxyBridge.c
@@ -24,6 +24,7 @@
#include <linux/sock_diag.h>
#include <linux/inet_diag.h>
#include <netdb.h>
+#include <sys/wait.h>
#include "ProxyBridge.h"
#define LOCAL_PROXY_PORT 34010
@@ -38,6 +39,68 @@
#define HTTP_BUFFER_SIZE 1024
#define LOG_BUFFER_SIZE 1024
+// Secure replacement for system() to avoid shell injection and adhere to secure coding practices
+static int run_command_v(const char *cmd_path, char *const argv[]) {
+ pid_t pid = fork();
+ if (pid == -1) {
+ return -1;
+ } else if (pid == 0) {
+ // Child
+ // Redirect stdout/stderr to /dev/null
+ int fd = open("/dev/null", O_WRONLY);
+ if (fd >= 0) {
+ dup2(fd, STDOUT_FILENO);
+ dup2(fd, STDERR_FILENO);
+ close(fd);
+ }
+ execvp(cmd_path, argv);
+ _exit(127); // Access denied or not found
+ } else {
+ // Parent
+ int status;
+ waitpid(pid, &status, 0);
+ if (WIFEXITED(status)) {
+ return WEXITSTATUS(status);
+ }
+ return -1;
+ }
+}
+
+// Helper to run simple commands without arguments or simplified interface
+static int run_iptables_cmd(const char *arg1, const char *arg2, const char *arg3, const char *arg4, const char *arg5, const char *arg6, const char *arg7, const char *arg8, const char *arg9, const char *arg10, const char *arg11, const char *arg12, const char *arg13, const char *arg14) {
+ // Construct argv excluding NULLs
+ const char *argv[17];
+ int i = 0;
+ argv[i++] = "iptables";
+ if (arg1) argv[i++] = arg1;
+ if (arg2) argv[i++] = arg2;
+ if (arg3) argv[i++] = arg3;
+ if (arg4) argv[i++] = arg4;
+ if (arg5) argv[i++] = arg5;
+ if (arg6) argv[i++] = arg6;
+ if (arg7) argv[i++] = arg7;
+ if (arg8) argv[i++] = arg8;
+ if (arg9) argv[i++] = arg9;
+ if (arg10) argv[i++] = arg10;
+ if (arg11) argv[i++] = arg11;
+ if (arg12) argv[i++] = arg12;
+ if (arg13) argv[i++] = arg13;
+ if (arg14) argv[i++] = arg14;
+ argv[i] = NULL;
+
+ return run_command_v("iptables", (char **)argv);
+}
+
+// Helper for strtol to replace atoi
+static int safe_atoi(const char *str) {
+ if (!str) return 0;
+ char *endptr;
+ long val = strtol(str, &endptr, 10);
+ if (endptr == str) return 0;
+ return (int)val;
+}
+
+
typedef struct PROCESS_RULE {
uint32_t rule_id;
char process_name[MAX_PROCESS_NAME];
@@ -324,7 +387,7 @@ static uint32_t find_pid_from_inode(unsigned long target_inode, uint32_t uid_hin
if (link_len == expected_len) {
link_target[link_len] = '\0';
if (memcmp(link_target, expected, expected_len) == 0) {
- pid = (uint32_t)atoi(proc_entry->d_name);
+ pid = (uint32_t)safe_atoi(proc_entry->d_name);
closedir(fd_dir);
closedir(proc_dir);
return pid;
@@ -531,14 +594,14 @@ static bool match_ip_pattern(const char *pattern, uint32_t ip)
char *dash = strchr(pattern_octets[i], '-');
if (dash != NULL)
{
- int start = atoi(pattern_octets[i]);
- int end = atoi(dash + 1);
+ int start = safe_atoi(pattern_octets[i]);
+ int end = safe_atoi(dash + 1);
if (ip_octets[i] < start || ip_octets[i] > end)
return false;
}
else
{
- int pattern_val = atoi(pattern_octets[i]);
+ int pattern_val = safe_atoi(pattern_octets[i]);
if (pattern_val != ip_octets[i])
return false;
}
@@ -554,12 +617,12 @@ static bool match_port_pattern(const char *pattern, uint16_t port)
char *dash = strchr(pattern, '-');
if (dash != NULL)
{
- int start_port = atoi(pattern);
- int end_port = atoi(dash + 1);
+ int start_port = safe_atoi(pattern);
+ int end_port = safe_atoi(dash + 1);
return (port >= start_port && port <= end_port);
}
- return (port == atoi(pattern));
+ return (port == safe_atoi(pattern));
}
static bool ip_match_wrapper(const char *token, const void *data)
@@ -2673,8 +2736,8 @@ bool ProxyBridge_Start(void)
// setup iptables rules for packet interception - USE MANGLE table so it runs BEFORE nat
log_message("setting up iptables rules");
// mangle table runs before nat, so we can mark packets there
- int ret1 = system("iptables -t mangle -A OUTPUT -p tcp -j NFQUEUE --queue-num 0");
- int ret2 = system("iptables -t mangle -A OUTPUT -p udp -j NFQUEUE --queue-num 0");
+ int ret1 = run_iptables_cmd("-t", "mangle", "-A", "OUTPUT", "-p", "tcp", "-j", "NFQUEUE", "--queue-num", "0", NULL, NULL, NULL, NULL);
+ int ret2 = run_iptables_cmd("-t", "mangle", "-A", "OUTPUT", "-p", "udp", "-j", "NFQUEUE", "--queue-num", "0", NULL, NULL, NULL, NULL);
if (ret1 != 0 || ret2 != 0) {
log_message("failed to add iptables rules ret1=%d ret2=%d", ret1, ret2);
@@ -2683,8 +2746,8 @@ bool ProxyBridge_Start(void)
}
// setup nat redirect for marked packets
- int ret3 = system("iptables -t nat -A OUTPUT -p tcp -m mark --mark 1 -j REDIRECT --to-port 34010");
- int ret4 = system("iptables -t nat -A OUTPUT -p udp -m mark --mark 2 -j REDIRECT --to-port 34011");
+ int ret3 = run_iptables_cmd("-t", "nat", "-A", "OUTPUT", "-p", "tcp", "-m", "mark", "--mark", "1", "-j", "REDIRECT", "--to-port", "34010");
+ int ret4 = run_iptables_cmd("-t", "nat", "-A", "OUTPUT", "-p", "udp", "-m", "mark", "--mark", "2", "-j", "REDIRECT", "--to-port", "34011");
if (ret3 != 0 || ret4 != 0) {
log_message("failed to add nat redirect rules");
}
@@ -2719,10 +2782,10 @@ bool ProxyBridge_Stop(void)
running = false;
// cleanup iptables
- int ret1 = system("iptables -t mangle -D OUTPUT -p tcp -j NFQUEUE --queue-num 0 2>/dev/null");
- int ret2 = system("iptables -t mangle -D OUTPUT -p udp -j NFQUEUE --queue-num 0 2>/dev/null");
- int ret3 = system("iptables -t nat -D OUTPUT -p tcp -m mark --mark 1 -j REDIRECT --to-port 34010 2>/dev/null");
- int ret4 = system("iptables -t nat -D OUTPUT -p udp -m mark --mark 2 -j REDIRECT --to-port 34011 2>/dev/null");
+ int ret1 = run_iptables_cmd("-t", "mangle", "-D", "OUTPUT", "-p", "tcp", "-j", "NFQUEUE", "--queue-num", "0", NULL, NULL, NULL, NULL);
+ int ret2 = run_iptables_cmd("-t", "mangle", "-D", "OUTPUT", "-p", "udp", "-j", "NFQUEUE", "--queue-num", "0", NULL, NULL, NULL, NULL);
+ int ret3 = run_iptables_cmd("-t", "nat", "-D", "OUTPUT", "-p", "tcp", "-m", "mark", "--mark", "1", "-j", "REDIRECT", "--to-port", "34010");
+ int ret4 = run_iptables_cmd("-t", "nat", "-D", "OUTPUT", "-p", "udp", "-m", "mark", "--mark", "2", "-j", "REDIRECT", "--to-port", "34011");
(void)ret1;
(void)ret2;
(void)ret3;
@@ -2981,9 +3044,9 @@ static void library_cleanup(void)
{
// Even if not running, ensure iptables rules are removed
// This handles cases where the app crashed before calling Stop
- (void)!system("iptables -t mangle -D OUTPUT -p tcp -j NFQUEUE --queue-num 0 2>/dev/null");
- (void)!system("iptables -t mangle -D OUTPUT -p udp -j NFQUEUE --queue-num 0 2>/dev/null");
- (void)!system("iptables -t nat -D OUTPUT -p tcp -m mark --mark 1 -j REDIRECT --to-port 34010 2>/dev/null");
- (void)!system("iptables -t nat -D OUTPUT -p udp -m mark --mark 2 -j REDIRECT --to-port 34011 2>/dev/null");
+ run_iptables_cmd("-t", "mangle", "-D", "OUTPUT", "-p", "tcp", "-j", "NFQUEUE", "--queue-num", "0", NULL, NULL, NULL, NULL);
+ run_iptables_cmd("-t", "mangle", "-D", "OUTPUT", "-p", "udp", "-j", "NFQUEUE", "--queue-num", "0", NULL, NULL, NULL, NULL);
+ run_iptables_cmd("-t", "nat", "-D", "OUTPUT", "-p", "tcp", "-m", "mark", "--mark", "1", "-j", "REDIRECT", "--to-port", "34010");
+ run_iptables_cmd("-t", "nat", "-D", "OUTPUT", "-p", "udp", "-m", "mark", "--mark", "2", "-j", "REDIRECT", "--to-port", "34011");
}
}