Solved

Need help with VBS code to modify the content of a file

Posted on 2014-01-21
13
1,083 Views
Last Modified: 2014-01-30
Hi,

We need modify the following file "exception.sites" (no extension) from the following path

C:\Users\username\AppData\LocalLow\Sun\Java\Deployment\security\

We need to add a few sites to the current "exception.sites" the sites have to be entered one per line, for example:

http://url1
http://url2

The file can be modified by the users since they might be entering a site that has to be excluded from Java.

The script will run as part as the logon script and will add the sites to the local file on a per user basis.

We are trying to find a way to manage the new Java 7 U 51 exception list and warranty that the business sites are included in the exception.list file and also that the users will be able to manage their own exceptions.  

https://blogs.oracle.com/java-platform-group/entry/upcoming_exception_site_list_in

Anyone can give us a hand with the script?

Thank you!
0
Comment
Question by:llarava
  • 6
  • 5
  • 2
13 Comments
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
I recommend to change that to use a corporate (system) file and a user file, and merging those together on login. Otherwise you'll need to check whether the corporate sites are not already in the exception list - nothing really difficult, but sounds unnecessary.
0
 

Author Comment

by:llarava
Comment Utility
Yes, that makes sense. How do you get that implemented?
0
 

Author Comment

by:llarava
Comment Utility
So far I have the following:

Const ForAppending = 8
Const Open_as_Unicode = True

strFolder = "C:\Users\testuser\AppData\LocalLow\Sun\Java\Deployment\security"
strFile = "exception.sites"

strPath = strFolder & "\" & strFile
Set FSO = CreateObject("Scripting.FileSystemObject")

If (FSO.FileExists(strPath) = True) Then
  Set objFile = FSO.OpenTextFile(strPath, ForAppending, Open_as_Unicode)
  objFile.Writeline("test")
    objFile.Close : Set objFile = Nothing
End If

But I need to change - strFolder = "C:\Users\testuser\AppData\LocalLow\Sun\Java\Deployment\security" for a variable that captures the different users.

Also a way to control that sites are only added one time.

Could someone help to adjust to script?

Thank you!
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
Hi, I haven't tested this yet, but it should work.

Regards,

Rob.

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const Open_as_Unicode = True

Set objNetwork = CreateObject("WScript.Network")

strFolder = "C:\Users\" & objNetwork.UserName & "\AppData\LocalLow\Sun\Java\Deployment\security"
strFile = "exception.sites"

strPath = strFolder & "\" & strFile
Set FSO = CreateObject("Scripting.FileSystemObject")

Set dctURLs = CreateObject("Scripting.Dictionary")
dctURLs.CompareMode = vbTextCompare

' Add corporate URLs that are required here
dctURLs.Add "http://URL1", 0
dctURLs.Add "http://URL2", 0

If (FSO.FileExists(strPath) = True) Then
	Set objFile = FSO.OpenTextFile(strPath, ForReading, Open_as_Unicode)
	' Read through the current list to maintain any user added URLs
	While Not objFile.AtEndOfStream
		strURL = Trim(objFile.ReadLine)
		If strURL <> "" Then
			If dctURLs.Exists(strURL) = False Then dctURLs.Add strURL, 0
		End If
	Wend
	objFile.Close
	Set objFile = Nothing
	Set objFile = FSO.CreateTextFile(strPath, True, Open_as_Unicode)
	For Each strURL In dctURLs
		objFile.WriteLine strURL
	Next
	objFile.Close
	Set objFile = Nothing
End If 

Open in new window

0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
That script assumes that the root folder for user profiles is C:\Users, but I reckon that's sufficient in your case. Since it is only adding URLs, you can't tell to remove, but should you need that too, it is nothing difficult to implement.
0
 

Author Comment

by:llarava
Comment Utility
Hi Rob,

Thanks for your reply.

I have executed the script 3 times and the URLs are added to the file every time the script is executed as shown below,

Notice how the first time the URL's are being added correctly then they are added again in a strange format, etc.

Since the script is going to run as part as the login script can the URL's be added just once.  

http://URL1
http://URL2
ÿþh t t p : / / U R L 1
 
 h t t p : / / U R L 2
 
 ÿ þ h   t   t   p   :   /   /   U   R   L   1  
   
 
   h   t   t   p   :   /   /   U   R   L   2  

Any thoughts?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
Hmmm, try changing
Const Open_as_Unicode = True

Open in new window


to
Const Open_as_Unicode = -1

Open in new window


Rob.
0
 

Author Comment

by:llarava
Comment Utility
I I have made the change as indicated but still


I have made the changed as indicated. I have executed the script 3 times again and this is the content of the file.

http://URL1
http://URL2
ÿþh t t p : / / U R L 1
 
 h t t p : / / U R L 2
 
 ÿ þ h   t   t   p   :   /   /   U   R   L   1  
   
 
   h   t   t   p   :   /   /   U   R   L   2  
   
 
   ÿ   þ
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
Perhaps it's not supposed to be Unicode....try this.

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const Open_as_Unicode = -1

Set objNetwork = CreateObject("WScript.Network")

strFolder = "C:\Users\" & objNetwork.UserName & "\AppData\LocalLow\Sun\Java\Deployment\security"
strFile = "exception.sites"

strPath = strFolder & "\" & strFile
Set FSO = CreateObject("Scripting.FileSystemObject")

Set dctURLs = CreateObject("Scripting.Dictionary")
dctURLs.CompareMode = vbTextCompare

' Add corporate URLs that are required here
dctURLs.Add "http://URL1", 0
dctURLs.Add "http://URL2", 0

If (FSO.FileExists(strPath) = True) Then
	Set objFile = FSO.OpenTextFile(strPath, ForReading, 0)
	' Read through the current list to maintain any user added URLs
	While Not objFile.AtEndOfStream
		strURL = Trim(objFile.ReadLine)
		If strURL <> "" Then
			If dctURLs.Exists(strURL) = False Then dctURLs.Add strURL, 0
		End If
	Wend
	objFile.Close
	Set objFile = Nothing
	Set objFile = FSO.CreateTextFile(strPath, True, 0)
	For Each strURL In dctURLs
		objFile.WriteLine strURL
	Next
	objFile.Close
	Set objFile = Nothing
End If 

Open in new window


If that still fails, try to copy an unmodified file back to your user account and try the script again.

Rob.
0
 

Author Comment

by:llarava
Comment Utility
Hi,

I made the change and the script works like a champ. One more quick question. The "exception.sites" doesn't get created until the user executes the Java Exception List and adds a file to it.

Could the script create the the file "exception.sites" in the following path:

User\AppData\LocalLow\Sun\Java\Deployment\security\

Then append the sites in the script to it.

Finally if the file "exception.sites" exists then do not create the file but execute the part of the script that adds/checks if the internal sites are there.

Thank you!
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
Comment Utility
Sure, I think just moving the CreateTextFile part out of the If statement should do that.

Regards,

Rob.

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const Open_as_Unicode = -1

Set objNetwork = CreateObject("WScript.Network")

strFolder = "C:\Users\" & objNetwork.UserName & "\AppData\LocalLow\Sun\Java\Deployment\security"
strFile = "exception.sites"

strPath = strFolder & "\" & strFile
Set FSO = CreateObject("Scripting.FileSystemObject")

Set dctURLs = CreateObject("Scripting.Dictionary")
dctURLs.CompareMode = vbTextCompare

' Add corporate URLs that are required here
dctURLs.Add "http://URL1", 0
dctURLs.Add "http://URL2", 0

If (FSO.FileExists(strPath) = True) Then
	Set objFile = FSO.OpenTextFile(strPath, ForReading, 0)
	' Read through the current list to maintain any user added URLs
	While Not objFile.AtEndOfStream
		strURL = Trim(objFile.ReadLine)
		If strURL <> "" Then
			If dctURLs.Exists(strURL) = False Then dctURLs.Add strURL, 0
		End If
	Wend
	objFile.Close
	Set objFile = Nothing
End If
Set objFile = FSO.CreateTextFile(strPath, True, 0)
For Each strURL In dctURLs
	objFile.WriteLine strURL
Next
objFile.Close
Set objFile = Nothing

Open in new window

0
 

Author Comment

by:llarava
Comment Utility
PERFECT! Thank you!
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
No problem. Thanks for the grade.

Rob.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now