Chrome执行JS脚本进行接口请求的方法--完成很多需要复杂授权接口的hack

背景:

最近在工作中经常遇到需要在网页上进行重复操作的场景,比如注册大量账户,新建多个子账户之类的操作,都是在一个网站中进行类似重复的操作,但是又很难通过接口去实现,因为网站的授权认证特别麻烦,有各种校验,有些还是第三方登录,比如Google Auth,要通过接口请求去模拟登录异常困难。

问题:

 1、需要在网页上进行重复操作,但实际是调用一个重复的接口;

2、网站的权限校验比较复杂;

解决思路:

通过在Chrome的console中自定义JavaScript的方法,然后通过循环执行调用这个接口传入不同参数来实现我们的目的;

实现步骤:

1、在console中定义函数,请求参数是form表单的情况:

PS:我们这里解决的是通过请求注册接口注册多个账户

const test_register = async () => {
  const uri = 'https://xxx.xxxx.xxx.xxx/system/register_ajax/'
  for (let i = 1000; i < 2000; i ++) {
    const searchParams = new URLSearchParams();
    searchParams.set('username', `user${i}`)
    searchParams.set('password', 'xxxxx')
    searchParams.set('email', `user${i}@gmail.com`)
    searchParams.set('country_code', '000')
    searchParams.set('phone', `000000${i}`)
    await fetch(uri, {
      method: 'POST',
      credentials: 'include',
      body: searchParams,
      headers: {
        "x-csrftoken": "OIYahxxxxxJfafHN0xxxxxxelc098L"
      }
    })
  }
}

2、执行函数:

test_register()

注意事项:

如果请求的接口是JSON格式的话参数应该这样定义:

const add_members = async () => {
      let uri = "https://xxxx.xxx.xxxxxx.com/api/v1/member/?CSRF_TOKEN=xxxxxxxx-4bb2-47a7-aa01-xxxxxxxx"
      for (let i = 6; i < 11; i ++) {
        let post_body = {"parm1":"value", "account_name":`member${i}`, "password":"xxxxxxxxxxxx", "confirmPassword":"", "nick_name":`member${i}`, "email":`member${i}@gmail.com`,"phonePrefix":"+86","phone":`+861345664700${i}`}
        await fetch(uri, {
          method: 'POST',
          credentials: 'same-origin',  //注意这个 [MUST]submit cookies in same origin, if not add this field request will not add cookies
          body: JSON.stringify(post_body)
        })
      }
    }

猜你喜欢

转载自www.cnblogs.com/l33klin/p/9164188.html