美文网首页
[抓包工具]Whistle使用

[抓包工具]Whistle使用

作者: 我叫不迷糊 | 来源:发表于2022-10-17 00:16 被阅读0次

    官方帮助文档
    GitHub项目
    安装、启动、代理配置、证书安装等均可以参考官方帮助文档解决。
    使用whistle主要是因为它支持命令行操作,可以满足自动化测试的需要。

    常用基础命令

    启动:w2 start
    停用:w2 stop
    查看状态:w2 status
    帮助:w2 -h

    默认Whistle端口号是:8899

    启动后会显示打开 Whistle 管理界面的说明,如下图:


    启动Whistle截图

    我一般使用 http://127.0.0.1:8899 打开

    管理界面使用可以参考官方文档——快速上手

    启动多个Whistle

    有时同时有多个不同的使用场景,希望互不干扰,所以需要启动多个Whistle。

    通过指定与默认不同的规则目录和端口。

    -S: 指定规则目录
    -C:默认目录的配置拷贝到新的目录,也可以指定目录
    -p:端口号

    例如,指定规则目录和端口号是7788:
    启动:w2 start -S 7788 -C -p 7788
    停用:w2 stop -S 7788
    查看状态:w2 status -S 7788

    常用规则配置

    使用管理界面配置规则
    选择Rules,直接在文本框中填写规则即可,如下图:

    Rules界面

    语法:pattern operatorURI
    pattern: 匹配请求url的表达式,可以为:域名,路径,正则及通配符等等
    operatorURI :为对应的操作,由操作协议+操作值组成(operatorURI = opProtocol://opValue)

    多个规则换行即可。

    注释规则,在行首加上#,或者快捷键command+/。(mac)

    1. 使用本地文件替换response的json:

    operatorURI = 本地文件路径
    例如:

    # 本地文件路径/xxx/xxx/xxx.json,替换http://xxx.xxx/xxx.json的response
    http://xxx.xxx/xxx.json  /xxx/xxx/xxx.json
    

    2. 使用Values替换response的json

    operatorURI = file://{key}
    例如:

    # 使用key ```demo_config.json```替换http://xxx.xxx/xxx.json的response
    http://xxx.xxx/xxx.json file://{demo_config.json}
    

    如何添加Values?
    打开Values,点击顶部Create按钮创建Key,再在右侧文本区域编辑json内容即可。

    Values 界面

    回到Rules配置规则。

    3. 替换http status

    operatorURI = replaceStatus://code

    # http://xxx.xxx/xxx.json响应码替换为404
    http://xxx.xxx/xxx.json statusCode://404
    

    4. 设置延迟

    请求延迟:operatorURI = reqDelay://timeMS(单位:ms)

    # 设置请求延迟1s
    http://xxx.xxx/xxx.json  reqDelay://1000
    

    响应延迟:operatorURI = resDelay://timeMS(单位:ms)

    # 设置响应延迟1s
    http://xxx.xxx/xxx.json  resDelay://1000
    

    5.设置弱网

    请求速度:operatorURI = reqSpeed://kbs(单位:kb/s,千比特/每秒)

    # 设置请求速度3kb/s
    http://xxx.xxx/xxx.json  reqSpeed://3
    

    响应速度:operatorURI = resSpeed://kbs (单位:kb/s,千比特/每秒)

    # 设置响应速度3kb/s
    http://xxx.xxx/xxx.json  resSpeed://3
    

    6. 重定向

    operatorURI = redirect://jumpUrl

    # 重定向到baidu
    http://xxx.xxx/xxx.json  redirect://https://www.baidu.com
    

    7. abort请求

    operatorURI = enable://abort

    # abort http://xxx.xxx/xxx.json
    http://xxx.xxx/xxx.json enable://abort
    

    更多配置规则见官方文档——协议列表

    命令行配置规则

    添加规则命令:
    w2 add [filepath]
    或者
    w2 use [filepath]

    filepath: 配置规则的js文件路径。filepath可选,默认为当前目录的 .whistle.js,即web管理界面中Rules中的Default。

    js文件格式示例:

    exports.name = `Automation-Common`;
    exports.rules = `
    http://xxx.xxx/xxx.json  /xxx/xxx/xxx.json
    http://xxx.xxx/xxx.json statusCode://404
    `;
    

    exports.name = Automation-Common:是规则名称为Automation-Common
    exports.rules = ……:对应Web管理界面文本框中设置的规则内容

    设置后打开Web管理界面可以看到规则添加且选中,如下图:

    执行命令后

    如果希望强制覆盖同名规则,使用w2 add [filepath] --force
    如果启动多个Whistle,需要带上要添加规则的规则目录,w2 add [filepath] --force -S [规则目录]

    命令示例,例如添加规则/xxx/xxx.js:

    w2 add /xxx/xxx.js
    w2 add /xxx/xxx.js --force
    w2 add /xxx/xxx.js --force -S 7788
    

    相关文章

      网友评论

          本文标题:[抓包工具]Whistle使用

          本文链接:https://www.haomeiwen.com/subject/ygzlzrtx.html