myBlog myBlog Home

Back to Blog MTNCOMP


DNS analysis, CNAME vs A records

Published: Sunday, December 30, 2018 written by Drew Jackson
View Count: 77
Keywords: DNS analysis, CNAME, A, AAAA



 
when analyzing databases, this is why things get interesting and you need worker threads and async operations.
 
 
Imports System.Data.SqlClient
Imports System.Threading
Public Class Form1
    Inherits System.Windows.Forms.Form
    'Create ADO.NET objects.
    Private myConn As SqlConnection
    Private myCmd As SqlCommand
    Private myReader As SqlDataReader
    Private results As String
    Private maxDBrecords As Integer
    Private DbDNS As String
    Private DbDNSTable As String
    Private DataSourceIPAddress As String

    Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
        Application.Exit()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        RichTextBox1.Clear()
        CbDNSdb.Items.Add("DNSRecords1")
        CbDNSdb.Items.Add("DNSRecords2")
        CbDNSdb.Items.Add("DNSRecords3")
        CbDNSdb.Items.Add("DNSRecords4")
        CbDNSdb.Items.Add("DNSRecords5")
        CbServerIP.Items.Add("xxxxxxxxx")
        CbServerIP.Items.Add("xxxxxxxxxxx")
        CbServerIP.Items.Add("xxxxxxxxxxxxxx")
        TbIP.Text = "xxxxxxxxx"
        TbTable.Text = "DNSRecords1"
        DataSourceIPAddress = TbIP.Text
        DbDNSTable = TbTable.Text
        DbDNS = "dbDNS"
        ListStatus.Items.Add("App loaded.")

        'Call ConnectDB()
    End Sub

    Private Sub ConnectDB(myquery, numparams)
        ListStatus.Items.Add("Konnecting 2 db.")
        'Create a Connection object.
        myConn = New SqlConnection("Initial Catalog=" & DbDNS & ";" & "Data Source=tcp:" & DataSourceIPAddress & ";User ID=xxxxxxxxxxxx;Password=xxxxxxxxxxxx")
        'Create a Command object.
        myCmd = myConn.CreateCommand
        'myCmd.CommandText = "SELECT max(row_id) FROM DNSRecords1"
        myCmd.CommandText = myquery

        ListStatus.Items.Add("Opening db.")
        'Open the connection.
        myConn.Open()

        results = ""
        myReader = myCmd.ExecuteReader()

        ListStatus.Items.Add("Reading db.")
        'Concatenate the query result into a string.
        Do While myReader.Read()
            Select Case numparams

                Case 0
                    'don't return anything, just execute query
                Case 1
                    results = results & myReader(0) & vbCrLf
                Case 2
                    results = results & myReader(0) & vbTab & myReader(1) & vbCrLf
                Case 3
                    results = results & myReader(0) & vbTab & myReader(1) & vbTab & myReader(2) & vbCrLf
                Case 4
                    results = results & myReader(0) & vbTab & myReader(1) & vbTab & myReader(2) & vbTab & myReader(3) & vbCrLf
                Case 5
                    results = results & myReader(0) & vbTab & myReader(1) & vbTab & myReader(2) & vbTab & myReader(3) & vbTab & myReader(4) & vbCrLf

                Case Else

            End Select
            If numparams > 1 Then
                ListResults.Items.Add(results)
                results = ""
            End If

        Loop
        'Display results.
        'MsgBox(results)
        ListResults.Items.Add(results)
        'Close the reader and the database connection.
        myReader.Close()
        ListStatus.Items.Add("Klozing db.")
        myConn.Close()

    End Sub

    Private Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click
        If iStart.Text <> "" And iEnd.Text <> "" Or zMyQuery.Text <> "" Then
            Debug.Print("good. start and end or query")
            If zMyQuery.Text <> "" Then
                Call ConnectDB(zMyQuery.Text, 1)
            Else
                Dim s
                s = "select * from " & TbTable.Text & " WHERE row_id >= " & iStart.Text & " and row_id <= " & iEnd.Text
                Call ConnectDB(s, 5)
            End If
        Else
            ListResults.Items.Add("no good. nothign entered to find.")
            Debug.Print("no good. nothing entered to find.")
        End If

    End Sub

    Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
        ListResults.Items.Clear()
    End Sub

    Private Sub BtnProcessDB_Click(sender As Object, e As EventArgs) Handles BtnProcessDB.Click

        'process the database
        Dim x, y, d
        x = 0   'hits
        y = 0   'tries, misses
        d = "" 'domain name
        If ChkBoxStopProcessing.Checked Then
            ChkBoxStopProcessing.Checked = False

        End If
        LblStartDateTime.Text = Now()
        Dim s
        s = "select max(row_id) from " & TbTable.Text
        Call ConnectDB(s, 1)
        maxDBrecords = results
        Debug.Print("maxrecords=" & maxDBrecords)
        ListStatus.Items.Add("r=" & maxDBrecords)
        'myConn = New SqlConnection("Initial Catalog=dbDNS;Data Source=localhost;User ID=xxxxxxxxxxxx;Password=xxxxxxxxxxxx")
        myConn = New SqlConnection("Initial Catalog=" & DbDNS & ";Data Source=tcp:" & DataSourceIPAddress & ";User ID=xxxxxxxxxxxx;Password=xxxxxxxxxxxx")
        ListStatus.Items.Add("Opening db.")
        myConn.Open()
        ListStatus.Items.Add("Reading db.")

        Dim r
        Do Until ChkBoxStopProcessing.CheckState = CheckState.Checked
            Application.DoEvents()
            'r = CInt(Math.Ceiling(Rnd() * maxDBrecords) + 1)
            Static Generator As System.Random = New System.Random()
            r = Generator.Next(1, maxDBrecords)
            'as long as r is within bounds
            If r > 0 And r < maxDBrecords Then
                Application.DoEvents()
                Debug.Print("r = " & r)
                myCmd = myConn.CreateCommand
                s = "select domain_name from " & TbTable.Text & " WHERE substring(recordtype,9,5) = 'cname' and been_processed is null and row_id = " & r
                myCmd.CommandText = s
                myReader = myCmd.ExecuteReader()
                Do While myReader.Read()
                    results = results & myReader(0) & vbCrLf
                    'ListResults.Items.Add("[" & x & "]" & r & "," & results)
                    'x = x + 1
                    'LblMatches.Text = x.ToString
                    d = Mid(results, 9, Len(results) - 11)
                    Debug.Print("domainame = " & results & " - " & d)
                    'the cname / domain has www. in it even better, ignore everything else
                    If InStr(d, ".com") > 0 And InStr(d, "www.") > 0 And Len(d) < 32 Then
                        x = x + 1
                        LblMatches.Text = x.ToString
                        ListResults.Items.Add("[" & x & "]" & r & "," & results)
                        Application.DoEvents()
                        Call GetWebText(r, d)
                    End If
                Loop
                results = ""
                y = y + 1
                LblTries.Text = y.ToString
                myReader.Close()
                If x Mod 20 = 0 Then
                    ListResults.Items.Clear()
                    'ListStatus.Items.Clear()
                End If
            End If

        Loop

        ListStatus.Items.Add("Klozing db.")
        myConn.Close()
        'analyze and score the records
        'repeat in this loop, and pause (remember there are other workers involved)
    End Sub

    Private Sub BtnBetaTest_Click(sender As Object, e As EventArgs) Handles BtnBetaTest.Click

        'get a webpage, naming my homepage
        Dim s
        s = "https://mountaincomputers.org"
        s = InputBox("enter domain", "get domain page", s)
        Debug.Print("get my home page")
        'Dim sourceString As String = New System.Net.WebClient().DownloadString("https://mountaincomputers.org")
        Dim sourceString As String = New System.Net.WebClient().DownloadString(s)
        'Debug.Print(sourceString)
        RichTextBox1.Text = ""
        RichTextBox1.Text = sourceString

    End Sub

    Private Sub GetWebText(r As Integer, v As String)
        Dim myConn2 As SqlConnection
        Dim myCmd2 As SqlCommand
        Dim s
        Try
            Dim sourceString As String = New System.Net.WebClient().DownloadString("http://" & v)
            Application.DoEvents()
            'myConn2 = New SqlConnection("Initial Catalog=dbDNS;Data Source=localhost;User ID=xxxxxxxxxxxx;Password=xxxxxxxxxxxx")
            myConn2 = New SqlConnection("Initial Catalog=" & DbDNS & ";Data Source=tcp:" & DataSourceIPAddress & ";User ID=xxxxxxxxxxxx;Password=xxxxxxxxxxxx")
            myConn2.Open()
            myCmd2 = myConn2.CreateCommand
            s = "UPDATE " & TbTable.Text & " SET processed_datetime = getdate(), been_processed = 1, homepagetext = '" & GetSQLText(sourceString) & "' WHERE row_id = " & r
            myCmd2.CommandText = s
            myCmd2.ExecuteNonQuery()
            myConn2.Close()
            sourceString = ""
        Catch ex As Exception
            Debug.Print("error" & ex.Message & " trying to reach " & v)
        Finally
            'do nothing
        End Try

    End Sub

    Private Function GetSQLText(sText)
        Dim bEnd
        Dim i
        Dim iLast
        Dim sSQLText
        sSQLText = ""
        bEnd = False
        i = 1
        iLast = 1
        While Not bEnd
            i = InStr(iLast, sText, "'")
            If i > 0 Then
                sSQLText = sSQLText & Mid(sText, iLast, i - iLast) & "''"
                iLast = i + 1
            Else
                sSQLText = sSQLText & Mid(sText, iLast)
                bEnd = True
            End If
        End While
        GetSQLText = sSQLText
    End Function

    Private Sub CbDNSdb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CbDNSdb.SelectedIndexChanged
        'change db table
        TbTable.Text = CbDNSdb.SelectedItem
    End Sub

    Private Sub CbServerIP_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CbServerIP.SelectedIndexChanged
        'change server ip
        TbIP.Text = CbServerIP.SelectedItem
    End Sub

    Private Sub BtnShowStats_Click(sender As Object, e As EventArgs) Handles BtnShowStats.Click
        'show the stats in the richtextbox1
        Dim s, txt
        s = ""
        txt = ""
        RichTextBox1.Text = ""
        Try
            s = "select max(row_id) from " & TbTable.Text
            Call ConnectDB(s, 1)
            txt = "Max Records: " & results
            s = "select count(*) from " & TbTable.Text & " WHERE been_processed = 1"
            Call ConnectDB(s, 1)
            txt = txt & vbCrLf & "Processed: " & results
            RichTextBox1.Text = txt
        Catch ex As Exception
            Debug.Print("error" & ex.Message & " ,getting stats error. ")
        Finally
            'do nothing
        End Try
    End Sub
End Class

 

if you found this article helpful, consider contributing $10, 20 or so..to the author. more authors coming soon
FYI we use paypal or patreon, patreon has 3x the transaction fees, so we don't, not yet.

© 2019 myBlog™ v1.1 All rights reserved. We count views as reads, so let's not over think it.