|
| 1 | +--- |
| 2 | +title: Linux.Detection.Yara.Glob |
| 3 | +hidden: true |
| 4 | +tags: [Client Artifact] |
| 5 | +--- |
| 6 | + |
| 7 | +This artifact returns a list of target files then runs Yara over the target |
| 8 | +list. |
| 9 | + |
| 10 | +There are 2 kinds of Yara rules that can be deployed: |
| 11 | + |
| 12 | +1. Url link to a yara rule. |
| 13 | +2. or a Standard Yara rule attached as a parameter. |
| 14 | + |
| 15 | +Only one method of Yara will be applied and search order is as above. |
| 16 | + |
| 17 | +The artifact leverages Glob for search so relevant filters can be applied |
| 18 | +including Glob, Size and date. Date filters will target files with a timestamp |
| 19 | +before LatestTime and after EarliestTime. The artifact also has an option to |
| 20 | +upload any files with Yara hits. |
| 21 | + |
| 22 | +Some examples of path glob may include: |
| 23 | + |
| 24 | +* Specific binary: `/usr/bin/ls` |
| 25 | +* Wildcards: `/var/www/*.js` |
| 26 | +* More wildcards: `/var/www/**/*.js` |
| 27 | +* Multiple extentions: `/var/www/*\.{php,aspx,js,html}` |
| 28 | +* Windows: `C:/Users/**/*.{exe,dll,ps1,bat}` |
| 29 | +* Windows: `C:\Users\**\*.{exe,dll,ps1,bat}` |
| 30 | + |
| 31 | +NOTE: this artifact runs the glob plugin with the nosymlink switch turned on. |
| 32 | +This will NOT follow any symlinks and may cause unexpected results if |
| 33 | +unknowingly targeting a folder with symlinks. |
| 34 | +If upload is selected NumberOfHits is redundant and not advised as hits are |
| 35 | +grouped by path to ensure files only downloaded once. |
| 36 | + |
| 37 | + |
| 38 | +<pre><code class="language-yaml"> |
| 39 | +name: Generic.Detection.Yara.Glob |
| 40 | +author: Matt Green - @mgreen27 |
| 41 | +description: | |
| 42 | + This artifact returns a list of target files then runs Yara over the target |
| 43 | + list. |
| 44 | + |
| 45 | + There are 2 kinds of Yara rules that can be deployed: |
| 46 | + |
| 47 | + 1. Url link to a yara rule. |
| 48 | + 2. or a Standard Yara rule attached as a parameter. |
| 49 | + |
| 50 | + Only one method of Yara will be applied and search order is as above. |
| 51 | + |
| 52 | + The artifact leverages Glob for search so relevant filters can be applied |
| 53 | + including Glob, Size and date. Date filters will target files with a timestamp |
| 54 | + before LatestTime and after EarliestTime. The artifact also has an option to |
| 55 | + upload any files with Yara hits. |
| 56 | + |
| 57 | + Some examples of path glob may include: |
| 58 | + |
| 59 | + * Specific binary: `/usr/bin/ls` |
| 60 | + * Wildcards: `/var/www/*.js` |
| 61 | + * More wildcards: `/var/www/**/*.js` |
| 62 | + * Multiple extentions: `/var/www/*\.{php,aspx,js,html}` |
| 63 | + * Windows: `C:/Users/**/*.{exe,dll,ps1,bat}` |
| 64 | + * Windows: `C:\Users\**\*.{exe,dll,ps1,bat}` |
| 65 | + |
| 66 | + NOTE: this artifact runs the glob plugin with the nosymlink switch turned on. |
| 67 | + This will NOT follow any symlinks and may cause unexpected results if |
| 68 | + unknowingly targeting a folder with symlinks. |
| 69 | + If upload is selected NumberOfHits is redundant and not advised as hits are |
| 70 | + grouped by path to ensure files only downloaded once. |
| 71 | + |
| 72 | +aliases: |
| 73 | + - Windows.Detection.Yara.Glob |
| 74 | + - Linux.Detection.Yara.Glob |
| 75 | + - MacOS.Detection.Yara.Glob |
| 76 | + |
| 77 | +type: CLIENT |
| 78 | +parameters: |
| 79 | + - name: PathGlob |
| 80 | + description: Only file names that match this glob will be scanned. |
| 81 | + default: /usr/bin/ls |
| 82 | + - name: SizeMax |
| 83 | + description: maximum size of target file. |
| 84 | + type: int64 |
| 85 | + - name: SizeMin |
| 86 | + description: minimum size of target file. |
| 87 | + type: int64 |
| 88 | + - name: UploadHits |
| 89 | + type: bool |
| 90 | + - name: DateAfter |
| 91 | + type: timestamp |
| 92 | + description: "search for events after this date. YYYY-MM-DDTmm:hh:ssZ" |
| 93 | + - name: DateBefore |
| 94 | + type: timestamp |
| 95 | + description: "search for events before this date. YYYY-MM-DDTmm:hh:ssZ" |
| 96 | + - name: YaraUrl |
| 97 | + description: If configured will attempt to download Yara rules form Url |
| 98 | + type: upload |
| 99 | + - name: YaraRule |
| 100 | + type: yara |
| 101 | + description: Final Yara option and the default if no other options provided. |
| 102 | + default: | |
| 103 | + rule IsELF:TestRule { |
| 104 | + meta: |
| 105 | + author = "the internet" |
| 106 | + date = "2021-05-03" |
| 107 | + description = "A simple ELF rule to test yara features" |
| 108 | + condition: |
| 109 | + uint32(0) == 0x464c457f |
| 110 | + } |
| 111 | + - name: NumberOfHits |
| 112 | + description: This artifact will stop by default at one hit. This setting allows additional hits |
| 113 | + default: 1 |
| 114 | + type: int |
| 115 | + - name: ContextBytes |
| 116 | + description: Include this amount of bytes around hit as context. |
| 117 | + default: 0 |
| 118 | + type: int |
| 119 | + |
| 120 | +sources: |
| 121 | + - query: | |
| 122 | + -- check which Yara to use |
| 123 | + LET yara_rules <= YaraUrl || YaraRule |
| 124 | + |
| 125 | + -- time testing |
| 126 | + LET time_test(stamp) = |
| 127 | + if(condition= DateBefore AND DateAfter, |
| 128 | + then= stamp < DateBefore AND stamp > DateAfter, |
| 129 | + else= |
| 130 | + if(condition=DateBefore, |
| 131 | + then= stamp < DateBefore, |
| 132 | + else= |
| 133 | + if(condition= DateAfter, |
| 134 | + then= stamp > DateAfter, |
| 135 | + else= True |
| 136 | + ))) |
| 137 | + |
| 138 | + -- first find all matching glob |
| 139 | + LET files = SELECT OSPath, Name, Size, Mtime, Atime, Ctime, Btime |
| 140 | + FROM glob(globs=PathGlob,nosymlink='True') |
| 141 | + WHERE |
| 142 | + NOT IsDir AND NOT IsLink |
| 143 | + AND if(condition=SizeMin, |
| 144 | + then= SizeMin < Size, |
| 145 | + else= True) |
| 146 | + AND if(condition=SizeMax, |
| 147 | + then=SizeMax > Size, |
| 148 | + else= True) |
| 149 | + AND |
| 150 | + ( time_test(stamp=Mtime) |
| 151 | + OR time_test(stamp=Atime) |
| 152 | + OR time_test(stamp=Ctime) |
| 153 | + OR time_test(stamp=Btime)) |
| 154 | + |
| 155 | + -- scan files and prepare hit metadata |
| 156 | + LET hits = SELECT * FROM foreach(row=files, |
| 157 | + query={ |
| 158 | + SELECT |
| 159 | + OSPath, |
| 160 | + File.Size as Size, |
| 161 | + Mtime, Atime, Ctime, Btime, |
| 162 | + Rule, Tags, Meta, |
| 163 | + String.Name as YaraString, |
| 164 | + String.Offset as HitOffset, |
| 165 | + upload( accessor='scope', |
| 166 | + file='String.Data', |
| 167 | + name=format(format="%v-%v-%v", |
| 168 | + args=[ |
| 169 | + OSPath, |
| 170 | + if(condition= String.Offset - ContextBytes < 0, |
| 171 | + then= 0, |
| 172 | + else= String.Offset - ContextBytes), |
| 173 | + if(condition= String.Offset + ContextBytes > Size, |
| 174 | + then= Size, |
| 175 | + else= String.Offset + ContextBytes) ] |
| 176 | + )) as HitContext |
| 177 | + FROM yara(rules=yara_rules,files=OSPath, |
| 178 | + context=ContextBytes,number=NumberOfHits) |
| 179 | + }) |
| 180 | + |
| 181 | + -- upload files if selected |
| 182 | + LET upload_hits = SELECT *, upload(file=OSPath,name=OSPath) as Upload FROM hits |
| 183 | + |
| 184 | + -- return rows |
| 185 | + SELECT * FROM if(condition= UploadHits, |
| 186 | + then= upload_hits, |
| 187 | + else= hits ) |
| 188 | + |
| 189 | +column_types: |
| 190 | + - name: HitContext |
| 191 | + type: preview_upload |
| 192 | +</code></pre> |
| 193 | + |
0 commit comments