Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save m8r1us/33ac57e618c3185566c028f3cc1839ac to your computer and use it in GitHub Desktop.
Save m8r1us/33ac57e618c3185566c028f3cc1839ac to your computer and use it in GitHub Desktop.
Please read the Notes at the top of each of the scripts before you run them. Set Windows IE proxy with a cmd file, or set Windows IE proxy globally by changing the "DefaultConnectionSettings" registry key as well as "ProxySettingsPerUser" and "Proxy" for GPO settings. PLEASE SAVE A COPY OF YOUR REGISTRY JUST IN CASE YOU NEED TO GO BACK!
:: NOTE: If you don't want to make proxy settings for the whole machine,
:: find the line in this script that starts with "reg add" and change
:: "HKLM" to "HKCU" so that it only affects the current user (or change it
:: to reflect the specific hive that you want to change)
:: Supported Characters in proxy server address (if you need more, consult
:: an ascii table and make a new "if" statement for the character you need,
:: but these should suffice):
:: * - . / : ; = ? @ ~ < >
:: All Numbers
:: All Uppercase Letters
:: All Lowercase Letters
@echo off
:: This causes variables within this script to be expanded at execution time
:: rather than parse time.
setlocal ENABLEDELAYEDEXPANSION
:: Start at main
goto :main
:: Called as "string2hex returnVal string"
:: Returns a Comma separated list of hex digits in the global variable "ret"
:: and the length of the input string as "pos"
:string2hex
set pos=0
set str=%~1
set ret=
:: Loop starts here. We will come back to this until the string is done
:NextChar
set char=!str:~%pos%,1!
:: set char=%str%
:: if the character doesn't exist, exit out because we are done parsing the
:: string
if "%char%" == "" goto :eof
:: Otherwise check to see what the char is, and add the corresponding hex
:: code to the return variable of this subroutine
if "%char%" == "*" (
set "ret=!ret!2a"
:: If we find the character, break out of the if statements
goto :breakIf
)
if "%char%" == "-" (
set "ret=!ret!2d"
goto :breakIf
)
if "%char%" == "." (
set "ret=!ret!2e"
goto :breakIf
)
if "%char%" == "/" (
set "ret=!ret!2f"
goto :breakIf
)
if "%char%" == "0" (
set "ret=!ret!30"
goto :breakIf
)
if "%char%" == "1" (
set "ret=!ret!31"
goto :breakIf
)
if "%char%" == "2" (
set "ret=!ret!32"
goto :breakIf
)
if "%char%" == "3" (
set "ret=!ret!33"
goto :breakIf
)
if "%char%" == "4" (
set "ret=!ret!34"
goto :breakIf
)
if "%char%" == "5" (
set "ret=!ret!35"
goto :breakIf
)
if "%char%" == "6" (
set "ret=!ret!36"
goto :breakIf
)
if "%char%" == "7" (
set "ret=!ret!37"
goto :breakIf
)
if "%char%" == "8" (
set "ret=!ret!38"
goto :breakIf
)
if "%char%" == "9" (
set "ret=!ret!39"
goto :breakIf
)
if "%char%" == ":" (
set "ret=!ret!3a"
goto :breakIf
)
if "%char%" == ";" (
set "ret=!ret!3b"
goto :breakIf
)
if "%char%" == "<" (
set "ret=!ret!3c"
goto :breakIf
)
if "%char%" == "=" (
set "ret=!ret!3d"
goto :breakIf
)
if "%char%" == ">" (
set "ret=!ret!3e"
goto :breakIf
)
if "%char%" == "?" (
set "ret=!ret!3f"
goto :breakIf
)
if "%char%" == "@" (
set "ret=!ret!40"
goto :breakIf
)
if "%char%" == "A" (
set "ret=!ret!41"
goto :breakIf
)
if "%char%" == "B" (
set "ret=!ret!42"
goto :breakIf
)
if "%char%" == "C" (
set "ret=!ret!43"
goto :breakIf
)
if "%char%" == "D" (
set "ret=!ret!44"
goto :breakIf
)
if "%char%" == "E" (
set "ret=!ret!45"
goto :breakIf
)
if "%char%" == "F" (
set "ret=!ret!46"
goto :breakIf
)
if "%char%" == "G" (
set "ret=!ret!47"
goto :breakIf
)
if "%char%" == "H" (
set "ret=!ret!48"
goto :breakIf
)
if "%char%" == "I" (
set "ret=!ret!49"
goto :breakIf
)
if "%char%" == "J" (
set "ret=!ret!4a"
goto :breakIf
)
if "%char%" == "K" (
set "ret=!ret!4b"
goto :breakIf
)
if "%char%" == "L" (
set "ret=!ret!4c"
goto :breakIf
)
if "%char%" == "M" (
set "ret=!ret!4d"
goto :breakIf
)
if "%char%" == "N" (
set "ret=!ret!4e"
goto :breakIf
)
if "%char%" == "O" (
set "ret=!ret!4f"
goto :breakIf
)
if "%char%" == "P" (
set "ret=!ret!50"
goto :breakIf
)
if "%char%" == "Q" (
set "ret=!ret!51"
goto :breakIf
)
if "%char%" == "R" (
set "ret=!ret!52"
goto :breakIf
)
if "%char%" == "S" (
set "ret=!ret!53"
goto :breakIf
)
if "%char%" == "T" (
set "ret=!ret!54"
goto :breakIf
)
if "%char%" == "U" (
set "ret=!ret!55"
goto :breakIf
)
if "%char%" == "V" (
set "ret=!ret!56"
goto :breakIf
)
if "%char%" == "W" (
set "ret=!ret!57"
goto :breakIf
)
if "%char%" == "X" (
set "ret=!ret!58"
goto :breakIf
)
if "%char%" == "Y" (
set "ret=!ret!59"
goto :breakIf
)
if "%char%" == "Z" (
set "ret=!ret!5a"
goto :breakIf
)
if "%char%" == "a" (
set "ret=!ret!61"
goto :breakIf
)
if "%char%" == "b" (
set "ret=!ret!62"
goto :breakIf
)
if "%char%" == "c" (
set "ret=!ret!63"
goto :breakIf
)
if "%char%" == "d" (
set "ret=!ret!64"
goto :breakIf
)
if "%char%" == "e" (
set "ret=!ret!65"
goto :breakIf
)
if "%char%" == "f" (
set "ret=!ret!66"
goto :breakIf
)
if "%char%" == "g" (
set "ret=!ret!67"
goto :breakIf
)
if "%char%" == "h" (
set "ret=!ret!68"
goto :breakIf
)
if "%char%" == "i" (
set "ret=!ret!69"
goto :breakIf
)
if "%char%" == "j" (
set "ret=!ret!6a"
goto :breakIf
)
if "%char%" == "k" (
set "ret=!ret!6b"
goto :breakIf
)
if "%char%" == "l" (
set "ret=!ret!6c"
goto :breakIf
)
if "%char%" == "m" (
set "ret=!ret!6d"
goto :breakIf
)
if "%char%" == "n" (
set "ret=!ret!6e"
goto :breakIf
)
if "%char%" == "o" (
set "ret=!ret!6f"
goto :breakIf
)
if "%char%" == "p" (
set "ret=!ret!70"
goto :breakIf
)
if "%char%" == "q" (
set "ret=!ret!71"
goto :breakIf
)
if "%char%" == "r" (
set "ret=!ret!72"
goto :breakIf
)
if "%char%" == "s" (
set "ret=!ret!73"
goto :breakIf
)
if "%char%" == "t" (
set "ret=!ret!74"
goto :breakIf
)
if "%char%" == "u" (
set "ret=!ret!75"
goto :breakIf
)
if "%char%" == "v" (
set "ret=!ret!76"
goto :breakIf
)
if "%char%" == "w" (
set "ret=!ret!77"
goto :breakIf
)
if "%char%" == "x" (
set "ret=!ret!78"
goto :breakIf
)
if "%char%" == "y" (
set "ret=!ret!79"
goto :breakIf
)
if "%char%" == "z" (
set "ret=!ret!7a"
goto :breakIf
)
if "%char%" == "~" (
set "ret=!ret!7e"
goto :breakIf
)
:: if a character is not found exit out (TODO: add some error catching here,
:: since we should never reach the end of this if statement)
goto :eof
:: We come straight here if a character is found. We will increment the index
:: counter, and loop back to check the next character in the string
:breakIf
set /a pos=pos+1
goto NextChar
goto :eof
:: Convert Decimal Values to Hex
:dec2hex
:: This variable will be used to lookup the correct hex digit
set "hex=0123456789ABCDEF"
:: Set "high" to be the most significant digit of the hex byte
:: Set "low" to be the least significant digit of the hex byte
set /a high=%~1 / 16
set /a low=%~1 %% 16
:: Set a global variable to be the combination of the most and least
:: significant digits of the hex byte (this is also the step where we
:: lookup the correct hex digit)
set poshex=!hex:~%high%,1!!hex:~%low%,1!
goto :eof
:: Start of main
:main
set /p proxyserver=Enter the Proxy Server:
set /p proxyport=Enter the Proxy Port:
set /p proxyexceptions=Enter the Proxy Exceptions Separated by semicolons (surround with double quotes):
:: Get the proxy name as hex along with the length of the proxy name
:: in hex and set variables for them so that we can reference them easily
set proxyname=%proxyserver%:%proxyport%
call :string2hex %proxyname%
set proxynamehex=%ret%
call :dec2hex %pos%
set proxynamelenhex=%poshex%
:: Get the proxy exceptions in hex along with the length of the proxy
:: exception length in hex and set variables for them so that we can
:: reference them easily
call :string2hex %proxyexceptions%
set proxyexceptionshex=%ret%
call :dec2hex %pos%
set proxyexceptionslenhex=%poshex%
:: Filler Data that needs to be there
:: * the 46 is just always there
:: * the 16 is a counter that would normally increment every time the value
:: is changed so there is no need to change it here
:: * the 03 is a flag that determines whether the proxy will be on or not,
:: and if automatically detect settings is on or off (0x03 is 0b00000011.
:: From left to right, the first 4 0s mean nothing and are just always 0.
:: The next bit is a 1 if the "Automatically Detect Settings" Checkbox is
:: checked in interet options. The next bit is a 1 if a script URL is set
:: in internet options. The next bit is a 1 if the manual proxy is
:: enabled, and the last bit is always a 1. So this setting just has the
:: manual proxy on)
:: * Ther rest of the 0s are to fill space between the actual useful
:: information that was gathered earlier
set data1=460000001600000003000000
set data2=000000
set data3=000000
set data4=000000000000000000000000000000000000000000000000000000000000000000000000
:: This is where we combine the filler data above with the useful information
:: ie: data1 + the length of the proxy name and port + data2 +
:: proxy name and port + proxy exception list length + data3 +
:: proxy exception list + data4
set data=%data1%%proxynamelenhex%%data2%%proxynamehex%%proxyexceptionslenhex%%data3%%proxyexceptionshex%%data4%
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Connections" /v "DefaultConnectionSettings" /t "REG_BINARY" /d "%data%" /f
pause
endlocal
:: This script toggles the state of these two registry settings.
:: Running it once will grey out the options and set the proxy machine
:: wide, running it again will revert it back, etc.
:: NOTE: You only need these settings if you are wanting to set the proxy
:: globally on your machine for all users, or you want to block users
:: from being able to change the proxy settings
:: Alternatively, set these GPOs in gpedit.msc if you have it
:: NOTE: Setting these registry keys directly will not be reflected in
:: gpedit.msc so far as I can tell. From what I have personally found,
:: gpedit.msc does not seem to actually look at the registry to see what
:: the state of the GPOs are, therefore, even though you have changed the
:: registry keys, it will NOT show as enabled or disabled in gpedit.msc.
:: The setting will be whatever it was when you last set it.
:: NOTE: I found these keys and the locations by looking at the ADMX template
:: files. Inside the inetres.admx file, it shows that these are the keys that
:: are modified for those specific GPOs.
@echo off
reg query "HKLM\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings" /v "ProxySettingsPerUser" | find "0x0" > nul
:: Currently Set to (1) Per User, change to Per Machine
if %errorlevel% == 1 (
echo Setting Proxy Settings to Machine Wide
reg add "HKLM\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings" /v "ProxySettingsPerUser" /t "REG_DWORD" /d "0" /f > null
goto :endproxysettingsperuser
)
:: Set to (0) Per Machine, change to Per User
if %errorlevel% == 0 (
echo Setting Proxy Settings to User Wide
reg add "HKLM\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings" /v "ProxySettingsPerUser" /t "REG_DWORD" /d "1" /f > null
goto :endproxysettingsperuser
)
:endproxysettingsperuser
reg query "HKLM\Software\Policies\Microsoft\Internet Explorer\Control Panel" /v "Proxy" | find "0x0" > nul
:: Currently Set to (1) Greying the boxes, change to not greying the boxes
if %errorlevel% == 1 (
echo Setting Proxy Settings to NOT be greyed out
reg add "HKLM\Software\Policies\Microsoft\Internet Explorer\Control Panel" /v "Proxy" /t "REG_DWORD" /d "0" /f > null
goto :endproxy
)
:: Currently Set to (0) not be greyed, change to greyed
if %errorlevel% == 0 (
echo Setting Proxy Settings to be greyed out
reg add "HKLM\Software\Policies\Microsoft\Internet Explorer\Control Panel" /v "Proxy" /t "REG_DWORD" /d "1" /f > null
goto :endproxy
)
:endproxy
echo DONE!
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment