Jump to content
Sign in to follow this  

How to add recoder into HOSTS with RunAs in Win7

Recommended Posts



i want add one recorder into HOSTS file file with following script in windows 7, but always failed, does anyone help me to correct it ?

--script start--

Local $sUserName = "Administrator"
Local $sPassword = "abcd1234"
RunAs($sUserName,@computername,$sPassword,0,"attrib -s -h -r C:\Windows\System32\drivers\etc\hosts")
RunAs($sUserName,@computername,$sPassword,0,"echo xxx.xxx.xxx.xxx www.google.com" & @CRLF)
RunAs($sUserName,@computername,$sPassword,0,"attrib +s +h +r C:\Windows\System32\drivers\etc\hosts")

--script end--

Share this post

Link to post
Share on other sites

This works for me:

Local $sUserName = "xxxx"
Local $sPassword = "xxxx"
Local $sHostFile = "C:\Windows\System32\drivers\etc\hosts"
RunAs($sUserName,@computername,$sPassword,0,"attrib -s -h -r " & $sHostFile)
;RunAs($sUserName,@computername,$sPassword,0,"echo xxx.xxx.xxx.xxx www.google.com" & @CRLF)
$hFile = FileOpen($sHostFile, $FO_APPEND)
FileWrite($hFile,"echo xxx.xxx.xxx.xxx www.google.com" & @CRLF)
Run("Notepad.exe " &  $sHostFile)
RunAs($sUserName,@computername,$sPassword,0,"attrib +s +h +r " & $sHostFile)


Edited by Shane0000

Share this post

Link to post
Share on other sites

Hi Shane0000, from your code, i got the error message:

$FO_APPEND Error: Variable used without being declared.

i try another command like below:

Local $sUserName = "aaaaa"
Local $sPassword = "bbbbb"
RunAs($sUserName,@computername,$sPassword,0,"attrib -s -h -r C:\Windows\system32\drivers\etc\hosts")
Local $file = FileOpen("C:\Windows\system32\drivers\etc\hosts",1)
Local $command = FileWriteLine($file, "")
Local $command = FileWriteLine($file, "ccc.ccc.ccc.ccc    www.google.com")
RunAs($sUserName,@computername,$sPassword,0,"attrib +s +h +r C:\Windows\system32\drivers\etc\hosts")
MsgBox(0,"Information","Configuration completed")
Edited by JRZJulien

Share this post

Link to post
Share on other sites

$FO_APPEND is part of the File Operations globals file you probably have to INCLUDE


[autoit]#include <FileConstants.au3> [/autoit]

Edited by Blue_Drache

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Similar Content

    • PINTO1927
      By PINTO1927
      HI GUYS,
      I'm trying to run this script with an advanced domain user, but when compiling the cmd it returns access denied, as if it did not recognize the user of AD.
      RunAsWait("administrator", "CONTOSO", "Services.1", 2, "C:\Users\albert.frizz\Desktop\test.bat")
      can you help me please?
    • Traskiz
      By Traskiz
      I have a problem, I cant run script as administrator in Windows 10:
      RegWrite('HKLM\SOFTWARE\Policies\Microsoft\Windows\BITS', 'EnableBITSMaxBandwidth','REG_DWORD',Number('1'))
      RegWrite('HKLM\SOFTWARE\Policies\Microsoft\Windows\BITS', 'MaxTransferRateOnSchedule','REG_DWORD',Number('100'))
      RegWrite('HKLM\SOFTWARE\Policies\Microsoft\Windows\BITS', 'MaxBandwidthValidFrom','REG_DWORD',Number('7'))
      RegWrite('HKLM\SOFTWARE\Policies\Microsoft\Windows\BITS', 'MaxBandwidthValidTo','REG_DWORD',Number('22'))
      RegWrite('HKLM\SOFTWARE\Policies\Microsoft\Windows\BITS', 'UseSystemMaximum','REG_DWORD',Number('1'))
      RegWrite('HKLM\SOFTWARE\Policies\Microsoft\Windows\BITS', 'MaxTransferRateOffSchedule','REG_DWORD',Number('400'))
      Global $sUserName = "administrator"
      Global $sPassword = "pass"
      Global $sDomain = "domain"
      RunAsWait($sUserName, $sDomain, $sPassword, 2, "main.exe", "", @TempDir)
      If I run "run.exe" it dont work...
      If  I change main.exe to this:
      and it runs and shows administrator in message box... but it cant elevate main.exe with RegWrite() command...
      I tried using #RequireAdmin in first line of main.exe, but it not worked... UAC is set to "do not notify"
      This method of elevation worked on Windows 7 and Windows XP...
      Please help!
    • Surf243
      By Surf243
      I've been using this UDF to set ACL permissions to some network folders, everything works great (no issues). However, I want to apply these permissions using elevated domain credentials supplied by the user and not the user that's currently running the script. As a temporary solution, I've implemented a RunAs function, but that's not the solution I'm looking for. I'm not fluent with using Dlls, but I have been trying out different methods.
      My RunAs Function:
      Func _RunAs($sUser, $sPass) If @Compiled Then RunAs($sUser, @LogonDomain, $sPass, 4, FileGetShortName(@ScriptFullPath), "", @SW_MAXIMIZE) Else RunAs($sUser, @LogonDomain, $sPass, 4, FileGetShortName(@AutoItExe) & " " & FileGetShortName(@ScriptFullPath), "", @SW_MAXIMIZE) EndIf EndFunc ;==>_RunAs I tried LogonUser and I know that I can take that token to ImpersonateLoggedOnUser, but I'm not sure how to implement that or if that's even the right method. I also need to RevertToSelf once completed.
      Func _LogonUser($sUsername, $sPassword, $sServer = @LogonDomain) ; Returns True if user exists Local $stToken $stToken = DllStructCreate("int") Local $aRet = DllCall("advapi32.dll", "int", "LogonUser", _ "str", $sUsername, "str", $sServer, "str", $sPassword, "dword", 3, "dword", 0, "ptr", DllStructGetPtr($stToken)) ;$hToken = DllStructGetData($stToken, 1) If Not @error And $aRet[0] <> 0 Then Return True EndIf Return False EndFunc ;==>_LogonUser Any assistance, suggestions or idea's would be helpful.
    • Milas
      By Milas
      Hey guys,
               for some reason when I run this command with run as in autoit it returns access is denied. but when I run the same thing in an elevated command shell it works fine. Im not sure where I am messing up.
      global $rtech,$rcred, $IP $rtech = inputbox("","username") $rcred = InputBox("","password") $ip = inputbox("","enter IP") $pid = Runas ($rtech,"mhs", $rcred, 2 ,@ComSpec & ' /c reg query \\' & $ip & '"\hklm\software\microsoft\windows\currentversion\group policy\state\machine"|findstr /i "disting"', @SystemDir, @SW_hide, $STDERR_CHILD + $STDOUT_CHILD) Local $line While 1 $line &= StdoutRead($pid) If @error Then ExitLoop WEnd While 1 $line &= StderrRead($pid) If @error Then ExitLoop WEnd MsgBox(0, "", $line)  
    • WoodGrain
      By WoodGrain
      Hi All,
      I've got a script setup to drop a program into a temp folder and then run it from there, but I have mixed results, the Run() command will work on some computers but not others. The file will be placed into the temp folder in all cases. Running AutoIT The computers are all either Win 7 or 8.1, x64 (exe is compiled to x64 too), UAC is off, all have local admin rights - if I've missed something ask and I'll update the details.
      If Not FileExists (@TempDir & "\HCTB") Then DirCreate(@TempDir & "\HCTB") FileInstall("G:\IT\Downloads\TB\12.0.45471\Host\TB_Setup-sif7r8pgcq.exe", @TempDir & "\HCTB\TB_Setup-sif7r8pgcq.exe", 1) Local $iPID = Run(@TempDir & "\HCTB\TB_Setup-sif7r8pgcq.exe", "") Any suggestions?