linux - 通过本地用户名进行 Tshark 日志记录

标签 linux ubuntu iptables packet-capture tshark

我正在尝试将 tshark 转储中的网络流量归因于与相应流量相关联的登录用户。

我在企业网络上运行 Ubuntu 服务器,用户通过 RDP 或 SSH 使用 x-forwarding 登录。我的应用程序是用 bash 编写的,并使用 tshark 使用以下命令记录所有网络流量:

 tshark -b filesize:1024 -w traffic.pcap

然后我有一个在 chron 作业上运行的 while 循环来解析 pcap 文件,提取我想要的字段,并将它们保存到 json:
for f in *.pcap
do
    tshark -r $f -T json > $f.json
    cat $f.json | jq .[]._source.layers | jq -r '.frame_time_epoch[],.frame_number[],.tcp_stream[],.ip_src[],.tcp_srcport[],.ip_dst[],.tcp_dstport[],.frame_len[],.frame_protocols[],.http_host[],.http_request_uri[],.http_request_method[],.http_user_agent[],.http_referer[],.dns_qry_name[],.dns_resp_name[],.ssl_handshake_extensions_server_name[],.x509sat_uTF8String[],.x509ce_dNSName,"END"' > injest.json
done

然后将如下所示的输出 json 发送到我设置的 Elasticsearch 集群:
{
"layers": {
    "frame.time_epoch": ["1579755811.545369005"],
    "frame.number": ["3508"],
    "tcp.stream": ["0"],
    "ip.src": ["10.0.10.10"],
    "tcp.srcport": ["22"],
    "ip.dst": ["1.1.1.1"],
    "tcp.dstport": ["54116"],
    "frame.len": ["102"],
    "frame.protocols": ["eth:ethertype:ip:tcp:ssh"]
    }
}

我想知道无论如何我可以将Linux用户名添加到数据包捕获中,或者作为最终json中的附加字段,然后再将其发送到ELK。

换句话说,如果“user_a”和“user_b”都登录到服务器,我希望他们各自的互联网流量在那里记录用户名。 tshark 有可能吗?我读过 iptables 可以“标记”流量,但我不确定它是如何工作的,以及它是否能完成我想要做的事情。

最佳答案

通过发送的数据包记录用户

I'd like to know if there is anyway I can have the Linux username added to the packet capture



这可以通过添加如下详述的捕获评论来实现

or as an additional field in the final json before it is sent to ELK.



虽然这不是因为 tshark 不知道如何向您显示 pcapng header 选项,即使它可以读取它们(Wireshark 可以使用 reload as file format/capture 来做到这一点)。

In other words if 'user_a' and 'user_b' are both logged into the server I'd like their respective internet traffic to log their usernames. Is this at all possible with tshark?



不,这对于 tshark 是不可能的。看起来像 it's possible使用 auditctl 获取 PID,然后找到创建 PID 的用户。通过如此收集的用户名,您可以集成到下面的流程中。

添加捕获评论

要获取事件用户名,您可以使用以下内容:

names="$(who | cut -d ' ' -f 1 | sort | uniq)"

要保存为数据包捕获的一部分,请使用 tshark 的 --capture-comment 添加其他信息。请注意,此选项仅在您另存为 pcapng 时可用。默认保存类型是 pcapng。如果不确定,可以使用 captype $file ,其中 captype 与 Wireshark 一起提供。
tshark -b filesize:1024 -w traffic.pcap --capture-comment "$names"

提取捕获评论

tshark 没有办法提取捕获级注释,所以我使用 python,了解 pcapng's binary format提取评论:

import struct

def read_header(infile, header_code):
  with open(infile, "rb") as f:
      filebytes = f.read()
  header_len = struct.unpack("iill", filebytes[:24])[1]
  header = filebytes[24:24+header_len]
  seek = 24
  while seek < header_len:
      opt_code, opt_length = struct.unpack("hh", filebytes[seek:seek+4])
      if opt_code == header_code:
          value = filebytes[seek+4:seek+4+opt_length].decode('utf-8')
          return value
      seek += 4 + opt_length
  return ""

comment_option_code = 1
comment = read_header("traffic.pcap", comment_option_code)
print(comment)

$names 中提供了 user_a 和 user_b ,这将打印

user_a user_b

添加到 JSON

然后,您可以将数据附加到现有的 json 中,如下所示:

echo '{"data": "values"}' | jq --arg names "${names}" '. + {users:$names}'
{
  "data": "values",
  "users": "user_a user_b"
}

关于linux - 通过本地用户名进行 Tshark 日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60407737/

相关文章:

linux - 尝试使用cgroup显示CPU分配更改

linux - 如何将 -I 和 -n 与 xargs 结合起来?

使用 poll() 时正确处理 Ctrl-C

apache - 尽管有多个虚拟主机,但所有流量都定向到单个目录

ubuntu - TensorFlow Android 示例 : Cannot find WORKSPACE file

mysql - 通过备用端口建立 mysql 连接

mysql - 如何将mysql数据库流量镜像到另一台服务器

java - Logback 日志由应用程序保持打开状态,不会从磁盘中删除

java - MySQL 区分大小写

centos7 - 函数 ‘NF_HOOK’ 的参数太少