服务器抓包知识

编写背景:

我在某银行负责一个项目的主要开发,在调试与第三方的接口时遇到了一个问题:对方的服务器清楚地给了我返回值,但我没有收到,日志中也没有打印对应的值。一开始我以为是别人的问题,结果对方输了一句我想要的,然后我自己也搞砸了。

[En]

I am responsible for the main development of a project in a bank, and encountered a problem when debugging the interface with a third party: the server of the other party clearly gave me the returned value, but I did not receive it, and the corresponding value was not printed in the log. At first I thought it was the other person’s problem, but the other person lost the sentence “I tried to have it”, and then I was messed up by myself.

然后我想到了如何抓取包进行验证,看看是哪一方的问题造成的。因为日志可能是欺骗性的,但不可能抢到包。

[En]

Then I thought of the way to grab the package for verification to see which side of the problem caused it. Because the log may be deceptive, but it is not possible to grab the bag.

抓包的概念:

抓包顾名思义就是抓取数据包,基本上与第三方通讯就会产生数据包,我们将这些包做了一个抓取的动作,将它保存在我们指定的文件中。我们与第三方的交互走的是TCP ,且无加密,所以我们只需要简简单单的抓包解析数据即可。

抓包前的准备:

首先要想在服务器中使用抓包指令就需要安装对应的抓包工具”tcpdump “,大部分服务器上是没有的,需要自行安装,只有安装了之后才能使用tcpdump 的指令,安装的教程直接百度搜一下”tcpdump 安装”即可。

准备测试数据,因为抓包的速度很快,要抓取的东西很多,如果您的服务器有其他应用要交互,可能是因为您的数据准备不足,所以在执行抓包指令后,一天中的大部分时间都无法完成交易。当时,抓拍的包裹非常笨重,查看起来很不方便。我的建议是估计事务良好的节点,然后在该节点前面执行数据包捕获指令。

[En]

Prepare the test data, because the speed of catching the package is very fast, and there are a lot of things to catch, if your server has other applications to interact with, it may be because your data is not prepared enough that you will not be able to complete the transaction for most of the day after executing the packet capture instruction. at that time, the package captured is very bulky and inconvenient to view. My suggestion is to estimate the node where the transaction is good, and then execute the packet capture instruction in front of the node.

下载一个叫做Wireshark 工具,这个工具专门用来解析抓包后的文件,工具的安装及使用教程就自己百度了。

要得到服务器的ROOT 用户密码,因为tcpdump 指令只能是root 用户下操作。

开始抓包:

1、登录服务器,使用root 用户;

2、在发起交易的前一步就要执行抓包指令;

3、在任意目录下执行 tcpdump -i any -XO -vvv -s0 -w /root/anyport.cap

服务器抓包知识

root/anyport.cap 这个是文件保存的路径,可以自己定义。

4、执行完交易后,马上在服务器按CTRL+C 停止抓包,否则会无限抓包,这个十分重要一定要牢记;

解析数据包:

1、在服务器路径中取出刚抓包的文件;

2、将文件丢进Wireshark 工具中;

3、在工具上方的输入框中输入ip.addr=xxx.xxx.xxx.xx 可以直接定位对方ip 地址的数据;

4、找到对方返回的报文内容;

5、很惊奇的发现居然是乱码,这个其实是和内容编码有关系,如果编码是UTF-8 之类的常见编码则可以正常解析出来,但是项目采用的是cp937 这类编码,则就需要进行转换了。如果你的解析没有乱码,下面的内容对你意义不大了。

6、复制返回报文的hex 值,选择hex stream ;

7、在java 中写一个转换的方法,对数据进行转换。具体代码我附上,解释在代码中注解;

package test;

import java.io.UnsupportedEncodingException;

public class test {

    public static void main(String[] args) throws UnsupportedEncodingException {
        /*将抓包到的hex值粘贴进来*/
        byte[] teststr=hexToByteArray("40f0f5f7f44040f0f0f0f4f5f040404040404040404040404040404040f0f0f0f0404040404040f0f0f3f0f0f5f0f0f2f3f3f5f0f0f9f9f9f9f0f7f7f0f7f9f0f0f0f0f0f0f0f0f0404040404040404040f0f0f640f0f0f0f0f0f0f0f0f0f1f0f0f0f0f0f0f0f0f0f0f0f0f0f0f04040404040404040404040404040404040f0f3404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040d4c1c3c1e440c2d6c340e3c5e2e340c1c3c3d6e4d5e34040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040f1f3f8");
        System.err.println(new String(teststr,"cp937"));//指定对应的编码转String
    }
    /**
     * Hex字符串转byte
     * @param inHex 待转换的Hex字符串
     * @return  转换后的byte
     */
    public static byte hexToByte(String inHex){
       return (byte)Integer.parseInt(inHex,16);
    }
    public static byte[] hexToByteArray(String inHex){
        int hexlen = inHex.length();
        byte[] result;
        if (hexlen % 2 == 1){
            //奇数
            hexlen++;
            result = new byte[(hexlen/2)];
            inHex="0"+inHex;
        }else {
            //偶数
            result = new byte[(hexlen/2)];
        }
        int j=0;
        for (int i = 0; i < hexlen; i+=2){
            result[j]=hexToByte(inHex.substring(i,i+2));
            j++;
        }
        return result;
    }

}

Original: https://www.cnblogs.com/tiezhuxiong/p/16474008.html
Author: 铁柱兄
Title: 服务器抓包知识

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/523112/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球