Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. ' ==============================================================
  2. ' Vbscript' Name: BF2PrivateStats.vbs
  3. ' Usage: BF2PrivateStats.vbs [Enable|Disable]
  4. ' Author: The Shadow
  5. ' Email: shadow42@iinet.net.au
  6. ' Desc: This script enables/disables the HOSTS file workaround
  7. ' for BF2 Private Statistics. It supports dynamic IPs and can
  8. ' Enable/Disable the entry via way of a runtime argument.
  9. ' Execution is silent. Change the string lookup variables to suit.
  10. ' ==============================================================
  11.  
  12. ' Setup Environment
  13. Const FOR_READING = 1
  14. Const FOR_WRITING = 2
  15.  
  16. Set objFS = WScript.CreateObject("Scripting.FileSystemObject")
  17. Set objShell = WScript.CreateObject("Wscript.Shell")
  18. strHostsFile = objShell.ExpandEnvironmentStrings("%SystemRoot%") & "\system32\drivers\etc\hosts"
  19.  
  20. strMatchContent = "BF2web.gamespy.com"
  21.  
  22. ' ========================================================================
  23. ' String Lookup Variables
  24. ' ========================================================================
  25. ' EDIT: This Line includes your game server host name/IP address.
  26. 'strLookupAddr = "privatestats.gamespy.host" ' Host Name
  27. strLookupAddr = "186.124.122.74" ' IP Address
  28. 'strLookupAddr = "127.0.0.1" ' IP Address
  29. ' ========================================================================
  30.  
  31. ' ========================================================================
  32. ' Prepare a regular expression object
  33. Set myRegExp = New RegExp
  34. myRegExp.IgnoreCase = True
  35. myRegExp.Global = False
  36. myRegExp.Pattern = "\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
  37.  
  38. Set MatchData = myRegExp.Execute(strLookupAddr)
  39. If MatchData.Count > 0 Then
  40. ' Address is an IP Address
  41. Set Match = MatchData(0)
  42. ipAddress = Match.Value
  43. Else
  44. ' Resolve IP Address
  45. strLookup = "Address:"
  46. strTmpFile = objShell.ExpandEnvironmentStrings("%Temp%") & "\" & strLookupAddr & ".tmp"
  47. objShell.Run "%ComSpec% /c nslookup " & strLookupAddr & "> " & strTmpFile, 0, True
  48. Set objTF = objFS.OpenTextFile(strTmpFile)
  49. i = 1
  50. Do Until objTF.AtEndOfStream
  51. strLine = Trim(objTF.Readline)
  52. If i > 2 And Left(strLine, len(strLookup)) = strLookup Then
  53. Set MatchData = myRegExp.Execute(strLine)
  54. If MatchData.Count > 0 Then
  55. Set Match = MatchData(0)
  56. ipAddress = Match.Value
  57. Exit Do
  58. End If
  59. End If
  60. i = (i + 1)
  61. Loop
  62. objTF.Close
  63. objFS.DeleteFile strTmpFile
  64. End If
  65.  
  66. ' Check ipAddress
  67. If Len(ipAddress) > 0 Then
  68. ' Build Output String
  69. strOutput = ipAddress & Chr(9) & strMatchContent & Chr(9) & Chr(9) & "# BF2 Statistics Redirect"
  70.  
  71. ' Remove Outsmart Permissions
  72. objShell.Run "SetACL.exe -on ""%SystemRoot%\System32\drivers\etc\HOSTS"" -ot file -actn clear -clr dacl", 0, True
  73.  
  74. ' Get HOSTS Contents
  75. Set objTS = objFS.OpenTextFile(strHostsFile ,FOR_READING)
  76. strContents = objTS.ReadAll
  77. objTS.Close
  78.  
  79. ' Set HOSTS for Writing
  80. Set objTS = objFS.OpenTextFile(strHostsFile ,FOR_WRITING)
  81. arrLines = Split(strContents, vbNewLine)
  82.  
  83. ' Prepare a regular expression object
  84. Set myRegExp = New RegExp
  85. myRegExp.IgnoreCase = True
  86. myRegExp.Global = True
  87. myRegExp.Pattern = strMatchContent
  88.  
  89. ' Re-write HOSTS file
  90. bUpdated = False
  91. For i = 0 To UBound(arrLines) - 1
  92. If myRegExp.Test(arrLines(i)) Then
  93. bUpdated = True
  94. objTS.WriteLine strOutput
  95. Else
  96. objTS.WriteLine arrLines(i)
  97. End If
  98. Next
  99.  
  100. ' Check File Updated
  101. If bUpdated = False Then
  102. ' Insert Reference at end of File
  103. objTS.WriteLine strOutput
  104. End If
  105. objTS.Close
  106.  
  107. ' Outsmart BF2 - Deny Read Permissions on HOSTS file
  108. objShell.Run "%ComSpec% /c ipconfig /flushdns", 0, True
  109. objShell.Run "%ComSpec% /c ping " & strMatchContent, 0, True
  110. objShell.Run "SetACL.exe -on ""%SystemRoot%\System32\drivers\etc\HOSTS"" -ot file -actn ace -ace ""n:S-1-5-32-545;p:read;s:y;m:deny""", 0, True
  111.  
  112. ' Get Arguments
  113. Set objArgs = WScript.Arguments
  114. strArgs = ""
  115. If objArgs.Count >= 1 Then
  116. For i = 0 To objArgs.Count - 1
  117. strArgs = strArgs & " " & objArgs(i)
  118. Next
  119. Else
  120. strArgs = " +menu 1 +fullscreen 1"
  121. End If
  122.  
  123. ' Run BF2 :)
  124. objShell.Run "BF2.EXE" & strArgs, 0, True
  125.  
  126. ' Remove Outsmart Permissions
  127. objShell.Run "SetACL.exe -on ""%SystemRoot%\System32\drivers\etc\HOSTS"" -ot file -actn clear -clr dacl", 0, True
  128.  
  129. ' Re-write HOSTS file back to original
  130. Set objTS = objFS.OpenTextFile(strHostsFile ,FOR_WRITING)
  131. For i = 0 To UBound(arrLines) - 1
  132. objTS.WriteLine arrLines(i)
  133. Next
  134. objTS.Close
  135.  
  136. Else
  137. strErrMsg = "ERROR: Could not resolve '" & strLookupAddr & "'!" & vbCrLf
  138. strErrMsg = strErrMsg & "Please Manually update your HOSTS file!"
  139. Wscript.Echo strErrMsg
  140. End If
  141. ' ========================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement