esp8266 byethost Error get request

こ雲淡風輕ζ 提交于 2019-12-12 04:29:06

问题


Good morning, I want to send data by get through esp8266, I have a qualifying account in byethost and I also have a hosting account paid with another hosting provider, but with byethost I get the following error:

AT+CIPSTART="TCP","ahorrodeenergy.byethost17.com",80
AT+CIPSEND=67
GET /inserta.php HTTP/1.1 
Host:ahorrodeenergy.byethost17.com/inserta.php"



+IPD,1080:HTTP/1.1 200 OK
Server: nginx
Date: Fri, 10 Mar 2017 01:30:09 GMT
Content-Type: text/html
Content-Length: 851
Connection: keep-alive
Vary: Accept-Encoding
Expires: THu, 01 Jan 1970 00:00:01 GMT
Cache-Control: no-cache

And returns: This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support


回答1:


Spoof the User-Agent string. It probably tries to identify your browser by that, and then tries to figure out if you have JavaScript enabled. It could then try to use more active tests, like inserting a piece of JavaScript and expect a page to be called with the result of the computation of that javascript, in a challenge-and-response manner. But I think a User-Agent spoof should just work fine. Do the following:

AT+CIPSTART="TCP","ahorrodeenergy.byethost17.com",80
AT+CIPSEND=154
GET /inserta.php HTTP/1.1 
Host: ahorrodeenergy.byethost17.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0


+IPD,1080: ..

(number in CIPSEND assumes \r\nbeing used as newline)




回答2:


Based on the this awnser, the problem is testcookie-nginx-module which is used by byethost, and here the solution:

/*
  Author: Moien007
  Source: https://gist.github.com/moien007/06656aa4032c45b629a507dd4dcb6fd6
  Description:
  Bypass testcookie-nginx-module bot protection
  Web host providers like Byethost uses that module so...
*/

using System;
using System.Linq;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Net;
using System.Net.Http;

namespace Gist
{
    class CustomWebClient
    {
        const string TestCookieSign = "aes.js";

        public static byte[] Get(string url)
        {
            var address = new Uri(url);
            var cookieContainer = new CookieContainer();
            using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
            using (var client = new HttpClient(handler))
            {
                var content = client.GetAsync(address).WaitResult().Content;
                var script = content.ReadAsStringAsync().WaitResult();

                if (!script.Contains(TestCookieSign))
                {
                    return content.ReadAsByteArrayAsync().WaitResult();
                }

                var test = Decrypt(script);
                cookieContainer.Add(new Cookie("__test", test) { Domain = address.Host });

                content = client.GetAsync(address).WaitResult().Content;
                if (content.ReadAsStringAsync().WaitResult().Contains(TestCookieSign))
                {
                    throw new Exception();
                }

                return content.ReadAsByteArrayAsync().WaitResult();
            }
        }

        public static byte[] Post(string url, byte[] data)
        {
            var address = new Uri(url);
            var cookieContainer = new CookieContainer();
            using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
            using (var client = new HttpClient(handler))
            using (var post = new ByteArrayContent(data))
            {
                var content = client.PostAsync(address, post).WaitResult().Content;
                var script = content.ReadAsStringAsync().WaitResult();

                if (!script.Contains(TestCookieSign))
                {
                    return content.ReadAsByteArrayAsync().WaitResult();
                }

                var test = Decrypt(script);
                cookieContainer.Add(new Cookie("__test", test) { Domain = address.Host });

                content = client.PostAsync(address, post).WaitResult().Content;
                if (content.ReadAsStringAsync().WaitResult().Contains(TestCookieSign))
                {
                    throw new Exception();
                }

                return content.ReadAsByteArrayAsync().WaitResult();
            }
        }

        static string Decrypt(string script)
        {
            var split = script.Split(new[] { "toNumbers(\"", "\")" }, StringSplitOptions.RemoveEmptyEntries)
                        .Where(s => s.Length == 32)
                        .ToArray();

            if (split.Length != 3)
                throw new Exception();

            var key = StringToByteArray(split[0]);
            var iv = StringToByteArray(split[1]);
            var bytesIn = StringToByteArray(split[2]);

            var aes = Aes.Create();
            aes.Padding = PaddingMode.None;
            aes.Mode = CipherMode.CBC;
            aes.BlockSize = 128;
            aes.KeySize = 128;
            aes.Key = key;
            aes.IV = iv;

            var decrypter = aes.CreateDecryptor();
            var decrypted = decrypter.TransformFinalBlock(bytesIn, 0, bytesIn.Length);

            decrypter.Dispose();
            aes.Dispose();

            return BitConverter.ToString(decrypted).Replace("-", "").ToLower();
        }

        static byte[] StringToByteArray(string hex) // Taken from https://stackoverflow.com/a/321404/9248173
        {
            return Enumerable.Range(0, hex.Length)
                             .Where(x => x % 2 == 0)
                             .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                             .ToArray();
        }
    }

    static class ExtensionMethods
    {
        public static T WaitResult<T>(this Task<T> task)
        {
            task.Wait();
            return task.Result;
        }
    }
}


来源:https://stackoverflow.com/questions/42722584/esp8266-byethost-error-get-request

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!