1
0
mirror of https://github.com/djhackersdev/bemanitools.git synced 2025-01-19 07:17:23 +01:00

hooklib: add adapter_hook_override to allow specifying network adapter id

This commit is contained in:
Will Xyen 2020-01-28 04:14:00 -05:00
parent fbb7731681
commit a21e37c894
5 changed files with 108 additions and 0 deletions

View File

@ -4,6 +4,7 @@ src_hooklib := \
acp.c \
app.c \
adapter.c \
config-adapter.c \
rs232.c \
setupapi.c \

View File

@ -3,12 +3,16 @@
#include <windows.h>
#include <winsock2.h>
#include <stdbool.h>
#include <string.h>
#include "hook/table.h"
#include "hooklib/adapter.h"
#include "util/codepage.h"
#include "util/defs.h"
#include "util/log.h"
static DWORD WINAPI
my_GetAdaptersInfo(PIP_ADAPTER_INFO adapter_info, PULONG out_buf_len);
@ -16,6 +20,9 @@ my_GetAdaptersInfo(PIP_ADAPTER_INFO adapter_info, PULONG out_buf_len);
static DWORD(WINAPI *real_GetAdaptersInfo)(
PIP_ADAPTER_INFO adapter_info, PULONG out_buf_len);
static IP_ADDRESS_STRING override_address;
static bool use_address_override;
static const struct hook_symbol adapter_hook_syms[] = {
{.name = "GetAdaptersInfo",
.patch = my_GetAdaptersInfo,
@ -37,6 +44,24 @@ my_GetAdaptersInfo(PIP_ADAPTER_INFO adapter_info, PULONG out_buf_len)
return ret;
}
info = adapter_info;
if (use_address_override) {
while (info) {
// this is well defined to be at most sizeof(IP_ADDRESS_STRING)
// and NULL filled if shorter (hence the memset in adapter_hook_override)
if (!memcmp(info->IpAddressList.IpAddress.String, override_address.String, sizeof(IP_ADDRESS_STRING))) {
log_info("%s: using [override] adapter: %s, %s, %s, %s", __FUNCTION__, info->AdapterName, info->Description, info->IpAddressList.IpAddress.String, info->IpAddressList.IpMask.String);
// copy only this adapter over
memcpy(adapter_info, info, sizeof(*info));
adapter_info->Next = 0;
return ret;
}
info = info->Next;
}
}
ip_fwd_table = (MIB_IPFORWARDTABLE *) malloc(sizeof(MIB_IPFORWARDTABLE));
table_size = 0;
@ -58,6 +83,9 @@ my_GetAdaptersInfo(PIP_ADAPTER_INFO adapter_info, PULONG out_buf_len)
while (info) {
if (info->Index == best_adapter) {
log_info("%s: using [best] adapter: %s, %s, %s, %s", __FUNCTION__, info->AdapterName, info->Description, info->IpAddressList.IpAddress.String, info->IpAddressList.IpMask.String);
// copy only this adapter over
memcpy(adapter_info, info, sizeof(*info));
adapter_info->Next = 0;
return ret;
@ -73,3 +101,24 @@ void adapter_hook_init(void)
hook_table_apply(
NULL, "iphlpapi.dll", adapter_hook_syms, lengthof(adapter_hook_syms));
}
void adapter_hook_override(const char* adapter_address)
{
// starts off false anyways due to static
// but in case it gets called multiple times, set it anyways
use_address_override = false;
if (adapter_address == NULL || *adapter_address == NULL) {
// empty, do nothing
return;
}
if (strlen(adapter_address) > sizeof(IP_ADDRESS_STRING)) {
log_warning("%s: %s is not an ipv4 address", __FUNCTION__, adapter_address);
return;
}
memset(override_address.String, 0, sizeof(IP_ADDRESS_STRING));
memcpy(override_address.String, adapter_address, sizeof(IP_ADDRESS_STRING));
use_address_override = true;
}

View File

@ -3,4 +3,11 @@
void adapter_hook_init(void);
/**
* Uses the provided address to try and match a network adapter
*
* @param network ip to match
*/
void adapter_hook_override(const char* adapter_address);
#endif

View File

@ -0,0 +1,34 @@
#include "cconfig/cconfig-util.h"
#include "hooklib/config-adapter.h"
#include "util/log.h"
#define HOOKLIB_CONFIG_ADAPTER_OVERRIDE_IP_KEY "adapter.override_ip"
#define HOOKLIB_CONFIG_ADAPTER_DEFAULT_OVERRIDE_IP_VALUE ""
void hooklib_config_adapter_init(struct cconfig *config)
{
cconfig_util_set_str(
config,
HOOKLIB_CONFIG_ADAPTER_OVERRIDE_IP_KEY,
HOOKLIB_CONFIG_ADAPTER_DEFAULT_OVERRIDE_IP_VALUE,
"IP of adapter to force override with");
}
void hooklib_config_adapter_get(struct hooklib_config_adapter *config_adapter, struct cconfig *config)
{
if (!cconfig_util_get_str(
config,
HOOKLIB_CONFIG_ADAPTER_OVERRIDE_IP_KEY,
config_adapter->override_ip,
sizeof(config_adapter->override_ip),
HOOKLIB_CONFIG_ADAPTER_DEFAULT_OVERRIDE_IP_VALUE)) {
log_warning(
"Invalid value for key '%s' specified, fallback "
"to default '%s'",
HOOKLIB_CONFIG_ADAPTER_OVERRIDE_IP_KEY,
HOOKLIB_CONFIG_ADAPTER_DEFAULT_OVERRIDE_IP_VALUE);
}
}

View File

@ -0,0 +1,17 @@
#ifndef HOOKLIB_CONFIG_ADAPTER_H
#define HOOKLIB_CONFIG_ADAPTER_H
#include <windows.h>
#include "cconfig/cconfig.h"
struct hooklib_config_adapter {
// this is larger on purpose, in case ppl enter the wrong stuff here
char override_ip[32];
};
void hooklib_config_adapter_init(struct cconfig *config);
void hooklib_config_adapter_get(struct hooklib_config_adapter *config_adapter, struct cconfig *config);
#endif