congdantoancau

[Chrome] Disable Dev Mode Warning

Feb 6th, 2018
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <# :
  2. @echo off
  3. copy/b "%~f0" "%temp%\%~n0.ps1" >nul
  4. powershell -v 2 -ep bypass -noprofile "%temp%\%~n0.ps1" "'%cd% '" "'%~1'"
  5. del "%temp%\%~n0.ps1"
  6. echo: & pause
  7. exit /b
  8. #>
  9. param([string]$cwd='.', [string]$dll)
  10.  
  11. function main {
  12.     write-host -f white -b black `
  13.        "Chrome 'developer mode extensions' warning disabler 20170328"
  14.    $pathsDone = @{}
  15.    if ($dll -and (gi -literal $dll)) {
  16.        doPatch "DRAG'n'DROPPED" ((gi -literal $dll).directoryName + '\')
  17.        exit
  18.    }
  19.    doPatch 'CURRENT DIRECTORY' ((gi -literal $cwd).fullName + '\')
  20.    ('HKLM', 'HKCU') | %{ $hive = $_
  21.        ('', '\Wow6432Node') | %{
  22.            $key = "${hive}:\SOFTWARE$_\Google\Update\Clients"
  23.            gci -ea silentlycontinue $key -r | gp | ?{ $_.CommandLine } | %{
  24.                $path = $_.CommandLine -replace '"(.+?\\\d+\.\d+\.\d+\.\d+\\).+', '$1'
  25.                if (!$pathsDone[$path.toLower()]) {
  26.                    doPatch REGISTRY $path
  27.                    $pathsDone[$path.toLower()] = $true
  28.                }
  29.            }
  30.        }
  31.    }
  32. }
  33.  
  34. function doPatch([string]$pathLabel, [string]$path) {
  35.    $dll = Join-Path $path chrome.dll
  36.    if (!(Test-Path -literal $dll)) {
  37.        return
  38.    }
  39.    ''
  40.    $localAppData = [Environment]::GetFolderPath('LocalApplicationData')
  41.    "$pathLabel $((split-path $dll).Replace($localAppData, '%LocalAppData%'))"
  42.  
  43.    "`tREADING Chrome.dll..."
  44.    $bin = [IO.BinaryReader][IO.File]::OpenRead($dll)
  45.    $bytes = $bin.ReadBytes(1MB)
  46.  
  47.    # process PE headers
  48.    $BC = [BitConverter]
  49.    $coff = $BC::ToUInt32($bytes,0x3C) + 4
  50.    $is64 = $BC::ToUInt16($bytes,$coff) -eq 0x8664
  51.    $opthdr = $coff+20
  52.    $codesize = $BC::ToUInt32($bytes,$opthdr+4)
  53.    $imagebase32 = $BC::ToUInt32($bytes,$opthdr+28)
  54.  
  55.    # patch the flag in data section
  56.    $bin.BaseStream.Position = $codesize
  57.    $data = $BC::ToString($bin.ReadBytes($bin.BaseStream.Length - $codesize))
  58.    $bin.Close()
  59.    $flag = 'ExtensionDeveloperModeWarning'
  60.    $stroffs = $data.IndexOf($BC::ToString($flag[1..99]))
  61.    if ($stroffs -lt 0) {
  62.        write-host -f red "`t$flag not found"
  63.        return
  64.    }
  65.    if ($data.substring($stroffs-3, 2) -eq '00') {
  66.        write-host -f darkgreen "`tALREADY PATCHED"
  67.        return
  68.    }
  69.    $stroffs = $stroffs/3 - 1 + $codesize
  70.  
  71.    $centbrowser = $data.indexOf($BC::ToString('CentBrowser'[0..99])) -gt 0
  72.  
  73.    $EA = $ErrorActionPreference
  74.    $ErrorActionPreference = 'silentlyContinue'
  75.    $exe = join-path (split-path $path) chrome.exe
  76.    while ((get-process chrome -module | ?{ $_.FileName -eq $exe })) {
  77.        forEach ($timeout in 15..0) {
  78.            write-host -n -b yellow -f black `
  79.                "`rChrome is running and will be terminated in $timeout sec. "
  80.            write-host -n -b yellow -f darkyellow "Press ENTER to do it now. "
  81.            if ([console]::KeyAvailable) {
  82.                $key = $Host.UI.RawUI.ReadKey("AllowCtrlC,IncludeKeyDown,NoEcho")
  83.                if ($key.virtualKeyCode -eq 13) { break }
  84.                if ($key.virtualKeyCode -eq 27) { write-host; exit }
  85.            }
  86.            sleep 1
  87.        }
  88.        write-host
  89.        get-process chrome | ?{
  90.            $_.MainWindowHandle.toInt64() -and ($_ | gps -file).FileName -eq $exe
  91.        } | %{
  92.            "`tTrying to exit gracefully..."
  93.            if ($_.CloseMainWindow()) {
  94.                sleep 1
  95.            }
  96.        }
  97.        $killLabelShown = 0
  98.        get-process chrome | ?{
  99.            ($_ | gps -file | select -expand FileName) -eq $exe
  100.        } | %{
  101.            if (!$killLabelShown++) {
  102.                "`tTerminating background chrome processes..."
  103.            }
  104.            stop-process $_ -force
  105.        }
  106.        sleep -milliseconds 200
  107.    }
  108.    $ErrorActionPreference = $EA
  109.  
  110.    $bytes = [IO.File]::ReadAllBytes($dll)
  111.    $bytes[$stroffs] = 0
  112.    "`tPATCHED $flag flag"
  113.  
  114.    # patch the channel restriction code for stable/beta
  115.    $rxChannel = '83-F8-(?:03-7D|02-7F|02-0F-8F)'
  116.    # old code: cmp eax,3; jge ...
  117.    # new code: cmp eax,2; jg ... (jg can be 2-byte)
  118.    function patch64 {
  119.        $pos = 0
  120.        $rx = [regex]"$rxChannel-.{1,100}-48-8D"
  121.        do {
  122.            $m = $rx.match($code,$pos)
  123.            if (!$m.success) { break }
  124.            $chanpos = $searchBase + $m.index/3 + 2
  125.            $pos = $m.index + $m.length + 1
  126.            $offs = $BC::ToUInt32($bytes, $searchBase + $pos/3+1)
  127.            $diff = $searchBase + $pos/3+5+$offs - $stroffs
  128.        } until ($diff -ge 0 -and $diff -le 4096 -and $diff % 256 -eq 0)
  129.        if (!$m.success) {
  130.            $rx = [regex]"84-C0.{18,48}($rxChannel)-.{30,60}84-C0"
  131.            $m = $rx.matches($code)
  132.            if ($m.count -ne 1) { return }
  133.            $chanpos = $searchBase + $m[0].groups[1].index/3 + 2
  134.        }
  135.        $chanpos
  136.    }
  137.    function patch86 {
  138.        $flagOffs = [uint32]$stroffs + [uint32]$imagebase32
  139.        $flagOffsStr = $BC::ToString($BC::GetBytes($flagOffs))
  140.        $variants = "(?<channel>$rxChannel-.{1,100})-68-(?<flag>`$1-.{6}`$2)",
  141.                "68-(?<flag>`$1-.{6}`$2).{300,500}E8.{12,32}(?<channel>$rxChannel)",
  142.                "E8.{12,32}(?<channel>$rxChannel).{300,500}68-(?<flag>`$1-.{6}`$2)"
  143.        forEach ($variant in $variants) {
  144.            $pattern = $flagOffsStr -replace '^(..)-.{6}(..)', $variant
  145.            $patternDisplay = $pattern -replace '^(.{40}).+', '$1'
  146.            write-host -f darkgray "`tLooking for $patternDisplay..."
  147.            $minDiff = 65536
  148.            foreach ($m in [regex]::matches($code, $pattern)) {
  149.                $maybeFlagOffs = $BC::toUInt32($bytes, $searchBase +
  150.                                                       $m.groups['flag'].index/3)
  151.                $diff = [Math]::abs($maybeFlagOffs - $flagOffs)
  152.                if ($diff % 256 -eq 0 -and $diff -lt $minDiff) {
  153.                    $minDiff = $diff
  154.                    $chanpos = $searchBase + $m.groups['channel'].index/3 + 2
  155.                }
  156.            }
  157.        }
  158.        $chanpos
  159.    }
  160.    $searchBase = [int]($codesize/2)
  161.    foreach ($pass in 1..2) {
  162.        if ($centbrowser) { break }
  163.        $code = $BC::ToString($bytes, $searchBase, $codesize - $searchBase)
  164.        $chanpos = if ($is64) { patch64 } else { patch86 }
  165.        if ($chanpos) { break }
  166.        $codesize = $searchBase
  167.        $searchBase = 0
  168.    }
  169.    if ($chanpos) {
  170.        $bytes[$chanpos] = 9
  171.        "`tPATCHED Chrome release channel restriction"
  172.    } elseif (!$centbrowser) {
  173.        write-host -f red "`tUnable to find the channel code, try updating me"
  174.        write-host -f red "`thttp://stackoverflow.com/a/30361260"
  175.         return
  176.     }
  177.  
  178.     "`tWriting to a temporary dll..."
  179.     [IO.File]::WriteAllBytes("$dll.new",$bytes)
  180.  
  181.     "`tBacking up the original dll..."
  182.     move -literal $dll "$dll.bak" -force
  183.  
  184.     "`tRenaming the temporary dll as the original dll..."
  185.     move -literal "$dll.new" $dll -force
  186.  
  187.     write-host -f green "`tDONE.`n"
  188.     [GC]::Collect()
  189. }
  190.  
  191. main
Add Comment
Please, Sign In to add comment