Hello Guys,

Please who can point me in the right direction with regards to auto generating a ruleset text string from a list of url's.

I get a list of URL's in the format:

example.com/firstplace/secondplace.jpg
"
"
"
"
etc

I would like to be able to read the url's into memory and then construct a text file with entries like

alert tcp any any -> any any (content:"example.com"; uricontent:"/firstplace/secondplace.jpg"; nocase; sid:17000001; rev:1;)

Please note the URL is split in 2 starting from the first slash, and the other texts are auto generated including an alphanumeric incremental number sid:17000001 which i'd like to auto increment per line.

Please any pointers will be well received.

Kind regards

Dipopo

See if this helps.

Public Class Form1
    Private myCoolFile As String = "C:\test.txt" '// your File. Make sure there is NO Previous "C:\test.txt" File.
    Private arlUrlList As New ArrayList '// ArrayList to add your Urls.

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        '// Add some Urls to the ArrayList for testing.
        arlUrlList.Add("daniweb.com/forums/thread344440.html")
        arlUrlList.Add("daniweb.com/forums/forum58.html")
        arlUrlList.Add("google.com/firefox?client=firefox-a&rls=org.mozilla:en-US:official")

        Dim iSid As Integer = 17000001 '// your sid #.
        Dim myWriter As New IO.StreamWriter(myCoolFile) '// Open a StreamWriter to Save Urls to File.
        For Each myItem As String In arlUrlList '// Loop thru all items in Urls List.
            '// write to File( .Substring from 0 to the First Index of "/" ; .Substring from the First Index of "/" to end of String ; and your sid # )
            myWriter.WriteLine(myItem.Substring(0, myItem.IndexOf("/")) & ";" & myItem.Substring(myItem.IndexOf("/")) & ";sid:" & iSid)
            iSid += 1 '// Increment the sid # + 1.
        Next
        myWriter.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If IO.File.Exists(myCoolFile) Then
            Dim arMyFileLines() As String = IO.File.ReadAllLines(myCoolFile) '// Load each line as a String Array.
            Dim arTemp() As String '// .Split each Line.
            For Each myLine As String In arMyFileLines '// Lopp thru all Lines.
                arTemp = myLine.Split(";"c) '// .Split Line by the ";" char.
                MsgBox(arTemp(0) & vbNewLine & arTemp(1) & vbNewLine & arTemp(2)) '// Display result.
            Next
        Else '// For Testing Purposes: Restarts to Save the File if it does not exist.
            Application.Restart()
        End If
    End Sub
End Class
commented: Codeorder is a fantastic helper. I am highly appreciative of his work! +0

Thanks codeorder, I absolutely appreciate this gesture, you are a star!!!! I tried it out using visual studio, created a vb.net windows forms application and pasted the code there....ran debug and the application looped.

I must be doing something wrong, apologies for stating the obvious, any guidance will be well appreciated.

Thanks

Dipopo

It should only loop the first time, until it creates the file.
If needed, remove the:

Else '// For Testing Purposes: Restarts to Save the File if it does not exist.
            Application.Restart()

Or rename your file to a Directory available on your p.c..

Private myCoolFile As String = "C:\test.txt" '// your File. Make sure there is NO Previous "C:\test.txt" File.

Thanks Codeorder, awesome stuff. I have re-written to save the urls to file:

Dim myWriter As New IO.StreamWriter("C:\myCoolFile.txt")

and get this as output, will now try and remove the msgbox notification:

MsgBox(arTemp(0) & vbNewLine & arTemp(1) & vbNewLine & arTemp(2)) '// Display result.

Currently getting the below as output:

daniweb.com;/forums/thread344440.html;sid:17000001
daniweb.com;/forums/forum58.html;sid:17000002
google.com;/firefox?client=firefox-a&rls=org.mozilla:en-US:official;sid:17000003


Will try and change:

myWriter.WriteLine(myItem.Substring(0, myItem.IndexOf("/")) & ";" & myItem.Substring(myItem.IndexOf("/")) & ";sid:" & iSid)
            iSid += 1 '// Increment the sid # + 1.

so i can get below as final output.

alert tcp any any -> any any (content:"example.com"; uricontent:"/firstplace/secondplace.jpg"; nocase; sid:17000001; rev:1;

Thanks dude, I appreciate this immensely.

Hi Codeorder,

Please how do i get:

myWriter.WriteLine(myItem.Substring(0, myItem.IndexOf("/")) & ";" & myItem.Substring(myItem.IndexOf("/")) & ";sid:" & iSid)
iSid += 1 '// Increment the sid # + 1.

to output:

alert tcp any any -> any any (content:"example.com"; uricontent:"/firstplace/secondplace.jpg"; nocase; sid:17000001; rev:1;

Kind regards

Dipopo

myWriter.WriteLine("alert tcp any any -> any any (content:" & myItem.Substring(0, myItem.IndexOf("/")) & "; uricontent:" & myItem.Substring(myItem.IndexOf("/")) & "; nocase" & "; sid:" & iSid & "; rev:1;")

And to load, you would use:

MsgBox(arTemp(0) & vbNewLine & arTemp(1) & vbNewLine & arTemp(2) & vbNewLine & arTemp(3) & vbNewLine & arTemp(4)) '// Display result.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.