Skip to content

Commit

Permalink
Wire up referrer support
Browse files Browse the repository at this point in the history
  • Loading branch information
brad-sp committed Jun 15, 2015
1 parent 9683f7b commit 7282fef
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 28 deletions.
2 changes: 1 addition & 1 deletion alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static __inline void *cm_calloc(size_t count, size_t size)
}
#endif

static __inline char *cm_strdup(char *ptr)
static __inline char *cm_strdup(const char *ptr)
{
char *buf = cm_alloc(strlen(ptr) + 1);
if (buf)
Expand Down
13 changes: 11 additions & 2 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
int read_config(void)
{
// TODO unicode support
char buf[512], config_fname[MAX_PATH];
char buf[32768], config_fname[MAX_PATH];
FILE *fp;
unsigned int i;
unsigned int vallen;
Expand All @@ -40,6 +40,7 @@ int read_config(void)
}

g_config.force_sleepskip = -1;
memset(buf, 0, sizeof(buf));
while (fgets(buf, sizeof(buf), fp) != NULL)
{
// cut off the newline
Expand Down Expand Up @@ -89,15 +90,23 @@ int read_config(void)
}
else {
// is a URL
wchar_t *utmp = calloc(1, 512 * sizeof(wchar_t));
unsigned int url_len = (unsigned int)strlen(value);
wchar_t *utmp = calloc(1, (url_len + 1) * sizeof(wchar_t));
for (i = 0; i < url_len; i++)
utmp[i] = (wchar_t)(unsigned short)value[i];
g_config.url_of_interest = utmp;
g_config.suspend_logging = TRUE;
}
}
}
else if (!strcmp(key, "referrer")) {
unsigned int ref_len = (unsigned int)strlen(value);
wchar_t *rtmp = calloc(1, (ref_len + 1) * sizeof(wchar_t));
for (i = 0; i < ref_len; i++)
rtmp[i] = (wchar_t)(unsigned short)value[i];
g_config.w_referrer = rtmp;
g_config.referrer = strdup(value);
}
else if (!strcmp(key, "analyzer")) {
strncpy(g_config.analyzer, value,
ARRAYSIZE(g_config.analyzer)-2);
Expand Down
4 changes: 4 additions & 0 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ struct _g_config {
// URL of interest
wchar_t *url_of_interest;

// Referrer for initial URL request
wchar_t *w_referrer;
char *referrer;

// if this mutex exists then we're shutting down
char shutdown_mutex[MAX_PATH];

Expand Down
54 changes: 39 additions & 15 deletions hook_network.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,38 +299,62 @@ HOOKDEF(HINTERNET, WINAPI, InternetOpenUrlW,
return ret;
}

static did_initial_request;

HOOKDEF(HINTERNET, WINAPI, HttpOpenRequestA,
__in HINTERNET hConnect,
__in LPCTSTR lpszVerb,
__in LPCTSTR lpszObjectName,
__in LPCTSTR lpszVersion,
__in LPCTSTR lpszReferer,
__in LPCTSTR *lplpszAcceptTypes,
__in LPCSTR lpszVerb,
__in LPCSTR lpszObjectName,
__in LPCSTR lpszVersion,
__in LPCSTR lpszReferer,
__in LPCSTR *lplpszAcceptTypes,
__in DWORD dwFlags,
__in DWORD_PTR dwContext
) {
HINTERNET ret = Old_HttpOpenRequestA(hConnect, lpszVerb, lpszObjectName,
lpszVersion, lpszReferer, lplpszAcceptTypes, dwFlags, dwContext);
HINTERNET ret;
LPCSTR referer;

if (lpszReferer == NULL && g_config.url_of_interest && g_config.referrer && strlen(g_config.referrer))
referer = g_config.referrer;
else
referer = lpszReferer;

ret = Old_HttpOpenRequestA(hConnect, lpszVerb, lpszObjectName,
lpszVersion, referer, lplpszAcceptTypes, dwFlags, dwContext);
LOQ_nonnull("network", "psh", "InternetHandle", hConnect, "Path", lpszObjectName,
"Flags", dwFlags);

did_initial_request = TRUE;

return ret;
}

HOOKDEF(HINTERNET, WINAPI, HttpOpenRequestW,
__in HINTERNET hConnect,
__in LPWSTR lpszVerb,
__in LPWSTR lpszObjectName,
__in LPWSTR lpszVersion,
__in LPWSTR lpszReferer,
__in LPWSTR *lplpszAcceptTypes,
__in LPCWSTR lpszVerb,
__in LPCWSTR lpszObjectName,
__in LPCWSTR lpszVersion,
__in LPCWSTR lpszReferer,
__in LPCWSTR *lplpszAcceptTypes,
__in DWORD dwFlags,
__in DWORD_PTR dwContext
) {
HINTERNET ret = Old_HttpOpenRequestW(hConnect, lpszVerb, lpszObjectName,
lpszVersion, lpszReferer, lplpszAcceptTypes, dwFlags, dwContext);
HINTERNET ret;
LPCWSTR referer;

if (lpszReferer == NULL && g_config.url_of_interest && g_config.w_referrer && wcslen(g_config.w_referrer))
referer = g_config.w_referrer;
else
referer = lpszReferer;

ret = Old_HttpOpenRequestW(hConnect, lpszVerb, lpszObjectName,
lpszVersion, referer, lplpszAcceptTypes, dwFlags, dwContext);
LOQ_nonnull("network", "puh", "InternetHandle", hConnect, "Path", lpszObjectName,
"Flags", dwFlags);
return ret;

did_initial_request = TRUE;

return ret;
}

HOOKDEF(BOOL, WINAPI, HttpSendRequestA,
Expand Down
20 changes: 10 additions & 10 deletions hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -1474,22 +1474,22 @@ extern HOOKDEF(BOOL, WINAPI, InternetCrackUrlW,

extern HOOKDEF(HINTERNET, WINAPI, HttpOpenRequestA,
__in HINTERNET hConnect,
__in LPCTSTR lpszVerb,
__in LPCTSTR lpszObjectName,
__in LPCTSTR lpszVersion,
__in LPCTSTR lpszReferer,
__in LPCTSTR *lplpszAcceptTypes,
__in LPCSTR lpszVerb,
__in LPCSTR lpszObjectName,
__in LPCSTR lpszVersion,
__in LPCSTR lpszReferer,
__in LPCSTR *lplpszAcceptTypes,
__in DWORD dwFlags,
__in DWORD_PTR dwContext
);

extern HOOKDEF(HINTERNET, WINAPI, HttpOpenRequestW,
__in HINTERNET hConnect,
__in LPWSTR lpszVerb,
__in LPWSTR lpszObjectName,
__in LPWSTR lpszVersion,
__in LPWSTR lpszReferer,
__in LPWSTR *lplpszAcceptTypes,
__in LPCWSTR lpszVerb,
__in LPCWSTR lpszObjectName,
__in LPCWSTR lpszVersion,
__in LPCWSTR lpszReferer,
__in LPCWSTR *lplpszAcceptTypes,
__in DWORD dwFlags,
__in DWORD_PTR dwContext
);
Expand Down

0 comments on commit 7282fef

Please sign in to comment.