2012-07-16 08:26:42 +08:00
|
|
|
/* (C) 2011, 2012 rofl0r
|
2011-02-25 17:40:11 +08:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************/
|
2012-07-16 08:26:42 +08:00
|
|
|
|
2020-09-05 21:36:05 +08:00
|
|
|
#define _DEFAULT_SOURCE
|
2011-02-25 17:40:11 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2011-09-04 07:45:16 +08:00
|
|
|
#include <string.h>
|
2011-02-25 17:40:11 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2019-11-05 11:39:46 +08:00
|
|
|
#ifdef IS_MAC
|
|
|
|
#define _DARWIN_C_SOURCE
|
|
|
|
#endif
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
2011-09-11 04:32:27 +08:00
|
|
|
#include "common.h"
|
2011-02-25 20:21:34 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
static int usage(char **argv) {
|
|
|
|
printf("\nUsage:\t%s -q -f config_file program_name [arguments]\n"
|
|
|
|
"\t-q makes proxychains quiet - this overrides the config setting\n"
|
2016-12-05 15:37:11 +08:00
|
|
|
"\t-f allows one to manually specify a configfile to use\n"
|
2012-01-28 01:59:44 +08:00
|
|
|
"\tfor example : proxychains telnet somehost.com\n" "More help in README file\n\n", argv[0]);
|
2012-01-24 15:26:37 +08:00
|
|
|
return EXIT_FAILURE;
|
2011-02-25 20:21:34 +08:00
|
|
|
}
|
|
|
|
|
2012-01-28 02:31:01 +08:00
|
|
|
static const char *dll_name = DLL_NAME;
|
2011-11-06 21:11:36 +08:00
|
|
|
|
2011-11-07 00:47:44 +08:00
|
|
|
static char own_dir[256];
|
2012-01-28 01:59:44 +08:00
|
|
|
static const char *dll_dirs[] = {
|
2015-05-21 20:46:17 +08:00
|
|
|
#ifndef SUPER_SECURE /* CVE-2015-3887 */
|
2011-11-06 21:11:36 +08:00
|
|
|
".",
|
2015-05-21 20:46:17 +08:00
|
|
|
#endif
|
2011-11-07 00:47:44 +08:00
|
|
|
own_dir,
|
2011-11-06 21:11:36 +08:00
|
|
|
LIB_DIR,
|
|
|
|
"/lib",
|
|
|
|
"/usr/lib",
|
|
|
|
"/usr/local/lib",
|
|
|
|
"/lib64",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
static void set_own_dir(const char *argv0) {
|
2011-11-07 00:47:44 +08:00
|
|
|
size_t l = strlen(argv0);
|
2012-01-28 01:59:44 +08:00
|
|
|
while(l && argv0[l - 1] != '/')
|
|
|
|
l--;
|
2019-11-06 06:45:52 +08:00
|
|
|
if(l == 0 || l >= sizeof(own_dir))
|
2015-05-21 20:46:17 +08:00
|
|
|
#ifdef SUPER_SECURE
|
2015-05-21 21:04:10 +08:00
|
|
|
memcpy(own_dir, "/dev/null/", 11);
|
2015-05-21 20:46:17 +08:00
|
|
|
#else
|
2011-11-07 00:47:44 +08:00
|
|
|
memcpy(own_dir, ".", 2);
|
2015-05-21 20:46:17 +08:00
|
|
|
#endif
|
2011-11-07 00:47:44 +08:00
|
|
|
else {
|
|
|
|
memcpy(own_dir, argv0, l - 1);
|
|
|
|
own_dir[l] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-28 00:50:04 +08:00
|
|
|
#define MAX_COMMANDLINE_FLAGS 2
|
|
|
|
|
2011-09-04 07:45:16 +08:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
char *path = NULL;
|
|
|
|
char buf[256];
|
|
|
|
char pbuf[256];
|
2011-09-11 05:05:07 +08:00
|
|
|
int start_argv = 1;
|
2012-01-24 15:26:37 +08:00
|
|
|
int quiet = 0;
|
2012-01-26 12:11:52 +08:00
|
|
|
size_t i;
|
2012-01-28 01:59:44 +08:00
|
|
|
const char *prefix = NULL;
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2018-02-11 22:58:26 +08:00
|
|
|
if(argc == 2 && !strcmp(argv[1], "--help"))
|
|
|
|
return usage(argv);
|
|
|
|
|
2012-01-28 00:50:04 +08:00
|
|
|
for(i = 0; i < MAX_COMMANDLINE_FLAGS; i++) {
|
2012-01-26 12:11:52 +08:00
|
|
|
if(start_argv < argc && argv[start_argv][0] == '-') {
|
|
|
|
if(argv[start_argv][1] == 'q') {
|
2012-01-24 15:26:37 +08:00
|
|
|
quiet = 1;
|
|
|
|
start_argv++;
|
2012-01-26 12:11:52 +08:00
|
|
|
} else if(argv[start_argv][1] == 'f') {
|
2012-01-28 00:55:37 +08:00
|
|
|
|
|
|
|
if(start_argv + 1 < argc)
|
2012-01-26 12:11:52 +08:00
|
|
|
path = argv[start_argv + 1];
|
2012-01-28 00:55:37 +08:00
|
|
|
else
|
2012-01-26 12:11:52 +08:00
|
|
|
return usage(argv);
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2012-01-24 15:26:37 +08:00
|
|
|
start_argv += 2;
|
2011-09-04 07:45:16 +08:00
|
|
|
}
|
2012-01-28 00:55:37 +08:00
|
|
|
} else
|
2012-01-26 12:11:52 +08:00
|
|
|
break;
|
2011-09-04 07:45:16 +08:00
|
|
|
}
|
2012-01-26 12:11:52 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
if(start_argv >= argc)
|
|
|
|
return usage(argv);
|
2011-02-25 20:21:34 +08:00
|
|
|
|
2011-09-11 04:32:27 +08:00
|
|
|
/* check if path of config file has not been passed via command line */
|
2012-07-09 05:47:56 +08:00
|
|
|
path = get_config_path(path, pbuf, sizeof(pbuf));
|
2014-07-10 01:16:33 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
if(!quiet)
|
|
|
|
fprintf(stderr, LOG_PREFIX "config file found: %s\n", path);
|
2011-02-25 20:21:34 +08:00
|
|
|
|
2011-09-04 07:45:16 +08:00
|
|
|
/* Set PROXYCHAINS_CONF_FILE to get proxychains lib to use new config file. */
|
2011-09-11 04:32:27 +08:00
|
|
|
setenv(PROXYCHAINS_CONF_FILE_ENV_VAR, path, 1);
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
if(quiet)
|
|
|
|
setenv(PROXYCHAINS_QUIET_MODE_ENV_VAR, "1", 1);
|
2011-11-06 21:11:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
// search DLL
|
|
|
|
|
2019-11-05 11:39:46 +08:00
|
|
|
Dl_info dli;
|
|
|
|
dladdr(own_dir, &dli);
|
|
|
|
set_own_dir(dli.dli_fname);
|
2014-07-10 01:16:33 +08:00
|
|
|
|
2012-11-04 13:14:33 +08:00
|
|
|
i = 0;
|
2011-11-07 00:47:44 +08:00
|
|
|
|
2011-11-06 21:11:36 +08:00
|
|
|
while(dll_dirs[i]) {
|
|
|
|
snprintf(buf, sizeof(buf), "%s/%s", dll_dirs[i], dll_name);
|
|
|
|
if(access(buf, R_OK) != -1) {
|
|
|
|
prefix = dll_dirs[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!prefix) {
|
2012-01-24 15:26:37 +08:00
|
|
|
fprintf(stderr, "couldnt locate %s\n", dll_name);
|
2011-11-06 21:11:36 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2012-01-28 01:59:44 +08:00
|
|
|
if(!quiet)
|
|
|
|
fprintf(stderr, LOG_PREFIX "preloading %s/%s\n", prefix, dll_name);
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2014-07-10 00:07:24 +08:00
|
|
|
#ifdef IS_MAC
|
2012-01-28 03:48:24 +08:00
|
|
|
putenv("DYLD_FORCE_FLAT_NAMESPACE=1");
|
2014-07-10 00:07:24 +08:00
|
|
|
#define LD_PRELOAD_ENV "DYLD_INSERT_LIBRARIES"
|
2014-07-10 01:12:30 +08:00
|
|
|
#define LD_PRELOAD_SEP ":"
|
2014-07-10 00:07:24 +08:00
|
|
|
#else
|
|
|
|
#define LD_PRELOAD_ENV "LD_PRELOAD"
|
2014-07-10 01:12:30 +08:00
|
|
|
/* all historic implementations of BSD and linux dynlinkers seem to support
|
|
|
|
space as LD_PRELOAD separator, with colon added only recently.
|
|
|
|
we use the old syntax for maximum compat */
|
|
|
|
#define LD_PRELOAD_SEP " "
|
2012-01-28 03:48:24 +08:00
|
|
|
#endif
|
2014-07-10 01:12:30 +08:00
|
|
|
char *old_val = getenv(LD_PRELOAD_ENV);
|
|
|
|
snprintf(buf, sizeof(buf), LD_PRELOAD_ENV "=%s/%s%s%s",
|
|
|
|
prefix, dll_name,
|
|
|
|
/* append previous LD_PRELOAD content, if existent */
|
|
|
|
old_val ? LD_PRELOAD_SEP : "",
|
|
|
|
old_val ? old_val : "");
|
2014-07-10 00:07:24 +08:00
|
|
|
putenv(buf);
|
2011-09-11 05:05:07 +08:00
|
|
|
execvp(argv[start_argv], &argv[start_argv]);
|
2022-01-10 19:44:26 +08:00
|
|
|
fprintf(stderr, "proxychains: can't load process '%s'.", argv[start_argv]);
|
|
|
|
perror(" (hint: it's probably a typo)");
|
2011-02-25 20:21:34 +08:00
|
|
|
|
2011-09-11 04:32:27 +08:00
|
|
|
return EXIT_FAILURE;
|
2011-02-25 17:40:11 +08:00
|
|
|
}
|