regex 在C#中,字符串不会一起写入控制台

xggvc2p6  于 2023-10-22  发布在  C#
关注(0)|答案(1)|浏览(92)

我超级困惑。我编写了一个脚本,从事件日志中的失败登录中提取时间、IP、帐户名和帐户域。单独地,变量可以很好地写入控制台。但是,如果我试图把变量放在字符串定义中的相邻位置,它会画出空白。
代码:

using System;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;

class Program
{

    static void Main()
    {
        string GetIP(string input)
        {
            Regex regex = new Regex(@"Source Network Address:.*");
            Match match = regex.Match(input);

            if (match.Success)
            {
                string ipAddress = Regex.Replace(match.Value, @"Source Network Address:\s*", "");
                return ipAddress;
            }
            else
            {
                return "No match found.";
            }
        }
        string GetAcc(string input)
        {
            Regex regexAccountName = new Regex(@"Account Name:\s*(\S+)");

            // Find all matches in the input string.
            MatchCollection matches = regexAccountName.Matches(input);

            if (matches.Count >= 2)
            {
                // Extract the value from the second match (index 1).
                string accountName = matches[1].Groups[1].Value;
                return accountName;
            }
            else
            {
                return "No match found.";
            }
        }
        string GetDomain(string input)
        {
            Regex regexAccountName = new Regex(@"Account Domain:\s*(\S+)");

            // Find all matches in the input string.
            MatchCollection matches = regexAccountName.Matches(input);

            if (matches.Count >= 2)
            {
                // Extract the value from the second match (index 1).
                string accountName = matches[1].Groups[1].Value;
                return accountName;
            }
            else
            {
                return "No match found.";
            }
        }
        Console.WriteLine("Date/Time,IP Address,Account Name,Account Domain");

        int targetEventId = 4625;
        string logName = "Security"; // You can change the log name as needed

        EventLog eventLog = new EventLog(logName);

        foreach (EventLogEntry entry in eventLog.Entries)
        {
            if (entry.InstanceId == targetEventId)
            {
                // Console.WriteLine($"Event ID: {entry.InstanceId}");
                // Console.WriteLine($"Time: {entry.TimeGenerated}");
                DateTime Time = Convert.ToDateTime(entry.TimeGenerated);
                Console.WriteLine(Time);
                // Console.WriteLine($"Message: {entry.Message}");
                string message = entry.Message;
                string ip = GetIP(message);
                Console.WriteLine(ip);
                string acc = GetAcc(message);
                Console.WriteLine(acc);
                string domain = GetDomain(message);
                Console.WriteLine(domain);
                string csvOutput = Time+","+ip+","+acc+","+domain;
                Console.WriteLine(csvOutput);
            }
        }

        eventLog.Close();
        Console.WriteLine("End of logs. Press any key to exit.");
        Console.ReadKey();
    }
   
}

输入:

Event ID: 4625
Time: 9/26/2023 10:39:55 AM
Message: An account failed to log on.

Subject:
        Security ID:            S-1-0-0
        Account Name:           -
        Account Domain:         -
        Logon ID:               0x0

Logon Type:                     3

Account For Which Logon Failed:
        Security ID:            S-1-0-0
        Account Name:           [email protected]
        Account Domain:         MicrosoftAccount

Failure Information:
        Failure Reason:         %%2313
        Status:                 0xc000006d
        Sub Status:             0xc000006a

Process Information:
        Caller Process ID:      0x0
        Caller Process Name:    -

Network Information:
        Workstation Name:       DESKTOP-J7O5MEA
        Source Network Address: 192.168.1.198
        Source Port:            0

Detailed Authentication Information:
        Logon Process:          NtLmSsp
        Authentication Package: NTLM
        Transited Services:     -
        Package Name (NTLM only):       -
        Key Length:             0

This event is generated when a logon request fails. It is generated on the computer where access was attempted.

The Subject fields indicate the account on the local system which requested the logon. This is most commonly a service such as the Server service, or a local process such as Winlogon.exe or Services.exe.

The Logon Type field indicates the kind of logon that was requested. The most common types are 2 (interactive) and 3 (network).

The Process Information fields indicate which account and process on the system requested the logon.

The Network Information fields indicate where a remote logon request originated. Workstation name is not always available and may be left blank in some cases.

The authentication information fields provide detailed information about this specific logon request.
        - Transited services indicate which intermediate services have participated in this logon request.
        - Package name indicates which sub-protocol was used among the NTLM protocols.
        - Key length indicates the length of the generated session key. This will be 0 if no session key was requested.

输出量:

Date/Time,IP Address,Account Name,Account Domain
9/26/2023 10:39:53 AM
192.168.1.198
[email protected]
MicrosoftAccount
,[email protected],MicrosoftAccount8               // See expected output below
9/26/2023 10:39:55 AM
192.168.1.198
[email protected]
MicrosoftAccount
,[email protected],MicrosoftAccount8               // See expected output below

预期输出:9/26/2023 10:39:53 AM,192.168.1.198, [[email protected]](https://stackoverflow.com/cdn-cgi/l/email-protection) ,MicrosoftAccount
我尝试获取每个变量,并在定义新字符串时对它们运行.ToString()。没用我也试过string csvOutput = $"{Time},{ip},{acc},{domain}";

0s7z1bwu

0s7z1bwu1#

感谢@ devlinincarnate引导我朝着正确的方向前进!我是这样做的:

  • Thread.Sleep(5000);将csvOutput写入控制台后,我有时间点击break all
  • 转到即时窗口,使用? ip获取ip的值,并返回"192.168.1.198\r"
  • 因此,我将GetIP函数的一部分从
string ipAddress = Regex.Replace(match.Value, @"Source Network Address:\s*", "");
                return ipAddress;

收件人:

string ipAddress = Regex.Replace(match.Value, @"Source Network Address:\s*", "");
                string newIP = ipAddress.Replace("\r", "");
                return newIP;

一切都很好,StackOverflow再次拯救!:)

相关问题