Getting error "Invalid CSRF" when trying HttpWebRequest Post data

Free6

Newbie
Joined
Nov 21, 2016
Messages
47
Reaction score
1
Hi guys, have been struggling for more than 2 weeks now to solve this issue.
Can you please help me out?

I'm trying to post data and i'm getting {"status":403,"message":"Invalid CSRF"}.
I successfully logged in and set cookies on login and submitting page but when I try to submit the form (post httpwebrequest data) i'm getting {"status":403,"message":"Invalid CSRF"}.

I know that the web site has CSRF features for security, and I also use Fiddler to capture Http Traffcic.

Any idea is welcomed.

Please see code below code from Fiddler, it doesn't work, giving the above error.

Code:
Module zModPostOnly1

    Public cookieGUID As New CookieContainer

    Public Sub MakeRequestsPOSTOnly1()
        Dim response As HttpWebResponse
        Dim responseText As String

        If Request_www_gumtree_co_za(response) Then
            responseText = ReadResponse(response)

            Form1.TextBox1.Clear()
            Form1.TextBox1.Text = responseText

            response.Close()
        End If
    End Sub

    Private Function ReadResponse(response As HttpWebResponse) As String
        Using responseStream = response.GetResponseStream()
            Dim streamToRead As Stream = responseStream
            If response.ContentEncoding.ToLower().Contains("gzip") Then
                streamToRead = New GZipStream(streamToRead, CompressionMode.Decompress)
            ElseIf response.ContentEncoding.ToLower().Contains("deflate") Then
                streamToRead = New DeflateStream(streamToRead, CompressionMode.Decompress)
            End If

            Using streamReader = New StreamReader(streamToRead, Encoding.UTF8)
                Return streamReader.ReadToEnd()
            End Using
        End Using
    End Function

    Private Function Request_www_gumtree_co_za(ByRef response As HttpWebResponse) As Boolean
        response = Nothing

        Try
            Dim request As HttpWebRequest = DirectCast(WebRequest.Create("https://www.gumtree.co.za/api/postad/create"), HttpWebRequest)

            Dim tempCookies As New CookieContainer

            request.KeepAlive = True

            Console.WriteLine("csrf-token *** : " & Form1.TextBox4.Text.Trim)

            request.Headers.Add("Origin", "https://www.gumtree.co.za")

            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"


            request.CookieContainer = xlogincookie2

            request.ContentType = "application/json"
            request.Accept = "*/*"

            Form1.TextBox8.AppendText(request.Headers.ToString)

            request.Referer = "https://www.gumtree.co.za/post.html"
            request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip, deflate, br")
            request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.9")
            'request.Headers.Set(HttpRequestHeader.Cookie, "machguid=fe4212ed-890c-41d2-87a2-ae59dbdf0d07-16548470c23; _ga=GA1.3.367387851.1534515940; cto_lwid=f27487ae-6fae-429c-92d9-876df681c3aa; aam_uuid=68350233536367343452692645319460952219; __gads=ID=10f20cd6dbb15e")
            request.Headers.Add("DNT", "1")

            request.Method = "POST"
            request.ServicePoint.Expect100Continue = False

            Dim body As String = "{""ads"":[{""title"":"".Net Developer 6"",""categoryId"":9247,""location"":{""address"":""City Centre"",""latitude"":-33.9242692,""longitude"":18.4187029,""radius"":0},""categoryAttributes"":[{""name"":""JobType"",""value"":""fulltime""},{""name"":""AdvertisedBy"",""value"":""agency""},{""name"":""EmploymentEquity"",""value"":""eeaa""}],""description"":""<p>Hi jsdhkj kj hdfldkklsdkl lkjsdklfskl&nbsp;</p><p>sdkjfskldj kljklsdklsdkl</p><p>sdkljfsdjkl&nbsp;</p><p><br></p><p>sdkfjslkdjfklsjklkjdkfjgkldjkl kldfkld</p><p>dkfgkldjfkldklgfkldkm kl dmfkdkjhfglsmkdkj kl lsmldfl klsdklfskd ksdklfkl lks&nbsp;&nbsp;</p><p>sdk jlskdfsdkfs</p>"",""imageUrls"":[""https://i.ebayimg.com/00/s/NjAwWDgwMA==/z/0d0AAOSwAnlboQgC/$_18.JPG?set_id=8800005007""],""phone"":""0784952369"",""pafVersion"":""3.0""}]}"

            Dim postBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(body)
            request.ContentLength = postBytes.Length
            Dim stream As Stream = request.GetRequestStream()
            stream.Write(postBytes, 0, postBytes.Length)


            stream.Close()

            response = DirectCast(request.GetResponse(), HttpWebResponse)
        
            Console.WriteLine("*** Code POST : " & response.StatusCode & "  *** Description POST : " & response.ResponseUri.Query)

            tempCookies.Add(response.Cookies)


        Catch e As WebException
            If e.Status = WebExceptionStatus.ProtocolError Then
                response = DirectCast(e.Response, HttpWebResponse)
            Else
                Return False
            End If
        Catch e As Exception
            If response IsNot Nothing Then
                response.Close()
            End If
            Return False
        End Try

        Return True
    End Function

End Module
 
You are not sending the valid CSRF token. Same issue as your last thread - You MUST load the first page, using the same cookiecontainer, scrape the csrf token & then use that token to post to the next page. You cannot use old tokens or manually enter tokens to use.

If you are still getting the same issue after this then you are not correctly scraping the csrf token or scraping an incorrect token, their may be more than one on the same page for different forms.
 
I checked and double checked, there's only one CSRF Token; I checked it the whole days.

Can you please help me out?
 
I managed to upload the fiddler session archive into Google Drive,
this is the link ( drive(DOT)google(DOT)com/file/d/1URCMkid6OjOJO_bS6REkXgGyttlLHnt2/view?usp=sharing )

please change (DOT) to .

The fiddler session archive contents the whole process from login, filling the form, submitting the form and posting.
 
I checked and double checked, there's only one CSRF Token; I checked it the whole days.

Can you please help me out?

You are skipping the CSRF token completely it would seem. Compare your data to the real data to find the differences to fix.
 
Back
Top