我已经被困在这件事上大约2天了。不幸的是,我可能只使用powershell (我并不擅长)。我希望使用regex匹配以下条件:
hxxp://www.website.org
google.com
www.google.com
foob://geller.xyz
hxxps://website.net/tree/branch/etc
我正在寻找urls &域名(用于IOC),它们是尖牙的和非尖牙的。url/域具有所有不同格式,除了它们总是包括anycharacter.anycharacter。我认为最好的匹配方法是如果字符串两边都有一个句点,然后与字符串的开头和结尾匹配。我最接近的例子是:
^.*\b[^.]+$\b然而,我尝试过的任何事情都没有得到积极的结果。如果有人有任何想法,我将不胜感激。为了表明我并不懒惰,这里是我为其他IOC准备的(我只是停留在这一点上):
#Select a file with a dialog. TXT only
Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
InitialDirectory = [Environment]::GetFolderPath('Desktop')
Filter = 'TXT (*.txt)|*.txt'
}
[void]$FileBrowser.ShowDialog()
$FileBrowser.FileNames
#Sets file & applies set string while creating first ouput file
#First regex matches IPV4 <-- works well!
$input_path = $FileBrowser.FileNames
$output_file = ‘C:\Users\output.csv'
$regex = ‘\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b’
select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } > $output_file
#Second regex2 matches domains <- is a problem
$regex2 = '\b^.*[^.]+$\b'
select-string $input_path -Pattern $regex2 -AllMatches | % { $_.Matches } | % { $_.Value } | Out-File -FilePath C:\Users\01100\Desktop\Folder\output.csv -Append
#Third matches any file extension <--- works well!
$regex3 = '^\.[a-zA-Z0-9]+$'
select-string $input_path -Pattern $regex3 -AllMatches | % { $_.Matches } | % { $_.Value } | Out-File -FilePath C:\Users\01100\Desktop\Folder\output.csv -Append
#Fourth matches any hash <--- works well!
$regex4 = '[A-Fa-f0-9]{15,}'
select-string $input_path -Pattern $regex4 -AllMatches | % { $_.Matches } | % { $_.Value } | Out-File -FilePath C:\Users\01100\Desktop\Folder\output.csv -Append
#Fifth matches defanged IPs <---works well!
$regex5 = '\b\d{1,3}[^b]\.[^b]\d{1,3}[^b]\.[^b]\d{1,3}[^b]\.[^b]\d{1,3}\b'
select-string $input_path -Pattern $regex5 -AllMatches | % { $_.Matches } | % { $_.Value } | Out-File -FilePath C:\Users\01100\Desktop\Folder\output.csv -Append发布于 2020-11-01 03:30:52
如果我理解正确的话,您想匹配代表域名或url的所有行?你会发现,这不是一件微不足道的事情。有各种正则表达式示例来验证域名或urls (例如here或here)。但是,要求它们越精确,它们就会变得越复杂。
在您的情况下,这将更加困难,因为您有不同的格式(有时有或没有方案或查询字符串)。
您的正则表达式需要多精确取决于您的用例以及您愿意投入多少工作。根据您的示例和问题标题,我想您需要一个非常基本的版本。
我建议这个,它应该适用于最常见的情况:
'^([a-z0–9-]+://)?([a-z0–9-]+\.)+[a-z0–9-]+(/.*)?$'简短的解释:
在开始时对可选模式进行([a-z0–9-]+://)?检查(没有特定的模式)
([a-z0–9-]+\.)+[a-z0–9]+域,含可选的子域,后跟顶级域
(/.*)?匹配可选查询字符串(未验证)
如果需要更高的准确性,可以使用此正则表达式作为过滤输入的第一步,然后对输入字符串执行进一步的测试。您可以使用validate if it's a valid url或check if the domain name exists。
发布于 2020-11-01 05:15:57
如果您使用方括号作为定义URL/URI的标准,那么只需查找它们即可。如果它们不在那里,那么URL/URL当然仍然是热的。
Clear-Host
(@'
hxxp://www[.]website[.]org
google.com
www.google[.]com
foob://geller.xyz
hxxps://website[.]net/tree/branch/etc
.foob://geller.xyz
foob://geller.xyz.
'@) -split "`n" |
ForEach-Object {
$Url = $PSitem
Try {Write-Warning -Message "Defanged URL $(([regex]::Matches($Url, '.*\[\.\].*').Value))"}
Catch {Write-Verbose "Fanged URL : $Url" -Verbose}
}
# Results
<#
WARNING: Defanged URL hxxp://www[.]website[.]org
VERBOSE: Fanged URL :
VERBOSE: Fanged URL : google.com
VERBOSE: Fanged URL :
WARNING: Defanged URL www.google[.]com
VERBOSE: Fanged URL :
VERBOSE: Fanged URL : foob://geller.xyz
VERBOSE: Fanged URL :
WARNING: Defanged URL hxxps://website[.]net/tree/branch/etc
VERBOSE: Fanged URL :
VERBOSE: Fanged URL : .foob://geller.xyz
VERBOSE: Fanged URL : foob://geller.xyz.
#>如果您只是尝试排除字符串开头或结尾带有“句点”的字符串,而不管字符串是什么,那么这就是一个有效的示例,利用RegEx的“not”表达式。
Clear-Host
(@'
hxxp://www[.]website[.]org
google.com
www.google[.]com
foob://geller.xyz
hxxps://website[.]net/tree/branch/etc
.foob://geller.xyz
foob://geller.xyz.
'@) -split "`n" |
ForEach-Object {
Try{([regex]::Matches($PSitem, '^((?!(((^\..*)|(.*\.$)))).)*$')).Value}
Catch{}
}
# Results
<#
hxxp://www.website.org
google.com
www.google.com
foob://geller.xyz
hxxps://website.net/tree/branch/etc
#>...or执行相反的操作,然后:
Clear-Host
(@'
hxxp://www[.]website[.]org
google.com
www.google[.]com
foob://geller.xyz
hxxps://website[.]net/tree/branch/etc
.foob://geller.xyz
foob://geller.xyz.
'@) -split "`n" |
ForEach-Object {
Try{([regex]::Matches($PSitem, '^((^\..*|(.*\.$)))')).Value}
Catch{}
}
# Results
<#
.foob://geller.xyz
foob://geller.xyz.
#>或者完全跳过所有的RegEx,然后这样说。
Clear-Host
(@'
hxxp://www[.]website[.]org
google.com
www.google[.]com
foob://geller.xyz
hxxps://website[.]net/tree/branch/etc
.foob://geller.xyz
foob://geller.xyz.
'@) -split "`n" |
ForEach-Object {
If($PSItem[0] -eq '.' -or $PSItem[-1] -eq '.'){}
Else {$PSItem}
}
# Results
<#
hxxp://www[.]website[.]org
google.com
www.google[.]com
foob://geller.xyz
hxxps://website[.]net/tree/branch/etc
#>您还可以使用.Net名称空间来查看返回的内容,并使用这些属性来做出决定。
Clear-Host
@'
hxxp://www[.]website[.]org
google.com
www.google[.]com
foob://geller.xyz
hxxps://website[.]net/tree/branch/etc
'@ -split "`n" |
ForEach {
Try {($PSItem.trim() -as [System.URI])}
Catch {$PSItem.Exception.Message}
}
# Results
<#
AbsolutePath :
AbsoluteUri :
LocalPath :
Authority :
HostNameType :
IsDefaultPort :
IsFile :
IsLoopback :
PathAndQuery :
Segments :
IsUnc :
Host :
Port :
Query :
Fragment :
Scheme :
OriginalString : google.com
DnsSafeHost :
IdnHost :
IsAbsoluteUri : False
UserEscaped : False
UserInfo :
AbsolutePath :
AbsoluteUri :
LocalPath :
Authority :
HostNameType :
IsDefaultPort :
IsFile :
IsLoopback :
PathAndQuery :
Segments :
IsUnc :
Host :
Port :
Query :
Fragment :
Scheme :
OriginalString : www.google[.]com
DnsSafeHost :
IdnHost :
IsAbsoluteUri : False
UserEscaped : False
UserInfo :
AbsolutePath : /
AbsoluteUri : foob://geller.xyz/
LocalPath : /
Authority : geller.xyz
HostNameType : Dns
IsDefaultPort : True
IsFile : False
IsLoopback : False
PathAndQuery : /
Segments : {/}
IsUnc : False
Host : geller.xyz
Port : -1
Query :
Fragment :
Scheme : foob
OriginalString : foob://geller.xyz
DnsSafeHost : geller.xyz
IdnHost : geller.xyz
IsAbsoluteUri : True
UserEscaped : False
UserInfo :
#>https://stackoverflow.com/questions/64624764
复制相似问题