我正在使用SyslogNet库从我的应用程序向LEEF格式的第三方供应商SIEM系统发送SIEM Syslog消息(在此它的QRadar中)。 但是,我的消息没有被发送,并且该程序无处产生任何异常。 根据Rfc5424的LEEF消息,我可以看到并确认字符串与正确的模板/格式相关(参见代码下面的示例)。
消息未正确发送的唯一指示是WireShark跟踪显示消息仅由几个字节组成,更重要的是被视为格式错误的数据包:
代码如下:
using System.IO;
using System.Text;
using System.Reflection;
using SyslogNet.Client;
using SyslogNet.Client.Serialization;
using SyslogNet.Client.Transport;
using Severity = SyslogNet.Client.Severity;
namespace Providers.Syslog
{
public class SyslogLogger
{
private readonly SyslogSettings _syslogSettings;
private readonly SyslogUdpSender _syslogUdpSender;
public SyslogLogger(SyslogSettings syslogSettings)
{
_syslogSettings = syslogSettings;
_syslogUdpSender = new SyslogUdpSender(syslogSettings.SyslogServerName, syslogSettings.SyslogServerPort);
}
public void Log(string message)
{
var syslogMessage = new SyslogMessage(
null,
(Facility)_syslogSettings.Facility,
Severity.Informational,
null,
_syslogSettings.Identity,
message);
_syslogUdpSender.Send(syslogMessage, new SyslogMessageSerializer());
}
private class SyslogMessageSerializer : SyslogMessageSerializerBase, ISyslogMessageSerializer
{
private const int AppNameMaxLength = 32;
public void Serialize(SyslogMessage message, Stream stream)
{
var priorityValue = CalculatePriorityValue(message.Facility, message.Severity);
string timestamp = null;
if (message.DateTimeOffset.HasValue)
{
var dt = message.DateTimeOffset.Value;
var day = dt.Day < 10 ? " " + dt.Day : dt.Day.ToString();
timestamp = string.Concat(dt.ToString("MMM "), day, dt.ToString(" HH:mm:ss"));
}
var headerBuilder = new StringBuilder();
headerBuilder.Append("<").Append(priorityValue).Append(">");
if (!string.IsNullOrEmpty(timestamp))
{
headerBuilder.Append(timestamp).Append(" ");
}
if (!string.IsNullOrEmpty(message.HostName))
{
headerBuilder.Append(message.HostName).Append(" ");
}
if (!message.Message.IsSiemOrientadTemplate())
{
var appName = message.AppName;
headerBuilder.Append(string.IsNullOrWhiteSpace(appName)
? appName
: (appName.Length > AppNameMaxLength ? appName.Substring(0, AppNameMaxLength) : appName) + ": ");
headerBuilder.Append(message.Message ?? "No Content");
}
var encoding = new UTF8Encoding(false);
var bytes = encoding.GetBytes(headerBuilder.ToString());
stream.Write(bytes, 0, bytes.Length);
}
}
}
}
e.g。一条消息(我删除了实际数据并放置了占位符):
&#34; LEEF:2.0 | CompanyNameString | ProduceNameString | VERSIONSTRING | INT | ^ | ^ cat =测试 - QRADAR ^ sev = 6 ^ DescriptionString = ^时间戳= 2017年4月3日14:48:02 ^ userNameString = domain \ DisplayName ^ accountName = samAccountName ^ proto = Syslog &#34;
任何帮助都会受到赞赏,因为它已经有一段时间了,我无法找到解决方案。提前谢谢!
答案 0 :(得分:0)
终于找到了问题。 我忽略了这样一个事实,即内容是非SIEM系统的“if”子句。 将此行移出该子句可解决所有syslog提供程序类型的问题。
headerBuilder.Append(message.Message ??“No Content”);