-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwinhttpdiag.cpp
1245 lines (1145 loc) · 38.3 KB
/
winhttpdiag.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// WinHttpDiag.cpp
//
#include "stdafx.h"
#include "string.h"
#include "Winhttp.h"
#pragma comment(lib, "winhttp.lib")
void GetHost(WCHAR* pwszUrl, WCHAR* pwszHost, WCHAR* pwszPath, WCHAR* pwszExtraInfo, INTERNET_PORT* port, INTERNET_SCHEME* nScheme );
void print_time(void);
DWORD ErrorPrint();
//errorstr.cpp
LPCTSTR ErrorString(DWORD dwLastError);
void PrintAutoProxyOptions(WINHTTP_AUTOPROXY_OPTIONS* pAutoProxyOptions);
BOOL SetProxyInfo(HINTERNET hRequest, WINHTTP_PROXY_INFO* pProxyInfo, DWORD cbProxyInfoSize);
BOOL QueryProxyInfo(HINTERNET hRequest, WINHTTP_PROXY_INFO* pProxyInfo, DWORD cbProxyInfoSize);
void ShowProxyInfo(WINHTTP_PROXY_INFO* pProxyInfo, DWORD cbProxyInfoSize);
BOOL ShowIEProxyConfigForCurrentUser();
BOOL ResetAll(HINTERNET hHttpSession,DWORD dwFlags);
//
// maximum field lengths (arbitrary) from wininet.h
//
#define INTERNET_MAX_HOST_NAME_LENGTH 256
#define INTERNET_MAX_USER_NAME_LENGTH 128
#define INTERNET_MAX_PASSWORD_LENGTH 128
#define INTERNET_MAX_PORT_NUMBER_LENGTH 5 // INTERNET_PORT is unsigned short
#define INTERNET_MAX_PORT_NUMBER_VALUE 65535 // maximum unsigned short value
#define INTERNET_MAX_PATH_LENGTH 2048
#define INTERNET_MAX_SCHEME_LENGTH 32 // longest protocol name length
#define INTERNET_MAX_URL_LENGTH (INTERNET_MAX_SCHEME_LENGTH \
+ sizeof("://") \
+ INTERNET_MAX_PATH_LENGTH)
WCHAR Version[5] = L"1.23";
WCHAR wszWinHTTPDiagVersion[32] = L"WinHTTPDiag version ";
WINHTTP_CURRENT_USER_IE_PROXY_CONFIG IEProxyConfig;
void DisplayHelp()
{
printf("Tool to diagnose proxy issues when using WinHTTP\n");
printf("CLR checking uses CrytoAPI2 (CAPI2) which uses WinHTTP\n");
printf("Usage : WinHTTPDiag [-?] [-a] [-n] [-d] [-i] [-r] [-p proxy] [url]\n");
printf("Using WinHttpGetIEProxyConfigForCurrentUser by default\n");
printf("-? : Displays help\n");
printf("-n : Forces not using WinHttpGetIEProxyConfigForCurrentUser results when calling WinHttpGetProxyForUrl\n");
printf("-w : Simulating discovery of WPAD using DHCP and DNS\n");
printf("-a : Using WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY flag (Windows 8.1 and above only). Used by CryptoAPI 2 (CAPI2) on Windows 10.\n");
printf("-d : Displays the default WinHTTP proxy configuration from the registry using WinHttpGetDefaultProxyConfiguration which will be used with -n option\n");
printf("-i : Displays the proxy configuration using WinHttpGetIEProxyConfigForCurrentUser\n");
printf("-r : resetting auto-proxy caching using WinHttpResetAutoProxy with WINHTTP_RESET_ALL and WINHTTP_RESET_OUT_OF_PROC flags. Windows 8.0 and above only!\n");
printf("-z : resetting auto-proxy caching using WinHttpResetAutoProxy with WINHTTP_RESET_ALL and WINHTTP_RESET_DISCARD_RESOLVERS and WINHTTP_RESET_OUT_OF_PROC flags. Windows 10.0 and above only!\n");
printf("-p proxy: forcing usage of static proxy (requires an url)\n");
printf("-c PAC file url: forcing usage of a PAC file (requires an url)\n");
printf("-f : forcing DIRECT(requires an url).Useful for checking access to a PAC file bypassing usage of a PAC file when there is one in the IE settings\n");
printf("url : url to use in WinHttpSendRequest (using http://crl.microsoft.com/pki/crl/products/CodeSignPCA.crl if none given)\n");
printf("You can use psexec (http://live.sysinternals.com) -s to run WinHTTPDiag using the System (S-1-5-18) account: psexec -s c:\\tools\\WinHTTPDiag.exe\n");
printf("You can use psexec -u \"NT AUTHORITY\\LOCALSERVICE\" to run WinHTTPDiag using the Local Service (S-1-5-19) account\n");
printf("You can use psexec -u \"NT AUTHORITY\\NETWORKSERVICE\" to run WinHTTPDiag using the Network Service (S-1-5-20) account\n");
}
int _tmain(int argc, _TCHAR* argv[])
{
HINTERNET hHttpSession = NULL;
HINTERNET hConnect = NULL;
HINTERNET hRequest = NULL;
DWORD dwSize = 0;
LPVOID lpOutBuffer = NULL;
BOOL bResults = FALSE;
WINHTTP_AUTOPROXY_OPTIONS AutoProxyOptions;
WINHTTP_PROXY_INFO ProxyInfo;
DWORD cbProxyInfoSize = sizeof(ProxyInfo);
ZeroMemory(&AutoProxyOptions, sizeof(AutoProxyOptions));
ZeroMemory(&ProxyInfo, sizeof(ProxyInfo));
WCHAR DefaultUrl[INTERNET_MAX_URL_LENGTH] = L"http://crl.microsoft.com/pki/crl/products/CodeSignPCA.crl";
WCHAR url[INTERNET_MAX_URL_LENGTH] = L"";
WCHAR NamedProxy[INTERNET_MAX_URL_LENGTH] = L""; //1.17
WCHAR NamedPacFileUrl[INTERNET_MAX_URL_LENGTH] = L""; //1.17
BOOL bGetIEProxyConfigForCurrentUser = TRUE;
BOOL fResult = TRUE;
WINHTTP_PROXY_INFO* pProxyInfo = NULL;
//Banner
wcscat_s(wszWinHTTPDiagVersion, Version);
wprintf(L"%s by [email protected]\n", wszWinHTTPDiagVersion);
BOOL fTryAutoProxy = FALSE;
BOOL fTryAutoConfig = FALSE; //1.03
BOOL fTryStaticProxy = FALSE; //1.04
BOOL fResetAll = FALSE; //1.05
BOOL fUseAutomaticProxyFlag = FALSE; //1.14
BOOL fSuccess = FALSE;
BOOL bInvalidParameterRetry = TRUE; //1.16
BOOL bNamedProxy = FALSE; //1.17
BOOL bNamedPacFile = FALSE; //1.20
BOOL bDirect = FALSE; //1.21
BOOL fResetAllDiscard = FALSE; //1.23
ZeroMemory(&ProxyInfo, sizeof(ProxyInfo));
ZeroMemory(&AutoProxyOptions, sizeof(AutoProxyOptions));
ZeroMemory(&IEProxyConfig, sizeof(IEProxyConfig));
//no arguments
if (argc == 1)
{
wcscpy_s(url, DefaultUrl);
printf("Using url: %S in WinHttpSendRequest\n", url);
}
//1 argument
else if (argc == 2)
{
if ((argv[1][0] == '-') || (argv[1][0] == '/'))
{
if ((argv[1][1] == 'w'))
{
printf("\nForcing auto proxy detection : searching for WPAD using DHCP and if it fails DNS\n");
fTryAutoProxy = TRUE;
wcscpy_s(url, DefaultUrl);
printf("Using url: %S in WinHttpSendRequest\n", url);
goto winhttpopen;
}
if ((argv[1][1] == 'r'))
{
fResetAll = TRUE;
goto winhttpopen;
}
if ((argv[1][1] == 'z'))
{
fResetAllDiscard = TRUE;
goto winhttpopen;
}
if ((argv[1][1] == 'd'))
{
printf("Displaying the default WinHTTP proxy configuration from the registry using WinHttpGetDefaultProxyConfiguration\n");
if (WinHttpGetDefaultProxyConfiguration(&ProxyInfo))
{
ShowProxyInfo(&ProxyInfo, cbProxyInfoSize);
}
else
{
printf("<-WinHttpGetDefaultProxyConfiguration failed");
ErrorPrint();
}
exit(0L);
}
if ((argv[1][1] == 'i'))
{
ShowIEProxyConfigForCurrentUser();
exit(0L);
}
if ((argv[1][1] == '?') || (argv[1][1] != 'n'))
{
DisplayHelp();
exit(-1);
}
bGetIEProxyConfigForCurrentUser = FALSE;
printf("Not using WinHttpGetIEProxyConfigForCurrentUser results to call WinHttpGetProxyForUrl\n");
wcscpy_s(url, DefaultUrl);
printf("Using url: %S in WinHttpSendRequest\n", url);
}
else
{
wcscpy_s(url, argv[1]);
printf("Using url: %S in WinHttpSendRequest\n", url);
}
}
else if (argc == 3)
{
if ((argv[1][0] == '-') || (argv[1][0] == '/'))
{
if (argv[1][1] == 'n')
{
bGetIEProxyConfigForCurrentUser = FALSE;
printf("Not using WinHttpGetIEProxyConfigForCurrentUser to call WinHttpGetProxyForUrl\n");
wcscpy_s(url, argv[2]);
printf("Using url: %S in WinHttpSendRequest\n", url);
//1.18
printf("Displaying the default WinHTTP proxy configuration from the registry using WinHttpGetDefaultProxyConfiguration\n");
if (WinHttpGetDefaultProxyConfiguration(&ProxyInfo))
{
ShowProxyInfo(&ProxyInfo, cbProxyInfoSize);
}
else
{
printf("<-WinHttpGetDefaultProxyConfiguration failed");
ErrorPrint();
}
}
else if (argv[1][1] == 'a')
{
fUseAutomaticProxyFlag = TRUE;
bGetIEProxyConfigForCurrentUser = FALSE;
printf("Using WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY flag (Windows 8.1 and above only)\n");
wcscpy_s(url, argv[2]);
printf("Using url: %S in WinHttpSendRequest\n", url);
}
else if (argv[1][1] == 'f')
{
bDirect = TRUE;
fUseAutomaticProxyFlag = FALSE;
printf("Trying to use DIRECT access\n");
wcscpy_s(url, argv[2]);
printf("Using url: %S in WinHttpSendRequest\n", url);
goto winhttpopen;
}
else
{
DisplayHelp();
exit(-1);
}
}
}
else if (argc == 4)
{
if ((argv[1][0] == '-') || (argv[1][0] == '/'))
{
if (argv[1][1] == 'p')
{
bNamedProxy = TRUE;
wcscpy_s(NamedProxy, argv[2]);
printf("Using proxy: %S \n", NamedProxy);
wcscpy_s(url, argv[3]);
printf("Using url: %S in WinHttpSendRequest\n", url);
}
else if (argv[1][1] == 'c')
{
bNamedPacFile = TRUE;
fTryAutoConfig = TRUE;
wcscpy_s(NamedPacFileUrl, argv[2]);
printf("Using PAC file: %S \n", NamedPacFileUrl);
AutoProxyOptions.lpszAutoConfigUrl = NamedPacFileUrl;
wcscpy_s(url, argv[3]);
printf("Using url: %S in WinHttpSendRequest\n", url);
}
else
{
DisplayHelp();
exit(-1);
}
}
}
else
{
DisplayHelp();
exit(-1);
}
if ((bNamedProxy == TRUE) || (bNamedPacFile == TRUE) )
{
goto winhttpopen;
}
if (bGetIEProxyConfigForCurrentUser == TRUE)
{
if (ShowIEProxyConfigForCurrentUser())
{
if (IEProxyConfig.fAutoDetect)
{
fTryAutoProxy = TRUE;
}
if (IEProxyConfig.lpszAutoConfigUrl)
{
// fTryAutoProxy = FALSE; //version 1.02 -> version 1.04 AUtodetect should fallback to autoconfig in case of failure
fTryAutoConfig = TRUE; //Version 1.03
}
if (IEProxyConfig.lpszProxy)
{
//fTryAutoProxy = FALSE; fallback version 1.04
fTryStaticProxy = TRUE; //1.04
}
if (IEProxyConfig.lpszProxyBypass)
{
ProxyInfo.lpszProxyBypass = IEProxyConfig.lpszProxyBypass;
}
printf("\t\tSetting fAutoLogonIfChallenged in WINHTTP_AUTOPROXY_OPTIONS to TRUE\n");
printf("\t\t->The client's domain credentials will automatically be sent in response to an authentication challenge\n");
printf("\t\t when WinHTTP requests the PAC file\n");
AutoProxyOptions.fAutoLogonIfChallenged = TRUE;
}
else
{
// WinHttpingGetIEProxyForCurrentUser failed, try autodetection anyway...
AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;
fTryAutoProxy = TRUE;
printf("WinHttpingGetIEProxyForCurrentUser failed. Trying autoproxy anyway\n");
printf("\t Using WINHTTP_AUTOPROXY_AUTO_DETECT\n");
}
}
else if (!fUseAutomaticProxyFlag)
{
// 1.05. Retrieves the default proxy configuration.
printf("Retrieving the default proxy configuration\n ");
printf("\n->Calling WinHttpGetDefaultProxyConfiguration\n");
if (WinHttpGetDefaultProxyConfiguration(&ProxyInfo))
{
printf("<-WinHttpGetDefaultProxyConfiguration success\n");
// Display the proxy servers and free memory
// allocated to this string.
if (ProxyInfo.lpszProxy != NULL)
{
printf("\tDefault Proxy Configuration Proxy server list: %S\n", ProxyInfo.lpszProxy);
fTryStaticProxy = TRUE;
//GlobalFree(ProxyInfo.lpszProxy); -> end of prog?crackhost
}
else
{
printf("\tDefault Proxy Configuration Proxy server list is empty\n");
}
// Displays the bypass list and free memory allocated to this string.
if (ProxyInfo.lpszProxyBypass != NULL)
{
printf("\tDefault Proxy Configuration Proxy bypass list: %S\n", ProxyInfo.lpszProxyBypass);
//GlobalFree(ProxyInfo.lpszProxyBypass);
}
else
{
printf("\tDefault Proxy Configuration Proxy server bypass list is empty\n");
}
}
else
{
printf("<-WinHttpGetDefaultProxyConfiguration failed");
ErrorPrint();
AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;
fTryAutoProxy = TRUE;
printf("\t Using WINHTTP_AUTOPROXY_AUTO_DETECT\n");
}
}
//
// Create the WinHTTP session.
//
winhttpopen:
if (!fUseAutomaticProxyFlag)
{
printf("\n->Calling WinHttpOpen with WINHTTP_ACCESS_TYPE_NO_PROXY access type\n");
hHttpSession = WinHttpOpen(wszWinHTTPDiagVersion,
WINHTTP_ACCESS_TYPE_NO_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS,
0);
}
else
{
printf("\n->Calling WinHttpOpen with WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY access type\n");
hHttpSession = WinHttpOpen(wszWinHTTPDiagVersion,
WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS,
0);
}
// Exit if WinHttpOpen failed.
if (!hHttpSession)
{
printf("<-WinHttpOpen failed : ");
ErrorPrint();
goto Exit;
}
printf("<-WinHttpOpen succeeded. hHttpSession : %X \n", (unsigned int)hHttpSession);
if (fResetAll == TRUE)
{
ResetAll(hHttpSession, WINHTTP_RESET_ALL | WINHTTP_RESET_OUT_OF_PROC);
goto Exit;
}
if (fResetAllDiscard == TRUE)
{
ResetAll(hHttpSession, WINHTTP_RESET_ALL | WINHTTP_RESET_DISCARD_RESOLVERS | WINHTTP_RESET_OUT_OF_PROC);
goto Exit;
}
//1.09
WCHAR host[INTERNET_MAX_HOST_NAME_LENGTH] = L"";
WCHAR path[INTERNET_MAX_PATH_LENGTH] = L"";
WCHAR ExtraInfo[INTERNET_MAX_PATH_LENGTH] = L"";
INTERNET_PORT port;
INTERNET_SCHEME nScheme;
GetHost(url, host, path, ExtraInfo, &port, &nScheme);
WCHAR ObjectName[INTERNET_MAX_PATH_LENGTH] = L"";
//Combining path and extra info
if (wcscat_s(ObjectName, INTERNET_MAX_PATH_LENGTH, path))
{
printf("Error copying path %S in ObjectName\r\n", path);
exit(-1L);
}
if (wcscat_s(ObjectName, INTERNET_MAX_PATH_LENGTH, ExtraInfo))
{
printf("Error copying ExtraInfo %S in ObjectName %S\r\n", ExtraInfo, ObjectName);
exit(-1L);
}
//
// Create the WinHTTP connect handle.
//
printf("\n->Calling WinHttpConnect for host : %S", host);
print_time();
hConnect = WinHttpConnect(hHttpSession,
host,
port,
0);
// Exit if WinHttpConnect failed.
if (!hConnect)
{
printf("<-WinHttpConnect failed");
ErrorPrint();
goto Exit;
}
printf("<-WinHttpConnect for host : %S succeeded. hConnect : %X\n", host, (unsigned int)hConnect);
print_time();
//
// Create the HTTP request handle.
//
printf("\n->Calling WinHttpOpenRequest with Object name : %S\r\n", ObjectName);
// Prepare OpenRequest flag
DWORD dwOpenRequestFlag = (INTERNET_SCHEME_HTTPS == nScheme) ?
WINHTTP_FLAG_SECURE : 0;
hRequest = WinHttpOpenRequest(hConnect,
L"GET",
path,
L"HTTP/1.1",
WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
dwOpenRequestFlag);
// Exit if WinHttpOpenRequest failed.
if (!hRequest)
{
printf("<-WinHttpOpenRequest failed");
ErrorPrint();
goto Exit;
}
printf("<-WinHttpOpenRequest succeeded. hrequest=%X", (unsigned int)hRequest);
print_time();
if (fUseAutomaticProxyFlag)
{
goto sendrequest;
}
// Use DHCP and DNS-based auto-detection.
BOOL bTryWithDNS = FALSE;
if (fTryAutoProxy)
{
AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT; //version 1.04
//First try with DHCP
AutoProxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP;
// If obtaining the PAC script requires NTLM/Negotiate authentication, then automatically supply the client domain credentials.
AutoProxyOptions.fAutoLogonIfChallenged = TRUE;
}
// Call WinHttpGetProxyForUrl with our target URL.
// If auto-proxy succeeds, then set the proxy info on the request handle.
// If auto-proxy fails, ignore the error and attempt to send the HTTP request directly to the target server
// (using the default WINHTTP_ACCESS_TYPE_NO_PROXY configuration, which the requesthandle will inherit from the session).
//
printf("->Setting WINHTTP_AUTOPROXY_RUN_OUTPROCESS_ONLY\n");
//First trying out of process
AutoProxyOptions.dwFlags |= WINHTTP_AUTOPROXY_RUN_OUTPROCESS_ONLY;
if (fTryAutoProxy)
{
Retry:
printf("->Calling WinHttpGetProxyForUrl with following autoproxy options flags:\n");
PrintAutoProxyOptions(&AutoProxyOptions);
print_time();
if (WinHttpGetProxyForUrl(hHttpSession,
url,
&AutoProxyOptions,
&ProxyInfo))
{
printf("<-WinHttpGetProxyForUrl succeeded");
print_time();
printf("\r\n");
// A proxy configuration was found, set it on the request handle
printf("->Calling WinHttpSetOption with following proxy configuration found by WinHttpGetProxyForUrl:\n");
SetProxyInfo(hRequest, &ProxyInfo, cbProxyInfoSize);
print_time();
//1.08
goto sendrequest;
}
else
{
printf("<-WinHttpGetProxyForUrl failed");
DWORD dwError= ErrorPrint();
print_time();
if (dwError == ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT)
{
printf("Could not download the proxy auto-configuration script file: trying to access PAC file directly\n");
AutoProxyOptions.dwFlags ^= WINHTTP_AUTOPROXY_AUTO_DETECT;
AutoProxyOptions.dwFlags ^= WINHTTP_AUTOPROXY_CONFIG_URL;
ProxyInfo.dwAccessType ^= WINHTTP_ACCESS_TYPE_NAMED_PROXY;
ProxyInfo.dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
ProxyInfo.lpszProxy = L"";
printf("->Calling WinHttpSetOption with proxy configuration set to:\n");
SetProxyInfo(hRequest, &ProxyInfo, cbProxyInfoSize);
wcscpy_s(url, AutoProxyOptions.lpszAutoConfigUrl);
fTryAutoProxy = FALSE;
fTryAutoConfig = FALSE; //1.03
fTryStaticProxy = FALSE; //1.04
fResetAll = FALSE; //1.05
fUseAutomaticProxyFlag = FALSE; //1.14
fSuccess = FALSE;
bInvalidParameterRetry = TRUE; //1.16
bNamedProxy = FALSE; //1.17
bNamedPacFile = FALSE; //1.20
bDirect = TRUE; //1.21
goto winhttpopen;
}
if (bTryWithDNS == FALSE)
{
printf("Trying with DNS instead of DHCP\n");
AutoProxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DNS_A;
bTryWithDNS = TRUE;
goto Retry;
}
else
{
if (AutoProxyOptions.dwFlags & WINHTTP_AUTOPROXY_RUN_OUTPROCESS_ONLY)
{
printf("Out of proc only failed for DHCP and DNS. Trying in proc.\n");
AutoProxyOptions.dwFlags ^= WINHTTP_AUTOPROXY_RUN_OUTPROCESS_ONLY;
AutoProxyOptions.dwFlags |= WINHTTP_AUTOPROXY_RUN_INPROCESS;
AutoProxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP;
bTryWithDNS = FALSE;
goto Retry;
}
}
}
}
if (fTryAutoConfig)
{
if (fTryAutoProxy)
{
printf("Falling back to autoconfig\n");
AutoProxyOptions.dwFlags ^= WINHTTP_AUTOPROXY_AUTO_DETECT;;
}
AutoProxyOptions.dwFlags |= WINHTTP_AUTOPROXY_CONFIG_URL;
if (bNamedPacFile == FALSE) //1.20
{
AutoProxyOptions.lpszAutoConfigUrl = IEProxyConfig.lpszAutoConfigUrl;
}
printf("Setting WINHTTP_AUTOPROXY_CONFIG_URL flag in WINHTTP_AUTOPROXY_OPTIONS dwFlags\n");
printf("\t\tlpszAutoConfigUrl: %S\n", IEProxyConfig.lpszAutoConfigUrl);
printf("->Calling WinHttpGetProxyForUrl with following autoconfig options flags:\n");
// 24/07/2015
//04/08/20016 --> causes apparently 0x57/87 ERROR_INVALID_PARAMETER The parameter is incorrect error on Windows7/2008 R2
AutoProxyOptions.dwFlags |= WINHTTP_AUTOPROXY_NO_DIRECTACCESS;
AutoProxyOptions.dwFlags |= WINHTTP_AUTOPROXY_NO_CACHE_CLIENT;
AutoProxyOptions.dwFlags |= WINHTTP_AUTOPROXY_NO_CACHE_SVC;
retryautoconfig:
PrintAutoProxyOptions(&AutoProxyOptions);
print_time();
if (WinHttpGetProxyForUrl(hHttpSession,
url,
&AutoProxyOptions,
&ProxyInfo))
{
printf("<-WinHttpGetProxyForUrl succeeded");
print_time();
printf("\r\n");
// A proxy configuration was found, set it on the
// request handle
printf("->Calling WinHttpSetOption with following proxy configuration found by WinHttpGetProxyForUrl:\n");
SetProxyInfo(hRequest, &ProxyInfo, cbProxyInfoSize);
print_time();
goto sendrequest;
}
else
{
//004082016 1.07
printf("<-WinHttpGetProxyForUrl failed");
DWORD dwError = ErrorPrint();
if (dwError == ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT)
{
printf("Could not download the proxy auto-configuration script file: trying to access PAC file directly\n");
AutoProxyOptions.dwFlags ^= WINHTTP_AUTOPROXY_AUTO_DETECT;
AutoProxyOptions.dwFlags ^= WINHTTP_AUTOPROXY_CONFIG_URL;
ProxyInfo.dwAccessType ^= WINHTTP_ACCESS_TYPE_NAMED_PROXY;
ProxyInfo.dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
ProxyInfo.lpszProxy = L"";
printf("->Calling WinHttpSetOption with proxy configuration set to:\n");
SetProxyInfo(hRequest, &ProxyInfo, cbProxyInfoSize);
wcscpy_s(url, AutoProxyOptions.lpszAutoConfigUrl);
fTryAutoProxy = FALSE;
fTryAutoConfig = FALSE; //1.03
fTryStaticProxy = FALSE; //1.04
fResetAll = FALSE; //1.05
fUseAutomaticProxyFlag = FALSE; //1.14
fSuccess = FALSE;
bInvalidParameterRetry = TRUE; //1.16
bNamedProxy = FALSE; //1.17
bNamedPacFile = FALSE; //1.20
bDirect = TRUE; //1.21
goto winhttpopen;
}
if (dwError == ERROR_INVALID_PARAMETER)
{
if (bInvalidParameterRetry)
{
printf("WinHttpGetProxyForUrl failed with ERROR_INVALID_PARAMETER. Removing some parameters not supported on Windows7/2008R2\n ");
AutoProxyOptions.dwFlags ^= WINHTTP_AUTOPROXY_NO_DIRECTACCESS;
AutoProxyOptions.dwFlags ^= WINHTTP_AUTOPROXY_NO_CACHE_CLIENT;
AutoProxyOptions.dwFlags ^= WINHTTP_AUTOPROXY_NO_CACHE_SVC;
bInvalidParameterRetry = FALSE;
goto retryautoconfig;
}
else
{
printf("WinHttpGetProxyForUrl failed with ERROR_INVALID_PARAMETER.\r\n ");
goto Exit;
}
}
print_time();
if (AutoProxyOptions.dwFlags & WINHTTP_AUTOPROXY_RUN_OUTPROCESS_ONLY)
{
printf("Out of proc only failed. Trying in proc.\n");
AutoProxyOptions.dwFlags ^= WINHTTP_AUTOPROXY_RUN_OUTPROCESS_ONLY;
AutoProxyOptions.dwFlags |= WINHTTP_AUTOPROXY_RUN_INPROCESS;
goto retryautoconfig;
}
}
}
//1.04
if (fTryStaticProxy)
{
if ((fTryAutoProxy) || (fTryAutoConfig))
{
printf("Falling back to named proxy\n");
AutoProxyOptions.dwFlags ^= WINHTTP_AUTOPROXY_AUTO_DETECT;
AutoProxyOptions.dwFlags ^= WINHTTP_AUTOPROXY_CONFIG_URL;
}
if (bGetIEProxyConfigForCurrentUser == TRUE)
{
ProxyInfo.lpszProxy = IEProxyConfig.lpszProxy;
}
printf("\tSetting WINHTTP_AUTOPROXY_ALLOW_STATIC flag in WINHTTP_AUTOPROXY_OPTIONS dwFlags\n");
AutoProxyOptions.dwFlags |= WINHTTP_AUTOPROXY_ALLOW_STATIC;
printf("->Calling WinHttpGetProxyForUrl with following autoconfig options flags:\n");
// 24/07/2015
//04/08/20016 --> causes apparently 0x57/87 ERROR_INVALID_PARAMETER The parameter is incorrect error on Windows7/2008 R2
AutoProxyOptions.dwFlags |= WINHTTP_AUTOPROXY_NO_DIRECTACCESS;
AutoProxyOptions.dwFlags |= WINHTTP_AUTOPROXY_NO_CACHE_CLIENT;
AutoProxyOptions.dwFlags |= WINHTTP_AUTOPROXY_NO_CACHE_SVC;
retrystaticproxy:
PrintAutoProxyOptions(&AutoProxyOptions);
print_time();
if (WinHttpGetProxyForUrl(hHttpSession,
url,
&AutoProxyOptions,
&ProxyInfo))
{
printf("<-WinHttpGetProxyForUrl succeeded");
print_time();
printf("\r\n");
printf("\tNamed proxy configured: %S\n", ProxyInfo.lpszProxy);
printf("Setting dwAccessType to WINHTTP_ACCESS_TYPE_NAMED_PROXY\r\n");
ProxyInfo.dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
if (IEProxyConfig.lpszProxyBypass)
{
ProxyInfo.lpszProxyBypass = IEProxyConfig.lpszProxyBypass;
}
// A proxy configuration was found, set it on the
// request handle
printf("->Calling WinHttpSetOption with following proxy configuration found by WinHttpGetProxyForUrl:\n");
SetProxyInfo(hRequest, &ProxyInfo, cbProxyInfoSize);
print_time();
goto sendrequest;
}
else
{
//004082016 1.07
printf("<-WinHttpGetProxyForUrl failed");
ErrorPrint();
if (GetLastError() == ERROR_INVALID_PARAMETER)
{
if (bInvalidParameterRetry)
{
printf("WinHttpGetProxyForUrl failed with ERROR_INVALID_PARAMETER. Removing some parameters not supported on Windows7/2008R2\n ");
AutoProxyOptions.dwFlags ^= WINHTTP_AUTOPROXY_NO_DIRECTACCESS;
AutoProxyOptions.dwFlags ^= WINHTTP_AUTOPROXY_NO_CACHE_CLIENT;
AutoProxyOptions.dwFlags ^= WINHTTP_AUTOPROXY_NO_CACHE_SVC;
bInvalidParameterRetry = FALSE;
goto retrystaticproxy;
}
else
{
printf("WinHttpGetProxyForUrl failed with ERROR_INVALID_PARAMETER.\r\n ");
goto Exit;
}
}
print_time();
if (AutoProxyOptions.dwFlags & WINHTTP_AUTOPROXY_RUN_OUTPROCESS_ONLY)
{
printf("Out of proc only failed. Trying in proc.\n");
AutoProxyOptions.dwFlags ^= WINHTTP_AUTOPROXY_RUN_OUTPROCESS_ONLY;
AutoProxyOptions.dwFlags |= WINHTTP_AUTOPROXY_RUN_INPROCESS;
goto retrystaticproxy;
}
}
}
else
{
if ((fTryStaticProxy) || (fTryAutoProxy) || (fTryAutoConfig))
{
printf("Falling back to DIRECT/NO_PROXY\n");
}
else
{
printf("\nTrying DIRECT/NO_PROXY\n");
}
AutoProxyOptions.dwFlags ^= WINHTTP_AUTOPROXY_AUTO_DETECT;
AutoProxyOptions.dwFlags ^= WINHTTP_AUTOPROXY_CONFIG_URL;
ProxyInfo.dwAccessType ^= WINHTTP_ACCESS_TYPE_NAMED_PROXY;
printf("\t\t Setting WINHTTP_ACCESS_TYPE_NAMED_PROXY flag in WINHTTP_AUTOPROXY_OPTIONS dwAccessType\n");
printf("\t\tNamed proxy configured: %S\n", IEProxyConfig.lpszProxy);
ProxyInfo.dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
ProxyInfo.lpszProxy = IEProxyConfig.lpszProxy;
printf("->Calling WinHttpSetOption with proxy configuration set to:\n");
SetProxyInfo(hRequest, &ProxyInfo, cbProxyInfoSize);
}
if (bNamedProxy) //1.17
{
ProxyInfo.lpszProxy = NamedProxy;
printf("\tNamed proxy configured: %S\n", ProxyInfo.lpszProxy);
ProxyInfo.dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
printf("->Calling WinHttpSetOption with proxy configuration set to:\n");
SetProxyInfo(hRequest, &ProxyInfo, cbProxyInfoSize);
}
//
// Send the request.
//
sendrequest:
printf("\n->Calling WinHttpSendRequest with hrequest: %X\n", (unsigned int)hRequest);
print_time();
if (!WinHttpSendRequest(hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS,
0,
WINHTTP_NO_REQUEST_DATA,
0,
0,
NULL))
{
// Exit if WinHttpSendRequest failed.
printf("<-WinHttpSendRequest failed\n");
ErrorPrint();
print_time();
printf("\n->Proxy configuration after WinHttpSendRequest:\n");
QueryProxyInfo(hRequest, &ProxyInfo, cbProxyInfoSize);
print_time();
goto Exit;
}
printf("<-WinHttpSendRequest succeeded");
print_time();
printf("\n->Proxy configuration used:\n");
QueryProxyInfo(hRequest, &ProxyInfo, cbProxyInfoSize);
//
// Wait for the response.
//
printf("\n->Calling WinHttpReceiveResponse");
print_time();
// End the request.
bResults = WinHttpReceiveResponse(hRequest, NULL);
if (!bResults)
{
printf("<-WinHttpReceiveResponse");
ErrorPrint();
print_time();
goto Exit;
}
printf("<-WinHttpReceiveResponse succeeded");
print_time();
//
// A response has been received, then process it.
// First, use WinHttpQueryHeaders to obtain the size of the buffer.
WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF,
WINHTTP_HEADER_NAME_BY_INDEX, NULL,
&dwSize, WINHTTP_NO_HEADER_INDEX);
// Allocate memory for the buffer.
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
lpOutBuffer = new WCHAR[dwSize / sizeof(WCHAR)];
// Now, use WinHttpQueryHeaders to retrieve the header.
bResults = WinHttpQueryHeaders(hRequest,
WINHTTP_QUERY_RAW_HEADERS_CRLF,
WINHTTP_HEADER_NAME_BY_INDEX,
lpOutBuffer, &dwSize,
WINHTTP_NO_HEADER_INDEX);
}
// Print the header contents.
if (bResults)
printf("\n\tHeader contents: \n%S", (wchar_t*)lpOutBuffer);
// Free the allocated memory.
delete[] lpOutBuffer;
char buffer[10 * 1024];
DWORD bytesRead;
printf("Calling WinHttpReadData (maximum 10240 bytes)\r\n");
if (WinHttpReadData(hRequest, buffer, sizeof(buffer), &bytesRead))
{
wprintf(L"WinHttpReadData returning %d bytes\r\n", bytesRead);
if (bytesRead > 0)
{
buffer[bytesRead - 1] = '\0';
wprintf(L"%d bytes of buffer \r\n%S\r\n", bytesRead - 1, buffer);
}
}
else
{
printf("WinHttpReadData Error : %X\n", GetLastError());
}
/* 1.09 need to add a command line parameter
printf("Type y if you want to send the request again on same connection\r\n");
printf("or any other character to exit\r\n");
char c;
c = (char)getchar();
if ((c == 'y') || (c == 'Y'))
{
getchar(); //to get cr
goto sendrequest;
}
*/
Exit:
//
// Clean up the WINHTTP_PROXY_INFO structure.
//
if (ProxyInfo.lpszProxy != NULL)
GlobalFree(ProxyInfo.lpszProxy);
if (ProxyInfo.lpszProxyBypass != NULL)
GlobalFree(ProxyInfo.lpszProxyBypass);
//
// Close the WinHTTP handles.
//
if (hRequest != NULL)
WinHttpCloseHandle(hRequest);
if (hConnect != NULL)
WinHttpCloseHandle(hConnect);
if (hHttpSession != NULL)
WinHttpCloseHandle(hHttpSession);
return 0;
}
BOOL ResetAll(HINTERNET hHttpSession, DWORD dwFlags)
{
DWORD dReturn;
typedef
DWORD
(WINAPI
* PFN_WINHTTPRESETAUTOPROXY)(
__in_opt HINTERNET hSession,
__in DWORD dwFlags
);
PFN_WINHTTPRESETAUTOPROXY pfnWinHttpResetAutoProxy = NULL;
HMODULE shWinHttp = LoadLibraryExW(L"winhttp.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
//1.06
if (shWinHttp)
{
pfnWinHttpResetAutoProxy = (PFN_WINHTTPRESETAUTOPROXY)GetProcAddress(shWinHttp,
"WinHttpResetAutoProxy");
if (!pfnWinHttpResetAutoProxy)
{
printf("Could not find WinHttpResetAutoProxy in WinHTTP.DLL. This function only exits in Windows version 8.0 and above\n");
ErrorPrint();
return FALSE;
}
if (dwFlags == (WINHTTP_RESET_ALL | WINHTTP_RESET_OUT_OF_PROC))
{
printf("\n->Calling WinHttpResetAutoProxy with flags WINHTTP_RESET_ALL | WINHTTP_RESET_OUT_OF_PROC\n");
}
if (dwFlags == (WINHTTP_RESET_ALL | WINHTTP_RESET_DISCARD_RESOLVERS | WINHTTP_RESET_OUT_OF_PROC))
{
printf("\n->Calling WinHttpResetAutoProxy with flags WINHTTP_RESET_ALL | WINHTTP_RESET_DISCARD_RESOLVERS | WINHTTP_RESET_OUT_OF_PROC\n");
}
print_time();
dReturn = pfnWinHttpResetAutoProxy(hHttpSession, dwFlags);
print_time();
if (dReturn != ERROR_SUCCESS)
{
printf("<-WinHttpResetAutoProxy failed");
ErrorPrint();
return FALSE;
}
else
{
printf("<-WinHttpResetAutoProxy success\n");
printf("Note If you make subsequent calls to the WinHttpResetAutoProxy function, there must be at least 30 seconds delay between calls to reset the state of the auto-proxy.\n");
printf("If there is less than 30 seconds, the WinHttpResetAutoProxy function call may return ERROR_SUCCESS but the reset won't happen.\n");
}
}
else
{
printf("Loading winhttp.dll failed\n");
ErrorPrint();
return FALSE;
}
return TRUE;
}
void GetHost(WCHAR* pwszUrl, WCHAR* pwszHost, WCHAR* pwszPath, WCHAR* pwszExtraInfo, INTERNET_PORT* port, INTERNET_SCHEME* nScheme )
{
URL_COMPONENTSW URLParts;
wprintf(L"Calling WinHttpCrackUrl with url : %s\r\n", pwszUrl);
ZeroMemory(&URLParts, sizeof(URLParts));
URLParts.dwStructSize = sizeof(URLParts);
// The following elements determine which components are displayed
URLParts.dwSchemeLength = -1;
URLParts.dwHostNameLength = -1;
URLParts.dwUserNameLength = -1;
URLParts.dwPasswordLength = -1;
URLParts.dwUrlPathLength = -1;
URLParts.dwExtraInfoLength = -1;
URLParts.nScheme = -1;
if (!WinHttpCrackUrl((const WCHAR*)pwszUrl, wcslen(pwszUrl), 0, &URLParts))
{
printf("WinHttpCrackUrl error\r\n");
ErrorPrint();
exit(-1L);
}
if (URLParts.lpszHostName)
{
wcsncpy_s(pwszHost, INTERNET_MAX_HOST_NAME_LENGTH, URLParts.lpszHostName, URLParts.dwHostNameLength);
pwszHost[URLParts.dwHostNameLength] = L'\0';
wprintf(L"WinHttpCrackUrl returning host name : %s\r\n", pwszHost);
}
if (URLParts.lpszUrlPath)
{
wcsncpy_s(pwszPath, INTERNET_MAX_PATH_LENGTH, URLParts.lpszUrlPath, URLParts.dwUrlPathLength);
pwszPath[URLParts.dwUrlPathLength] = L'\0';
wprintf(L"WinHttpCrackUrl returning path : %s\r\n", pwszPath);
}
if (URLParts.lpszExtraInfo)
{
wcsncpy_s(pwszExtraInfo, INTERNET_MAX_PATH_LENGTH, URLParts.lpszExtraInfo, URLParts.dwExtraInfoLength);
pwszExtraInfo[URLParts.dwExtraInfoLength] = L'\0';
wprintf(L"WinHttpCrackUrl returning extra info : %s\r\n", pwszExtraInfo);
}
if (URLParts.nPort)
{
*port = URLParts.nPort;
wprintf(L"WinHttpCrackUrl returning port: %d (0x%X)\r\n", *port, *port);
}
if (URLParts.nScheme)
{
*nScheme = URLParts.nScheme;