How to block a website with Delphi, vbs or msdos?

Asked

Viewed 364 times

4

I have a user control and monitoring system and would like to block a site using Delphi or even MSDOS or vbs commands, or even using Sockets(I don’t know if it’s possible)? What I have so far is this::

Delphi

var 
  dd: TextFile; 

begin 

if FileExists('\Windows\system32\drivers\etc\hosts') then 
begin 

  AssignFile(dd,'\Windows\system32\drivers\etc\hosts'); 
  ReSet(dd); 
  Append(dd); // aqui ocorre "File access denied" 

  if IOResult = 0 then 
  begin 
    WriteLn(dd,'127.0.0.1 www.meusite.com'); 

  CloseFile(dd); 
  end; 
end; 

VBS

dim Fso,f
Dim rep,label,titre,defaut,data
label="Escreva a URL que deseja bloquear"
defaut=""
titre="Bloquear Sites"
rep=InputBox(label,titre,defaut)
Set Fso = CreateObject("Scripting.FileSystemObject") ' aqui dá "Acesso negado"
sys32=Fso.GetSpecialFolder(1)
Set f = fso.OpenTextFile(sys32+"\DRIVERS\ETC\hosts", 8)
if rep="" then Cleanup
f.Write vbnewline
f.Write "127.0.0.1   "  &rep

Sub Cleanup()
  Set FSO = Nothing
  WScript.Quit
End Sub
  • Do you want to create a proxy, a large application? Or just a "script"?

  • One script only. It’s for a user control system on a PC.

  • 1

    What’s wrong with this code you posted?

  • What you really need?

  • I need to find a way to block a site (read the title again), in Delphi, msdos or vbs (not VB.net). The post q code (if you read it) you must have seen that the two give error "access denied".

  • 1

    @Does Vaati give denied access to edit? If that’s why you have to run the application as an administrator

Show 1 more comment

2 answers

2


On the line:

Append(dd); // aqui ocorre "File access denied"

Or:

Set Fso = CreateObject("Scripting.FileSystemObject") ' aqui dá "Acesso negado"

Occurs the denied access for two possible reasons:

  • You did not run your application as an Administrator
  • The Antivirus is activated

The archive hosts is blocked for editing and only the "run as administrator" (equivalent to the root of the like-Unix system in Windows) can edit.

To always run Delphi as an Administrator you will need to configure the WOW. This question has an example of how to do this:

  • That’s exactly what I needed. About the UAC commands. Because I can’t just "run as an administrator" every time my client logs in. If you have any UAC content, I’d appreciate it. I did uac.manifest and uac.rs but the uac.res file is not created even after compiling with Delphi’s brcc32. if you have a right link d how to do I thank you.

  • I found: https://www.youtube.com/watch?v=1xPAJbevlrQ

  • I did not understand -1, within what was asked I answered. If the question was how to add UAC I would understand -1. Anyway I’ll add an example later ;)

-3

Solution in VBS. Make a copy of hosts and then replace it.

WebsitesToBlock = Array("twitter.com", "www.youtube.com", www.facebook.com")

If WScript.Arguments.length =0 Then
 Set objShell = CreateObject("Shell.Application")
    objShell.ShellExecute "wscript.exe", Chr(34) & WScript.ScriptFullName & Chr(34) & " RunAsAdministrator", "", "runas", 1
Else
 Const ForReading = 1, ForWriting = 2

 Set shell = CreateObject("WScript.Shell")    
    root = shell.ExpandEnvironmentStrings("%systemroot%")     
    hostFile = root & "\system32\drivers\etc\hosts"
    tempFile = hostFile & ".bak"

    blocked = 0
    towrite = false

 Set fso = CreateObject("Scripting.FileSystemObject")
 Set f1 = fso.OpenTextFile(hostFile, ForReading, True)
 Set f2 = fso.OpenTextFile(tempFile, ForWriting, True)

 Do Until f1.AtEndOfStream

        line = f1.Readline
        towrite = true

     For Each URL in WebsitesToBlock
         If instr(line, URL) Then
             If blocked = 0 Then 
                 If left(line, 1) = "#" Then blocked = 1 Else blocked = 2
             End If
            towrite = false
         End If
     Next    

     If towrite Then f2.WriteLine line
 Loop

 For Each URL in WebsitesToBlock
     If blocked <> 2 Then
            f2.WriteLine "127.0.0.1" & vbTab & vbTab & URL 
     End If
 Next

    fso.Copyfile tempFile, hostFile

    f1.Close
    f2.Close

 If blocked = 2 Then 
        WScript.echo "Time wasting websites have now been unblocked!" 
 Else
        WScript.echo "Time wasting websites are now blocked!" 
 End If

End If
  • I don’t understand, I could explain how this solves?

  • it wasn’t me who denied it, but it would be nice if you explained how the code edit Access denied ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.