Пример #1
0
        public void FindSequence(
            IEnumerable <KeyValuePair <HttpRequestInfo, HttpResponseInfo> > availSequence,
            IEnumerable <KeyValuePair <HttpRequestInfo, HttpResponseInfo> > expectedSequence,
            TrafficServerMode proxyMode,
            bool ignoreAuth)
        {
            TrafficViewerFile mockSite = new TrafficViewerFile();

            //add a few requests
            mockSite.AddRequestResponse("GET / HTTP/1.1\r\nHost:demo\r\n\r\n", "HTTP/1.1 200 OK\r\n\r\n");
            mockSite.AddRequestResponse("POST /login HTTP/1.1\r\nHost:demo\r\n\r\n", "HTTP/1.1 302 Redirect\r\n\r\n");
            //add the sequence
            foreach (KeyValuePair <HttpRequestInfo, HttpResponseInfo> reqRespInfo in availSequence)
            {
                mockSite.AddRequestResponse(reqRespInfo.Key.ToString(), reqRespInfo.Value.ToString());
            }
            //add some more requests
            mockSite.AddRequestResponse("GET /main.aspx HTTP/1.1\r\nHost:demo\r\n\r\n", "HTTP/1.1 200 OK\r\n\r\n");

            //now send the expected sequence
            TrafficStoreHttpClient client = new TrafficStoreHttpClient(mockSite, proxyMode, ignoreAuth);

            foreach (KeyValuePair <HttpRequestInfo, HttpResponseInfo> sentReqResp in expectedSequence)
            {
                HttpResponseInfo receivedResp = client.SendRequest(sentReqResp.Key);
                if (receivedResp.Status != 404)
                {
                    Assert.IsNotNull(receivedResp.Headers["Traffic-Store-Req-Id"]);          //verify that the request id is being added
                    receivedResp.Headers.Remove("Traffic-Store-Req-Id");                     //remove the request id from the comparison
                }

                Assert.AreEqual(sentReqResp.Value.ToString(), receivedResp.ToString());
            }
        }
Пример #2
0
        public void RemoveARangeOfRequests()
        {
            string originalRequest  = "GET / HTTP/1.1";
            string originalResponse = "HTTP/1.1 200 OK";

            List <int> addedRequests = new List <int>();

            TrafficViewerFile file = new TrafficViewerFile();

            addedRequests.Add(file.AddRequestResponse(originalRequest, originalResponse));
            addedRequests.Add(file.AddRequestResponse(originalRequest, originalResponse));

            //add one more
            int savedRequestId = file.AddRequestResponse(originalRequest, originalResponse);

            addedRequests.Add(file.AddRequestResponse(originalRequest, originalResponse));
            addedRequests.Add(file.AddRequestResponse(originalRequest, originalResponse));

            Assert.AreEqual(5, file.RequestCount);

            file.RemoveRequestBatch(addedRequests);


            Assert.AreEqual(1, file.RequestCount);

            Assert.IsNotNull(file.GetRequestInfo(savedRequestId));

            file.Close(false);
        }
Пример #3
0
        public void Test_HTTP_ReusingClient()
        {
            string testRequest1  = "GET http://site.com/ HTTP/1.1\r\n\r\n";
            string testResponse1 = "HTTP/1.1 302 Redirect\r\nLocation: http://site.com/a\r\n\r\n";
            string testRequest2  = "GET http://site.com/a HTTP/1.1\r\n\r\n";
            string testResponse2 = "HTTP/1.1 200 OK\r\n\r\n";

            WebRequestClient client = new WebRequestClient();

            TrafficViewerFile mockSite = new TrafficViewerFile();

            mockSite.AddRequestResponse(testRequest1, testResponse1);
            mockSite.AddRequestResponse(testRequest2, testResponse2);
            TrafficViewerFile dataStore = new TrafficViewerFile();
            MockProxy         mockProxy = new MockProxy(dataStore, mockSite);

            mockProxy.Start();

            client.SetProxySettings(mockProxy.Host, mockProxy.Port, null);

            CheckResponseStatus(testRequest1, client, 302);
            CheckResponseStatus(testRequest2, client, 200);

            mockProxy.Stop();
        }
Пример #4
0
        private TrafficViewerFile removeSimilar(TrafficViewerFile source)
        {
            TrafficViewerFile dest = new TrafficViewerFile();
            TVRequestInfo     info;
            int        id         = -1;
            List <int> _reqHashes = new List <int>();

            while ((info = source.GetNext(ref id)) != null)
            {
                byte[]          request = source.LoadRequestData(info.Id);
                HttpRequestInfo reqInfo = new HttpRequestInfo(request, true);
                int             hash    = reqInfo.GetHashCode(TrafficServerMode.BrowserFriendly);

                if (!_reqHashes.Contains(hash))
                {
                    byte[] response = source.LoadResponseData(info.Id);
                    dest.AddRequestResponse(request, response);
                    _reqHashes.Add(hash);
                }
            }

            //copy profile over
            dest.Profile = source.Profile;
            return(dest);
        }
Пример #5
0
        protected void SendTestRequestToMockProxy(HttpRequestInfo testRequest,
                                                  HttpResponseInfo testResponse, out HttpRequestInfo receivedRequest, out HttpResponseInfo receivedResponse, int proxyPort = 0)
        {
            TrafficViewerFile mockSite  = new TrafficViewerFile();
            TrafficViewerFile dataStore = new TrafficViewerFile();

            MockProxy mockProxy = new MockProxy(dataStore, mockSite, "127.0.0.1", proxyPort, 0);

            mockProxy.Start();

            //change the requests host and port to be the ones of the mock proxy
            testRequest.Host = mockProxy.Host;
            testRequest.Port = mockProxy.Port;

            mockSite.AddRequestResponse(testRequest.ToArray(false), testResponse.ToArray());

            IHttpClient client = GetHttpClient();

            receivedResponse = client.SendRequest(testRequest);
            // check what was received in the proxy
            byte[] receivedRequestBytes = dataStore.LoadRequestData(0);
            if (receivedRequestBytes == null)
            {
                receivedRequest = null;
            }
            else
            {
                receivedRequest = new HttpRequestInfo(receivedRequestBytes);
            }
            mockProxy.Stop();
        }
Пример #6
0
        protected void SendTestRequestThroughMockProxy(HttpRequestInfo testRequest,
                                                       HttpResponseInfo testResponse, out HttpRequestInfo receivedRequest, out HttpResponseInfo receivedResponse,
                                                       ClientType clientType = ClientType.WebRequestClient, int proxyPort = 0)
        {
            TrafficViewerFile mockSite = new TrafficViewerFile();

            mockSite.AddRequestResponse(testRequest.ToArray(true), testResponse.ToArray());
            TrafficViewerFile dataStore = new TrafficViewerFile();
            MockProxy         mockProxy = new MockProxy(dataStore, mockSite, "127.0.0.1", proxyPort, 0);

            mockProxy.Start();

            IHttpClient client = GetHttpClient(mockProxy.Port);

            receivedResponse = client.SendRequest(testRequest);
            // check what was received in the proxy

            byte [] receivedRequestBytes = dataStore.LoadRequestData(0);
            if (receivedRequestBytes == null)
            {
                receivedRequest = null;
            }
            else
            {
                receivedRequest = new HttpRequestInfo(receivedRequestBytes);
            }
            mockProxy.Stop();
        }
Пример #7
0
        public void TestPOSTRequestToProxy()
        {
            TrafficViewerFile dataStore = new TrafficViewerFile();
            TrafficViewerFile mockSite  = new TrafficViewerFile();

            string expectedResponseLine = "HTTP/1.1 200 OK";

            mockSite.AddRequestResponse(Resources.POSTRequest, expectedResponseLine);

            MockProxy proxy = new MockProxy(dataStore, mockSite);

            proxy.Start();

            IHttpClient httpClient = GetHttpClient(proxy.Port);

            HttpRequestInfo testRequestInfo = new HttpRequestInfo(Resources.POSTRequest);

            HttpResponseInfo respInfo = httpClient.SendRequest(testRequestInfo);

            Assert.AreEqual(200, respInfo.Status);

            HttpRequestInfo storedRequestInfo = new HttpRequestInfo(mockSite.LoadRequestData(0));

            Assert.AreEqual("uid=jsmith&passwd=Demo1234", storedRequestInfo.ContentDataString);

            proxy.Stop();
        }
Пример #8
0
        public void VerifyTempDir()
        {
            string originalRequest   = "GET / HTTP/1.1";
            string originalResponse  = "HTTP/1.1 200 OK";
            string predefinedTempDir = Environment.GetEnvironmentVariable("temp");

            TrafficViewerFile file = new TrafficViewerFile(predefinedTempDir);

            file.AddRequestResponse(originalRequest, originalResponse);

            Assert.IsTrue(Directory.Exists(file.TempFileFolder));

            file.Close(true);

            Assert.IsTrue(Directory.Exists(file.TempFileFolder));

            file = new TrafficViewerFile(predefinedTempDir);
            file.AddRequestResponse(originalRequest, originalResponse);

            Assert.IsTrue(Directory.Exists(file.TempFileFolder));


            file.Close(false);
            //after the file is closed the file gets closed with the option to not keep temp files
            Assert.IsFalse(Directory.Exists(file.TempFileFolder));
        }
Пример #9
0
        public void EditARequest()
        {
            string originalRequest  = "GET / HTTP/1.1";
            string originalResponse = "HTTP/1.1 200 OK";

            TrafficViewerFile file = new TrafficViewerFile();
            int reqId = file.AddRequestResponse(originalRequest, originalResponse);

            Assert.AreEqual(1, file.RequestCount);
            TVRequestInfo reqInfo = file.GetRequestInfo(reqId);

            string newRequest  = "POST /login HTTP/1.1";
            string newResponse = "HTTP/1.1 302 Redirect";

            file.SaveRequest(reqId, Encoding.UTF8.GetBytes(newRequest));
            file.SaveResponse(reqId, Encoding.UTF8.GetBytes(newResponse));

            //check the response info was updated
            Assert.AreEqual(newRequest, reqInfo.RequestLine);
            Assert.AreEqual("302", reqInfo.ResponseStatus);
            Assert.AreEqual(newRequest.Length, reqInfo.RequestLength);
            Assert.AreEqual(newResponse.Length, reqInfo.ResponseLength);

            string loadedRequest = Encoding.UTF8.GetString(file.LoadRequestData(reqId));

            Assert.AreEqual(newRequest, loadedRequest);
            string loadedResponse = Encoding.UTF8.GetString(file.LoadResponseData(reqId));

            Assert.AreEqual(newResponse, loadedResponse);
            file.Close(false);
        }
Пример #10
0
        public void TestDataStoreHasRequestAndResponse()
        {
            TrafficViewerFile dataStore            = new TrafficViewerFile();
            TrafficViewerFile mockSite             = new TrafficViewerFile();
            string            testRequest          = "GET http://site.com/a HTTP/1.1\r\n";
            string            expectedResponseLine = "HTTP/1.1 200 OK\r\n\r\n<body>";

            mockSite.AddRequestResponse(testRequest, expectedResponseLine);

            MockProxy proxy = new MockProxy(dataStore, mockSite);

            proxy.Start();

            IHttpClient httpClient = GetHttpClient(proxy.Port);

            HttpRequestInfo testRequestInfo = new HttpRequestInfo(testRequest);

            httpClient.SendRequest(testRequestInfo);

            byte[] testRequestBytes  = dataStore.LoadRequestData(0);
            byte[] testResponseBytes = dataStore.LoadResponseData(0);

            HttpRequestInfo reqInfo = new HttpRequestInfo(testRequestBytes);

            Assert.AreEqual(testRequestInfo.FullUrl, reqInfo.FullUrl);
            HttpResponseInfo respInfo = new HttpResponseInfo(testResponseBytes);

            Assert.AreEqual(200, respInfo.Status);
            Assert.AreEqual("<body>", respInfo.ResponseBody.ToString());

            proxy.Stop();
        }
Пример #11
0
        public void TestExclusions()
        {
            TrafficViewerFile dataStore = new TrafficViewerFile();

            dataStore.Profile.SetExclusions(new string[1] {
                @".*\.gif"
            });

            TrafficViewerFile mockSite           = new TrafficViewerFile();
            string            nonExcludedRequest = "GET http://site.com/a HTTP/1.1\r\n\r\n";
            string            excludedRequest    = "GET http://site.com/image.gif HTTP/1.1\r\n\r\n";
            string            testResponse       = "HTTP/1.1 200 OK";

            mockSite.AddRequestResponse(nonExcludedRequest, testResponse);
            mockSite.AddRequestResponse(excludedRequest, testResponse);

            MockProxy proxy = new MockProxy(dataStore, mockSite);

            proxy.Start();

            IHttpClient httpClient = GetHttpClient(proxy.Port);

            HttpRequestInfo testRequestInfo = new HttpRequestInfo(excludedRequest);

            HttpResponseInfo respInfo = httpClient.SendRequest(testRequestInfo);

            Assert.AreEqual(200, respInfo.Status);
            //verify that nothing was added to the file
            Assert.AreEqual(0, dataStore.RequestCount);

            //verify that when sending a request that is not excluded the request is being added

            testRequestInfo = new HttpRequestInfo(nonExcludedRequest);
            respInfo        = httpClient.SendRequest(testRequestInfo);

            Assert.AreEqual(200, respInfo.Status);
            //verify that the request was added to the file
            Assert.AreEqual(1, dataStore.RequestCount);

            HttpRequestInfo savedReqInfo = new HttpRequestInfo(dataStore.LoadRequestData(0));

            Assert.AreEqual(testRequestInfo.FullUrl, savedReqInfo.FullUrl);

            proxy.Stop();
        }
        private static MockProxy SetupMockProxy(string testRequest, string testResponse, TrafficViewerFile dataStore)
        {
            MockProxy         mockProxy;
            TrafficViewerFile mockSite = new TrafficViewerFile();

            mockSite.AddRequestResponse(testRequest, testResponse);

            mockProxy = new MockProxy(dataStore, mockSite);
            return(mockProxy);
        }
Пример #13
0
        public void TestEncryptedRequest()
        {
            TrafficViewerFile file      = new TrafficViewerFile();
            string            request1  = "GET /unencrypted HTTP/1.1";
            string            request2  = "GET /encrypted\r\n\r\nsecret=123456789 HTTP/1.1";
            string            response1 = "HTTP 200 OK\r\n\r\nUnencrypted Response";
            string            response2 = "HTTP 200 OK\r\n\r\nEncrypted Response (secret 1234567789)";

            file.AddRequestResponse(request1, response1);
            file.AddRequestResponse(request2, response2);

            var reqInfo = file.GetRequestInfo(1);

            Assert.IsFalse(reqInfo.IsEncrypted, "Default should be unencrypted");
            reqInfo.IsEncrypted = true;
            //resave the request
            file.SaveRequestResponse(1, request2, response2);
            TempFile tempFile = new TempFile();

            file.EnableDefrag = true; //defrag the raw file
            file.Save(tempFile.Path);

            file = new TrafficViewerFile();

            file.Open(tempFile.Path);


            Assert.IsFalse(file.GetRequestInfo(0).IsEncrypted, "First request should not be encrypted");
            Assert.IsTrue(file.GetRequestInfo(1).IsEncrypted, "Second request should be encrypted");


            string testRequest = Constants.DefaultEncoding.GetString(file.LoadRequestData(1));

            Assert.AreEqual(request2, testRequest);

            string testResponse = Constants.DefaultEncoding.GetString(file.LoadResponseData(1));

            Assert.AreEqual(response2, testResponse);
            file.Close(false);
            File.Delete(tempFile.Path);
        }
Пример #14
0
        public void RemoveARequest()
        {
            string originalRequest  = "GET / HTTP/1.1";
            string originalResponse = "HTTP/1.1 200 OK";

            TrafficViewerFile file = new TrafficViewerFile();
            int reqId = file.AddRequestResponse(originalRequest, originalResponse);

            //add one more
            file.AddRequestResponse(originalRequest, originalResponse);

            Assert.AreEqual(2, file.RequestCount);

            file.RemoveRequest(reqId);

            Assert.AreEqual(1, file.RequestCount);

            Assert.IsNull(file.GetRequestInfo(reqId));

            file.Close(false);
        }
Пример #15
0
        public void TestBasicAuth()
        {
            TrafficViewerFile tvf = new TrafficViewerFile();

            tvf.AddRequestResponse("GET / HTTP/1.1", Resources.basicauthresponse);

            TrafficStoreProxy proxy = new TrafficStoreProxy(tvf);

            proxy.Start();

            TrafficViewerHttpClient client = new TrafficViewerHttpClient();

            client.SetProxySettings(proxy.Host, proxy.Port, null);
        }
Пример #16
0
        public void TestRemovingCachedHeaders()
        {
            //setup a mock web server

            TrafficViewerFile serverdataStore = new TrafficViewerFile();

            serverdataStore.Profile.SetExclusions(new string[0] {
            });
            TrafficViewerFile mockSiteData = new TrafficViewerFile();
            string            testRequest  = "GET /a HTTP/1.1\r\nIf-Modified-Since: 10-10-2012\r\nIf-None-Match: 123\r\nProxy-Connection: keep-alive\r\nAccept-Encoding: gzip\r\n\r\n";
            string            testResponse = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n";

            mockSiteData.AddRequestResponse(testRequest, testResponse);
            MockProxy mockServer = new MockProxy(serverdataStore, mockSiteData);

            mockServer.Start();

            //setup a mock proxy

            TrafficViewerFile proxyDataStore = new TrafficViewerFile();

            proxyDataStore.Profile.SetExclusions(new string[1] {
                @".*\.gif"
            });
            ManualExploreProxy meProxy = new ManualExploreProxy("127.0.0.1", 17777, proxyDataStore);

            meProxy.Start();

            IHttpClient httpClient = GetHttpClient(ClientType.TrafficViewerHttpClient, meProxy.Port);             //need to use the traffic viewer client here
            //the webrequestclient does not allow requests to localhost through a proxy on localhost
            HttpRequestInfo testRequestInfo = new HttpRequestInfo(testRequest);

            testRequestInfo.Host = mockServer.Host;
            testRequestInfo.Port = mockServer.Port;


            httpClient.SendRequest(testRequestInfo);


            HttpRequestInfo savedReqInfo = new HttpRequestInfo(serverdataStore.LoadRequestData(0));

            Assert.IsNull(savedReqInfo.Headers["If-Modified-Since"]);
            Assert.IsNull(savedReqInfo.Headers["If-None-Match"]);
            Assert.IsNull(savedReqInfo.Headers["Accept-Encoding"]);
            Assert.IsNull(savedReqInfo.Headers["Proxy-Connection"]);

            meProxy.Stop();
            mockServer.Stop();
        }
Пример #17
0
        public void TestRequestSearchRequestBodyRegexNoBody()
        {
            ITrafficDataAccessor tvf = new TrafficViewerFile();
            string firstRequest      = "GET /a1 HTTP/1.1\r\nHeader1: a1";
            string secondRequest     = "GET /a2 HTTP/1.1\r\nHeader1: a2";
            string firstResponse     = "HTTP 200 OK\r\n<r>1</r>";
            string secondResponse    = "HTTP 200 OK\r\n<r>2</r>";

            tvf.AddRequestResponse(firstRequest, firstResponse);
            tvf.AddRequestResponse(secondRequest, secondResponse);

            Assert.AreEqual(2, tvf.RequestCount);

            RequestSearcher searcher = new RequestSearcher();
            RequestMatches  result   = new RequestMatches();

            SearchCriteriaSet criteriaSet = new SearchCriteriaSet();

            criteriaSet.Add(new SearchCriteria(SearchContext.RequestBody, true, "a=2|a1|<r>2</r>"));

            searcher.Search(tvf, criteriaSet, result);

            Assert.AreEqual(0, result.Count);
        }
Пример #18
0
        public void SaveAndOpen()
        {
            string expectedRequest  = "GET / HTTP/1.1";
            string expectedResponse = "HTTP/1.1 200 OK";

            TrafficViewerFile file = new TrafficViewerFile();
            int reqId = file.AddRequestResponse(expectedRequest, expectedResponse);

            file.GetRequestInfo(reqId).IsHttps = true;

            Assert.AreEqual(1, file.RequestCount);

            TempFile temp = new TempFile(".tvf");

            file.Save(temp.Path);
            //verify that the file can be saved
            Assert.IsTrue(File.Exists(temp.Path), "Cannot save the file");

            file.Close(false);

            //make a new file and verify we can open
            TrafficViewerFile file2 = new TrafficViewerFile();

            file2.Open(temp.Path);
            //verify actual file was open
            Assert.AreEqual(1, file2.RequestCount, "Incorrect request count after opening saved file");
            //verify request data is correct
            int           requestId = -1;
            TVRequestInfo info      = file2.GetNext(ref requestId);

            Assert.IsNotNull(info, "Cannot obtain request info");

            //veryfy transport info
            Assert.IsTrue(info.IsHttps);

            //verify request data
            string loadedRequest = Encoding.UTF8.GetString(file2.LoadRequestData(info.Id));

            Assert.AreEqual(expectedRequest, loadedRequest);

            string loadedResponse = Encoding.UTF8.GetString(file2.LoadResponseData(info.Id));

            Assert.AreEqual(expectedResponse, loadedResponse);


            file2.Close(false);
        }
Пример #19
0
        private void SendManyConnectionsToProxy(bool keepAlive)
        {
            TrafficViewerFile dataStore = new TrafficViewerFile();
            TrafficViewerFile mockSite  = new TrafficViewerFile();
            string            testRequest;

            if (keepAlive)
            {
                testRequest = "GET http://site.com/a HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";
            }
            else
            {
                testRequest = "GET http://site.com/a HTTP/1.1\r\nConnection: close\r\n\r\n";
            }
            string expectedResponseLine = "HTTP/1.1 200 OK";

            mockSite.AddRequestResponse(testRequest, expectedResponseLine);

            MockProxy proxy = new MockProxy(dataStore, mockSite);

            //set a lower connection limit
            proxy.ConnectionLimit = 2;

            proxy.Start();
            int connCount = 500;

            //open 500 connections and expect a good response
            for (int i = 0; i < connCount; i++)
            {
                IHttpClient httpClient = GetHttpClient(proxy.Port);

                HttpRequestInfo testRequestInfo = new HttpRequestInfo(testRequest);

                HttpResponseInfo respInfo = httpClient.SendRequest(testRequestInfo);

                Assert.AreEqual(200, respInfo.Status);
            }

            proxy.Stop();

            Assert.AreEqual(connCount, dataStore.RequestCount);
        }
Пример #20
0
        public void CustomTester_EmptyQueryParamUnitTest()
        {
            TrafficViewerFile mockSite = new TrafficViewerFile();

            mockSite.AddRequestResponse(String.Format("GET /search.jsp?query={0} HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n", MockTestController.PATH_TRAVERSAL),
                                        MockTestController.PATH_TRAVERSAL_RESPONSE);
            MockTestController mockTestController = new MockTestController(mockSite);


            string          testRequest = "GET /search.jsp?query= HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n";
            string          paramName   = "query";
            CustomTestsFile file        = GetCustomTestFile();
            Tester          tester      = new Tester(mockTestController, file);
            CustomTestDef   def         = file.GetCustomTests()["Path Traversal"];
            HttpRequestInfo original    = new HttpRequestInfo(testRequest, true);
            Uri             uri         = new Uri(original.FullUrl);

            tester.ExecuteTests(testRequest, "", uri, paramName, null, RequestLocation.Query, def);
            Assert.IsTrue(mockTestController.IssuesFound.ContainsKey(paramName));
        }
Пример #21
0
        //[TestMethod]
        public void ExportExdUtil()
        {
            string            sourcePath = @"c:\_transfer\jaguarmanualexplorefiltered.htd";
            TrafficViewerFile source     = new TrafficViewerFile();

            source.Open(sourcePath);

            int id            = -1;
            int index         = 0;
            int count         = source.RequestCount;
            int partNo        = 1;
            int numberOfParts = 6;

            int partSize = count / numberOfParts;

            TVRequestInfo     info;
            TrafficViewerFile currentPart = new TrafficViewerFile();

            while ((info = source.GetNext(ref id)) != null)
            {
                if (index < partSize * partNo)
                {
                    byte [] request  = source.LoadRequestData(info.Id);
                    byte [] response = source.LoadResponseData(info.Id);
                    currentPart.AddRequestResponse(request, response);
                }
                else
                {
                    ExportPart(partNo, currentPart);
                    currentPart.Close(false);
                    currentPart = new TrafficViewerFile();
                    partNo++;
                }
                index++;
            }

            if (currentPart.RequestCount > 0)
            {
                ExportPart(partNo, currentPart);
            }
        }
Пример #22
0
        public void TestGETRequestToProxy()
        {
            TrafficViewerFile dataStore            = new TrafficViewerFile();
            TrafficViewerFile mockSite             = new TrafficViewerFile();
            string            testRequest          = "GET http://site.com/a HTTP/1.1\r\n";
            string            expectedResponseLine = "HTTP/1.1 200 OK";

            mockSite.AddRequestResponse(testRequest, expectedResponseLine);

            MockProxy proxy = new MockProxy(dataStore, mockSite);

            proxy.Start();

            IHttpClient httpClient = GetHttpClient(proxy.Port);

            HttpRequestInfo testRequestInfo = new HttpRequestInfo(testRequest);

            HttpResponseInfo respInfo = httpClient.SendRequest(testRequestInfo);

            Assert.AreEqual(200, respInfo.Status);


            proxy.Stop();
        }
Пример #23
0
        private void SendRequest()
        {
            int         count  = 1;
            IHttpClient client = GetHttpClient();

            while (_fuzzOn || count > 0)
            {
                if (_abort)
                {
                    return;
                }

                string mutation = null;

                lock (_lock)
                {
                    if (_abort)
                    {
                        return;
                    }
                    count = _payloads.Count;
                    if (count == 0)
                    {
                        continue;
                    }
                    mutation = _payloads.Dequeue();
                }

                foreach (var request in _requestsToFuzz)
                {
                    string rawReq         = request.Key;
                    string mutatedRequest = rawReq.Replace(FUZZ, mutation);
                    if (Utils.IsMatch(mutatedRequest, Constants.SEQUENCE_VAR_PATTERN))
                    {
                        mutatedRequest = Regex.Replace(mutatedRequest, Constants.SEQUENCE_VAR_PATTERN, DateTime.Now.Ticks.ToString());
                    }
                    if (Utils.IsMatch(mutatedRequest, Constants.GUID_VAR_PATTERN))
                    {
                        mutatedRequest = Regex.Replace(mutatedRequest, Constants.GUID_VAR_PATTERN, Guid.NewGuid().ToString());
                    }

                    HttpRequestInfo reqInfo = new HttpRequestInfo(mutatedRequest);
                    if (reqInfo.ContentData != null)
                    {
                        reqInfo.Headers["Content-Length"] = reqInfo.ContentData.Length.ToString();
                    }
                    reqInfo.IsSecure = request.Value.IsHttps;
                    HttpResponseInfo respInfo = null;

                    if (_abort)
                    {
                        return;
                    }
                    try
                    {
                        respInfo = client.SendRequest(reqInfo);
                        if (_abort)
                        {
                            return;
                        }
                        string response = respInfo.ToString();

                        if (!String.IsNullOrEmpty(_textPattern.Text) && (Utils.IsMatch(response, _textPattern.Text) ^ _reversePattern.Checked))
                        {
                            lock (_lock)
                            {
                                if (_abort)
                                {
                                    return;
                                }
                                _matches++;
                            }
                            _outputFile.AddRequestResponse(mutatedRequest, response, request.Value.IsHttps);
                        }
                    }
                    catch
                    {
                    }
                    lock (_lockCount)
                    {
                        _count++;
                    }

                    if (_abort)
                    {
                        return;
                    }
                }
            }
        }
Пример #24
0
        //[TestMethod]
        public void Test_ReverseProxy()
        {
            string testRequest   = "GET / HTTP/1.1\r\n";
            string site1Response = "HTTP/1.1 200 OK\r\n\r\nThis is site1";
            string site2Response = "HTTP/1.1 200 OK\r\n\r\nThis is site2";
            //create two mock sites each on a different port and a http client that send a request to the first but in fact gets redirected to the other
            TrafficViewerFile site1Source = new TrafficViewerFile();

            site1Source.AddRequestResponse(testRequest, site1Response);
            TrafficViewerFile site2Source = new TrafficViewerFile();

            site2Source.AddRequestResponse(testRequest, site2Response);

            TrafficStoreProxy mockSite1 = new TrafficStoreProxy(
                site1Source, null, "127.0.0.1", 0, 0);

            mockSite1.Start();

            TrafficStoreProxy mockSite2 = new TrafficStoreProxy(
                site2Source, null, "127.0.0.1", 0, 0);

            mockSite2.Start();

            HttpRequestInfo reqInfo = new HttpRequestInfo(testRequest);

            //request will be sent to site 1
            reqInfo.Host = mockSite1.Host;
            reqInfo.Port = mockSite1.Port;

            ReverseProxy revProxy = new ReverseProxy("127.0.0.1", 0, 0, null);

            revProxy.ExtraOptions[ReverseProxy.FORWARDING_HOST_OPT] = mockSite2.Host;
            revProxy.ExtraOptions[ReverseProxy.FORWARDING_PORT_OPT] = mockSite2.Port.ToString();
            revProxy.Start();

            //make an http client
            IHttpClient            client   = new WebRequestClient();
            DefaultNetworkSettings settings = new DefaultNetworkSettings();

            settings.WebProxy = new WebProxy(revProxy.Host, revProxy.Port);

            client.SetNetworkSettings(settings);

            //send the request Http and verify the target site received it

            HttpResponseInfo respInfo = client.SendRequest(reqInfo);
            string           respBody = respInfo.ResponseBody.ToString();


            Assert.IsTrue(respBody.Contains("This is site2"));

            //check over ssl

            reqInfo.IsSecure = true;
            respInfo         = client.SendRequest(reqInfo);
            respBody         = respInfo.ResponseBody.ToString();
            Assert.IsTrue(respBody.Contains("This is site2"));

            mockSite1.Stop();
            mockSite2.Stop();
            revProxy.Stop();
        }
Пример #25
0
        public void Test_HTTP_WebRequestClient_Cookies()
        {
            string[] testRequestList  = new string[5];
            string[] testResponseList = new string[5];
            testRequestList[0]  = "GET http://site.com/a/1 HTTP/1.1\r\n\r\n";
            testResponseList[0] = "HTTP/1.1 302 Redirect\r\nSet-Cookie:a=1; Path=/a\r\nLocation: http://site.com/a\r\n\r\n";
            testRequestList[1]  = "GET http://site.com/a/2 HTTP/1.1\r\n\r\n";
            testResponseList[1] = "HTTP/1.1 302 OK\r\n\r\n";
            testRequestList[2]  = "GET http://site.com/b HTTP/1.1\r\nCookie:b=2\r\n\r\n";
            testResponseList[2] = "HTTP/1.1 302 OK\r\n\r\n";
            testRequestList[3]  = "GET http://site.com/a/3 HTTP/1.1\r\n\r\n";
            testResponseList[3] = "HTTP/1.1 302 Redirect\r\nSet-Cookie:a=2; Path=/a; Expires=Thu, 01-Jan-1970 00:00:01 GMT;\r\nLocation: http://site.com/a\r\n\r\n";
            testRequestList[4]  = "GET http://site.com/a/4 HTTP/1.1\r\n\r\n";
            testResponseList[4] = "HTTP/1.1 200 OK\r\n\r\n";

            WebRequestClient client = new WebRequestClient();

            client.ShouldHandleCookies = true;

            TrafficViewerFile mockSite = new TrafficViewerFile();

            for (int idx = 0; idx < testRequestList.Length; idx++)
            {
                mockSite.AddRequestResponse(testRequestList[idx], testResponseList[idx]);
            }

            TrafficViewerFile dataStore = new TrafficViewerFile();
            MockProxy         mockProxy = new MockProxy(dataStore, mockSite);

            mockProxy.Start();

            client.SetProxySettings(mockProxy.Host, mockProxy.Port, null);
            for (int idx = 0; idx < testRequestList.Length; idx++)
            {
                client.SendRequest(new HttpRequestInfo(testRequestList[idx]));
            }

            //second request should have the extra cookie
            byte[] receivedRequestBytes = dataStore.LoadRequestData(1);//index starts from 0
            Assert.IsNotNull(receivedRequestBytes, "Missing second request");

            HttpRequestInfo receivedRequest = new HttpRequestInfo(receivedRequestBytes, true);

            Assert.IsNotNull(receivedRequest.Cookies);
            Assert.AreEqual(1, receivedRequest.Cookies.Count);
            Assert.IsTrue(receivedRequest.Cookies.ContainsKey("a"));

            //third request should not have the a cookie it's sent to /b but should have the b cookie
            receivedRequestBytes = dataStore.LoadRequestData(2);
            Assert.IsNotNull(receivedRequestBytes, "Missing third request");
            receivedRequest = new HttpRequestInfo(receivedRequestBytes, true);
            Assert.IsNotNull(receivedRequest.Cookies);
            Assert.AreEqual(1, receivedRequest.Cookies.Count, "Request to /b should have 1 cookie");
            Assert.IsTrue(receivedRequest.Cookies.ContainsKey("b"));

            //last request should have no cookies because the a cookie is expired
            receivedRequestBytes = dataStore.LoadRequestData(4);
            Assert.IsNotNull(receivedRequestBytes, "Missing fifth request");
            receivedRequest = new HttpRequestInfo(receivedRequestBytes, true);
            Assert.IsNotNull(receivedRequest.Cookies);
            Assert.AreEqual(0, receivedRequest.Cookies.Count, "Last request should have no cookies");


            mockProxy.Stop();
        }
Пример #26
0
        static void Main(string[] args)
        {
            TrafficViewerFile file = new TrafficViewerFile();

            file.Profile.SetExclusions((IEnumerable <string>) new string[2]
            {
                "\\.(js|axd|zip|Z|tar|t?gz|sit|cab|pdf|ps|doc|ppt|xls|rtf|dot|mp(p|t|d|e|a|3|4|ga)|m4p|mdb|csv|pp(s|a)|xl(w|a)|dbf|slk|prn|dif|avi|mpe?g|mov(ie)?|qt|moov|rmi?|as(f|x)|m1v|wm(v|f|a)|wav|ra|au|aiff|midi?|m3u|gif|jpe?g|bmp|png|tif?f|ico|pcx|css|xml|dll)\\b",
                ConvertorProperties.ExcludedDomainsFromRecordingPattern
            });


            try
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader(args[0]))
                {
                    String line;
                    // Read and display lines from the file until the end of
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null)
                    {
                        Har har             = JsonConvert.DeserializeObject <Har>(line);
                        int requestHeaderId = 0;
                        int counter         = 0;
                        foreach (Entry tempEntry in har.log.entries)
                        {
                            if (HtdConvertorUtil.isRelevantRequest(tempEntry))
                            {
                                counter++;
                                requestHeaderId = file.AddRequestResponse(tempEntry.request.ToString(), tempEntry.response.ToString());

                                if (tempEntry.request.postData != null)
                                {
                                    Console.WriteLine(tempEntry.request.postData.ToString());
                                }

                                file.GetRequestInfo(requestHeaderId).Description = "AppScan Proxy Request to Server";

                                if (tempEntry.request.isHttps)
                                {
                                    file.GetRequestInfo(requestHeaderId).IsHttps = true;
                                }
                            }
                        }
                        Console.WriteLine(counter);
                    }
                }


                file.Save(args[1]);
                file.Close(false);
                Console.WriteLine("Recording has been done");
                //Console.ReadLine();
            }
            catch (Exception e)
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
        }
Пример #27
0
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("Usage: Traffic2Exd <traffic file path> <EXD file path>");
                Console.WriteLine("Supported import formats: .har, .txt, .htd");
                Console.WriteLine("If the EXD file already exists the tool will append to it.");

                Console.WriteLine("Exit codes: 1 - No args, 2 - Incorrect file path, 3 - Parsing error, 4 - Export error, 5 - Unsupported Exception.");
                Environment.ExitCode = 1;
            }
            else
            {
                string trafficFilePath = args[0];
                string exdFilePath     = args[1];
                if (!File.Exists(trafficFilePath))
                {
                    Console.WriteLine("Could not find har file: '{0}'", trafficFilePath);
                    Environment.ExitCode = 2;
                }
                else
                {
                    TrafficViewerFile tvf = new TrafficViewerFile();
                    try
                    {
                        if (File.Exists(exdFilePath))
                        {
                            Console.WriteLine("EXD file {0} already exists. Appending to it.", exdFilePath);
                            ConfigurationParser exdParser = new ConfigurationParser();
                            exdParser.Parse(exdFilePath, tvf, ParsingOptions.GetDefaultProfile());
                        }


                        Console.WriteLine("Importing from '{0}'...", trafficFilePath);
                        ITrafficParser parser = null;


                        if (trafficFilePath.ToLower().EndsWith(".har"))
                        {
                            parser = new HarParser();
                        }
                        else if (trafficFilePath.ToLower().EndsWith(".txt"))
                        {
                            parser = new DefaultTrafficParser();
                        }
                        else if (trafficFilePath.ToLower().EndsWith(".htd"))
                        {
                            TrafficViewerFile tvf2 = new TrafficViewerFile();
                            tvf2.Open(trafficFilePath);
                            int           id   = -1;
                            TVRequestInfo info = null;

                            while ((info = tvf2.GetNext(ref id)) != null)
                            {
                                tvf.AddRequestResponse(tvf2.LoadRequestData(info.Id), tvf2.LoadResponseData(info.Id));
                            }
                        }
                        else
                        {
                            Console.WriteLine("File extension is unsupported. Supported extensions/formats: .har, .txt, .htd");
                            Environment.ExitCode = 5;
                        }

                        if (parser != null)
                        {
                            parser.Parse(trafficFilePath, tvf, ParsingOptions.GetRawProfile());
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Parsing exception: '{0}'", ex.Message);
                        Environment.ExitCode = 3;
                    }
                    //now export

                    try
                    {
                        Console.WriteLine("Exporting to '{0}'...", exdFilePath);
                        var exporter = new ManualExploreExporter();
                        exporter.Export(tvf, new FileStream(exdFilePath, FileMode.Create, FileAccess.ReadWrite));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Export exception: '{0}'", ex.Message);
                        Environment.ExitCode = 4;
                    }
                    tvf.Close(false);
                    Console.WriteLine("Done.");
                }
            }
        }