로그 추출 쉘 (AWK,정규표현식)

tjddbs8424의 이미지

05/13/2018-23:12:27.585699 [**] [1:2240001:2] SURICATA DNS Unsolicited response [**] [Classification: Generic Protocol Command Decode] [Priority: 3] {UDP} 168.126.63.1:53 -> 192.168.233.148:40729
05/13/2018-23:47:39.748349 [**] [1:10001:1] kim rule [**] [Classification: (null)] [Priority: 3] {TCP} 192.168.233.148:35662 -> 112.175.50.171:80

이러한 로그 파일이 있는데 쉘프로그래밍을 하여 awk 정규표현식 같은 것을 활용 해서 파싱하고 싶습니다

1. 05/13/2018-23:12:27 (시간)

2. SURICATA DNS Unsolicited response(룰명)

3. UDP(프로토콜)

4. 168.126.63.1:53(출발지 아이피)

5.192.168.233.148:40729(목적지 아이피)

(위에 1번부터 5번까지는 예를 들은겁니다) 해당하는 부분만 뽑아서 DB 에 저장시키고 싶은데 어떻게 파싱해야할지.... awk '{print $1}' 같은거는 정확히 나오는 것도 있는데 각 로그의 길이 가 틀리다 보니 정규표현식으로 추출해야할것같은데 잘모르겠습니다 ㅠㅠ 도움주시면 정말 감사하겠습니다.

ymir의 이미지

$ cat something.log
05/13/2018-23:12:27.585699 [**] [1:2240001:2] SURICATA DNS Unsolicited response [**] [Classification: Generic Protocol Command Decode] [Priority: 3] {UDP} 168.126.63.1:53 -> 192.168.233.148:40729
05/13/2018-23:47:39.748349 [**] [1:10001:1] kim rule [**] [Classification: (null)] [Priority: 3] {TCP} 192.168.233.148:35662 -> 112.175.50.171:80
$
$ cat something.log | sed -e 's/\[[^]]*\]//g' -e 's/\(^[^ ]*\)\s*\([^{]*\)\(.*$\)/\1 \3 \2/g' | \
while read time proto src dir dst rule; do echo "time=$time proto=$(tr -d '{}' <<< $proto) src=$src dst=$dst rule=$rule"; done
time=05/13/2018-23:12:27.585699 proto=UDP src=168.126.63.1:53 dst=192.168.233.148:40729 rule=SURICATA DNS Unsolicited response
time=05/13/2018-23:47:39.748349 proto=TCP src=192.168.233.148:35662 dst=112.175.50.171:80 rule=kim rule

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

yoon의 이미지

혹시 sed -e 's/\[[^]]*\]//g' -e 's/\(^[^ ]*\)\s*\([^{]*\)\(.*$\)/\1 \3 \2/g' | \
while read time proto src dir dst rule; do echo "time=$time proto=$(tr -d '{}' <<< $proto) src=$src dst=$dst rule=$rule"; done 에대해서 설명 한번만 해주실수있나요???.... ㅠㅠ 이해가 잘 안되네요

ymir의 이미지

이해가 잘 안 가신다면, 저 명령들을 단계별로 끊어서 실행해 보세요.
중간 중간 결과를 보시면 어떤 순서로 데이터들이 바뀌었는지 이해하는데 도움이 될 겁니다.

먼저 불필요한 데이터를 잘라내고..(다행히 대괄호로 감싸여 있는 애들이 필요없는 애들이네요)
고정된 포맷을 갖도록 메시지를 재배치 하고.. (가변 길이를 가진 rule 을 맨 뒤로 보냈습니다)
그 포맷대로 읽어들여서 각각의 변수에 저장한 겁니다.

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

cinsk의 이미지

BEGIN {
    FS = " *\\[\\*\\*\\] *"
}
 
{
    f_time = $1
 
    # $2==> [1:2240001:2] SURICATA DNS Unsolicited response
 
    if (match($2, /\[([^]]*)\] *(.*)$/, m) == 0)
        next;
 
    f_dummy1 = m[1]             # [1:2240001:2]
    f_rule = m[2]               # SURICATA DNS Unsolicited response
 
    # $3==> [Classification: Generic Protocol Command Decode] [Priority: 3] {UDP} 168.126.63.1:53 -> 192.168.233.148:40729
    match($3, /\[([^]]*)\] *\[([^]]*)\] *{([^}]*)} *([^ ]+) *-> *([^ ]+)/, m)
 
    f_class = m[1]              # Classification: Generic Protocol Command Decode
    f_priority = m[2]           # Priority: 3
    f_protocol = m[3]           # UDP
    f_src = m[4]                # 168.126.63.1:53
    f_dst = m[5]                # 192.168.233.148:40729
 
    print "time:", f_time
    print "dummy:", f_dummy1
    print "rule:", f_rule
 
    print "class:", f_class
    print "priority: ", f_priority
    print "protocol: ", f_protocol
    print "src: ", f_src
    print "dst: ", f_dst
 
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.