I just recorded this hack attempt and I'm wondering if my code is secure. I'm also curious about what this qs injection is trying to do.
Code:
Error in page http://www.mydomain.org.uk/contactForm.aspx?officer=61'+or+1=@@version
The contact form does two things. It gets a querystring from another page, and populates a literal control to find in the recipients name. The visitor can then write a message. So the second thing it does is send the email using the same querystring value. So I'm wondering if I need do anything to secure the form further. (email addresses removed for obvious reasons). Is there any benefit to be gained by using "if len(request.querystring,3)" for example?
Code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If Page.IsValid Then
'8 officers who can receive email so only allow integers 1-8)
If Request.QueryString("officer") > 0 AndAlso Request.QueryString("officer") < 9 Then
Dim officerid As Integer = CInt(Request.QueryString("officer"))
Select Case officerid
Case 1
myMessage.To.Add(New MailAddress("***", "***"))
Case 2
myMessage.To.Add(New MailAddress("***", "***"))
Case 3
myMessage.To.Add(New MailAddress("***", "***"))
Case 4
myMessage.To.Add(New MailAddress("***", "***"))
Case 5
myMessage.To.Add(New MailAddress("***", "***"))
Case 6
myMessage.To.Add(New MailAddress("***", "***"))
Case 7
myMessage.To.Add(New MailAddress("***", "***"))
Case 8
myMessage.To.Add(New MailAddress("***", "***"))
Case Else
myMessage.To.Add(New MailAddress("***", "Webmaster"))
End Select
Dim fileName As String = Server.MapPath("~/App_Data/ContactForm.txt")
Dim mailBody As String = String.Empty
mailBody = System.IO.File.ReadAllText(fileName)
mailBody = mailBody.Replace("##Name##", Name.Text)
mailBody = mailBody.Replace("##Email##", email.Text)
mailBody = mailBody.Replace("##Phone##", phone.Text)
mailBody = mailBody.Replace("##Subject##", Subject.Text)
mailBody = mailBody.Replace("##Enquiry##", enquiry.Text)
Dim myMessage As MailMessage = New MailMessage()
myMessage.Subject = "Enquiry from the U3A web site"
myMessage.Body = mailBody
myMessage.From = New MailAddress(email.Text, Name.Text)
myMessage.Bcc.Add(New MailAddress("***", "Pembs U3A admins"))
Dim mySmtpClient As SmtpClient = New SmtpClient()
Try
mySmtpClient.Send(myMessage)
Catch ex As Exception
Literal2.Text = "An error occured trying to send your message"
End Try
Literal2.Visible = True
formtable.Visible = False
End If
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Literal1.Text = "Email "
If Len(Request.QueryString("Officerid")) < 3 Then
Dim officerid As Integer = CInt(Request.QueryString("officer"))
Select Case officerid
Case 1
Literal1.Text &= "Nova"
Case 2
Literal1.Text &= "Rhoda"
Case 3
Literal1.Text &= "Pauline and Arthur"
Case 4
Literal1.Text &= "Terry"
Case 5
Literal1.Text &= "Graham"
End Select
End If
Else
Literal1.Text = "Your email has been sent"
End If
End Sub