@@ -5,11 +5,11 @@ function Find-AVSignature
5
5
6
6
Locate tiny AV signatures.
7
7
8
- PowerSploit Function: Find-AVSignature
9
- Authors: Chris Campbell (@obscuresec) & Matt Graeber (@mattifestation)
10
- License: BSD 3-Clause
11
- Required Dependencies: None
12
- Optional Dependencies: None
8
+ PowerSploit Function: Find-AVSignature
9
+ Authors: Chris Campbell (@obscuresec) & Matt Graeber (@mattifestation)
10
+ License: BSD 3-Clause
11
+ Required Dependencies: None
12
+ Optional Dependencies: None
13
13
14
14
. DESCRIPTION
15
15
@@ -37,19 +37,19 @@ Optionally specifies the directory to write the binaries to.
37
37
38
38
. PARAMETER BufferLen
39
39
40
- Specifies the length of the file read buffer . Defaults to 64KB.
40
+ Specifies the length of the file read buffer . Defaults to 64KB.
41
41
42
42
. PARAMETER Force
43
43
44
- Forces the script to continue without confirmation.
44
+ Forces the script to continue without confirmation.
45
45
46
46
. EXAMPLE
47
47
48
- PS C:\> Find-AVSignature -Startbyte 0 -Endbyte max -Interval 10000 -Path c:\test\exempt\nc.exe
49
- PS C:\> Find-AVSignature -StartByte 10000 -EndByte 20000 -Interval 1000 -Path C:\test\exempt\nc.exe -OutPath c:\test\output\run2 -Verbose
50
- PS C:\> Find-AVSignature -StartByte 16000 -EndByte 17000 -Interval 100 -Path C:\test\exempt\nc.exe -OutPath c:\test\output\run3 -Verbose
51
- PS C:\> Find-AVSignature -StartByte 16800 -EndByte 16900 -Interval 10 -Path C:\test\exempt\nc.exe -OutPath c:\test\output\run4 -Verbose
52
- PS C:\> Find-AVSignature -StartByte 16890 -EndByte 16900 -Interval 1 -Path C:\test\exempt\nc.exe -OutPath c:\test\output\run5 -Verbose
48
+ Find-AVSignature -Startbyte 0 -Endbyte max -Interval 10000 -Path c:\test\exempt\nc.exe
49
+ Find-AVSignature -StartByte 10000 -EndByte 20000 -Interval 1000 -Path C:\test\exempt\nc.exe -OutPath c:\test\output\run2 -Verbose
50
+ Find-AVSignature -StartByte 16000 -EndByte 17000 -Interval 100 -Path C:\test\exempt\nc.exe -OutPath c:\test\output\run3 -Verbose
51
+ Find-AVSignature -StartByte 16800 -EndByte 16900 -Interval 10 -Path C:\test\exempt\nc.exe -OutPath c:\test\output\run4 -Verbose
52
+ Find-AVSignature -StartByte 16890 -EndByte 16900 -Interval 1 -Path C:\test\exempt\nc.exe -OutPath c:\test\output\run5 -Verbose
53
53
54
54
. NOTES
55
55
@@ -63,10 +63,12 @@ http://www.exploit-monday.com/
63
63
http://heapoverflow.com/f0rums/project.php?issueid=34&filter=changes&page=2
64
64
#>
65
65
66
- [CmdletBinding ()] Param (
66
+ [Diagnostics.CodeAnalysis.SuppressMessageAttribute (' PSShouldProcess' , ' ' )]
67
+ [CmdletBinding ()]
68
+ Param (
67
69
[Parameter (Mandatory = $True )]
68
70
[ValidateRange (0 , 4294967295 )]
69
- [UInt32 ]
71
+ [UInt32 ]
70
72
$StartByte ,
71
73
72
74
[Parameter (Mandatory = $True )]
@@ -75,112 +77,110 @@ http://heapoverflow.com/f0rums/project.php?issueid=34&filter=changes&page=2
75
77
76
78
[Parameter (Mandatory = $True )]
77
79
[ValidateRange (0 , 4294967295 )]
78
- [UInt32 ]
80
+ [UInt32 ]
79
81
$Interval ,
80
82
81
83
[String ]
82
- [ValidateScript ({Test-Path $_ })]
84
+ [ValidateScript ({Test-Path $_ })]
83
85
$Path = ($pwd.path ),
84
86
85
87
[String ]
86
88
$OutPath = ($pwd ),
87
-
88
-
89
- [ValidateRange (1 , 2097152 )]
90
- [UInt32 ]
91
- $BufferLen = 65536 ,
92
-
89
+
90
+ [ValidateRange (1 , 2097152 )]
91
+ [UInt32 ]
92
+ $BufferLen = 65536 ,
93
+
93
94
[Switch ] $Force
94
-
95
95
)
96
96
97
97
# test variables
98
98
if (! (Test-Path $Path )) {Throw " File path not found" }
99
99
$Response = $True
100
100
if (! (Test-Path $OutPath )) {
101
101
if ($Force -or ($Response = $psCmdlet.ShouldContinue (" The `" $OutPath `" does not exist! Do you want to create the directory?" , " " ))){new-item ($OutPath )- type directory}
102
- }
102
+ }
103
103
if (! $Response ) {Throw " Output path not found" }
104
104
if (! (Get-ChildItem $Path ).Exists) {Throw " File not found" }
105
105
[Int32 ] $FileSize = (Get-ChildItem $Path ).Length
106
106
if ($StartByte -gt ($FileSize - 1 ) -or $StartByte -lt 0 ) {Throw " StartByte range must be between 0 and $Filesize " }
107
107
[Int32 ] $MaximumByte = (($FileSize ) - 1 )
108
108
if ($EndByte -ceq " max" ) {$EndByte = $MaximumByte }
109
-
110
- # Recast $Endbyte into an Integer so that it can be compared properly.
111
- [Int32 ]$EndByte = $EndByte
112
-
113
- # If $Endbyte is greater than the file Length, use $MaximumByte.
109
+
110
+ # Recast $Endbyte into an Integer so that it can be compared properly.
111
+ [Int32 ]$EndByte = $EndByte
112
+
113
+ # If $Endbyte is greater than the file Length, use $MaximumByte.
114
114
if ($EndByte -gt $FileSize ) {$EndByte = $MaximumByte }
115
-
116
- # If $Endbyte is less than the $StartByte, use 1 Interval past $StartByte.
117
- if ($EndByte -lt $StartByte ) {$EndByte = $StartByte + $Interval }
118
115
119
- Write-Verbose " StartByte: $StartByte "
120
- Write-Verbose " EndByte: $EndByte "
121
-
116
+ # If $Endbyte is less than the $StartByte, use 1 Interval past $StartByte.
117
+ if ($EndByte -lt $StartByte ) {$EndByte = $StartByte + $Interval }
118
+
119
+ Write-Verbose " StartByte: $StartByte "
120
+ Write-Verbose " EndByte: $EndByte "
121
+
122
122
# find the filename for the output name
123
123
[String ] $FileName = (Split-Path $Path - leaf).Split(' .' )[0 ]
124
124
125
125
# Calculate the number of binaries
126
126
[Int32 ] $ResultNumber = [Math ]::Floor(($EndByte - $StartByte ) / $Interval )
127
127
if (((($EndByte - $StartByte ) % $Interval )) -gt 0 ) {$ResultNumber = ($ResultNumber + 1 )}
128
-
128
+
129
129
# Prompt user to verify parameters to avoid writing binaries to the wrong directory
130
130
$Response = $True
131
131
if ( $Force -or ( $Response = $psCmdlet.ShouldContinue (" This script will result in $ResultNumber binaries being written to `" $OutPath `" !" ,
132
132
" Do you want to continue?" ))){}
133
133
if (! $Response ) {Return }
134
-
135
- Write-Verbose " This script will now write $ResultNumber binaries to `" $OutPath `" ."
134
+
135
+ Write-Verbose " This script will now write $ResultNumber binaries to `" $OutPath `" ."
136
136
[Int32 ] $Number = [Math ]::Floor($Endbyte / $Interval )
137
-
138
- # Create a Read Buffer and Stream.
139
- # Note: The Filestream class takes advantage of internal .NET Buffering. We set the default internal buffer to 64KB per http://research.microsoft.com/pubs/64538/tr-2004-136.doc.
140
- [Byte []] $ReadBuffer = New-Object byte[] $BufferLen
141
- [System.IO.FileStream ] $ReadStream = New-Object System.IO.FileStream($Path , [System.IO.FileMode ]::Open, [System.IO.FileAccess ]::Read, [System.IO.FileShare ]::Read, $BufferLen )
142
-
143
- # write out the calculated number of binaries
144
- [Int32 ] $i = 0
145
- for ($i -eq 0 ; $i -lt $ResultNumber + 1 ; $i ++ )
146
- {
147
- # If this is the Final Binary, use $EndBytes, Otherwise calculate based on the Interval
148
- if ($i -eq $ResultNumber ) {[Int32 ]$SplitByte = $EndByte }
149
- else {[Int32 ] $SplitByte = (($StartByte ) + (($Interval ) * ($i )))}
150
-
151
- Write-Verbose " Byte 0 -> $ ( $SplitByte ) "
152
-
153
- # Reset ReadStream to beginning of file
154
- $ReadStream.Seek (0 , [System.IO.SeekOrigin ]::Begin ) | Out-Null
155
-
156
- # Build a new FileStream for Writing
157
- [String ] $outfile = Join-Path $OutPath " $ ( $FileName ) _$ ( $SplitByte ) .bin"
158
- [System.IO.FileStream ] $WriteStream = New-Object System.IO.FileStream($outfile , [System.IO.FileMode ]::Create, [System.IO.FileAccess ]::Write, [System.IO.FileShare ]::None, $BufferLen )
159
-
160
- [Int32 ] $BytesLeft = $SplitByte
161
- Write-Verbose " $ ( $WriteStream.name ) "
162
-
163
- # Write Buffer Length to the Writing Stream until the bytes left is smaller than the buffer
164
- while ($BytesLeft -gt $BufferLen ){
165
- [Int32 ]$count = $ReadStream.Read ($ReadBuffer , 0 , $BufferLen )
166
- $WriteStream.Write ($ReadBuffer , 0 , $count )
167
- $BytesLeft = $BytesLeft - $count
168
- }
169
-
170
- # Write the remaining bytes to the file
171
- do {
172
- [Int32 ]$count = $ReadStream.Read ($ReadBuffer , 0 , $BytesLeft )
173
- $WriteStream.Write ($ReadBuffer , 0 , $count )
174
- $BytesLeft = $BytesLeft - $count
175
- }
176
- until ($BytesLeft -eq 0 )
177
- $WriteStream.Close ()
178
- $WriteStream.Dispose ()
137
+
138
+ # Create a Read Buffer and Stream.
139
+ # Note: The Filestream class takes advantage of internal .NET Buffering. We set the default internal buffer to 64KB per http://research.microsoft.com/pubs/64538/tr-2004-136.doc.
140
+ [Byte []] $ReadBuffer = New-Object byte[] $BufferLen
141
+ [System.IO.FileStream ] $ReadStream = New-Object System.IO.FileStream($Path , [System.IO.FileMode ]::Open, [System.IO.FileAccess ]::Read, [System.IO.FileShare ]::Read, $BufferLen )
142
+
143
+ # write out the calculated number of binaries
144
+ [Int32 ] $i = 0
145
+ for ($i -eq 0 ; $i -lt $ResultNumber + 1 ; $i ++ )
146
+ {
147
+ # If this is the Final Binary, use $EndBytes, Otherwise calculate based on the Interval
148
+ if ($i -eq $ResultNumber ) {[Int32 ]$SplitByte = $EndByte }
149
+ else {[Int32 ] $SplitByte = (($StartByte ) + (($Interval ) * ($i )))}
150
+
151
+ Write-Verbose " Byte 0 -> $ ( $SplitByte ) "
152
+
153
+ # Reset ReadStream to beginning of file
154
+ $ReadStream.Seek (0 , [System.IO.SeekOrigin ]::Begin ) | Out-Null
155
+
156
+ # Build a new FileStream for Writing
157
+ [String ] $outfile = Join-Path $OutPath " $ ( $FileName ) _$ ( $SplitByte ) .bin"
158
+ [System.IO.FileStream ] $WriteStream = New-Object System.IO.FileStream($outfile , [System.IO.FileMode ]::Create, [System.IO.FileAccess ]::Write, [System.IO.FileShare ]::None, $BufferLen )
159
+
160
+ [Int32 ] $BytesLeft = $SplitByte
161
+ Write-Verbose " $ ( $WriteStream.name ) "
162
+
163
+ # Write Buffer Length to the Writing Stream until the bytes left is smaller than the buffer
164
+ while ($BytesLeft -gt $BufferLen ){
165
+ [Int32 ]$count = $ReadStream.Read ($ReadBuffer , 0 , $BufferLen )
166
+ $WriteStream.Write ($ReadBuffer , 0 , $count )
167
+ $BytesLeft = $BytesLeft - $count
168
+ }
169
+
170
+ # Write the remaining bytes to the file
171
+ do {
172
+ [Int32 ]$count = $ReadStream.Read ($ReadBuffer , 0 , $BytesLeft )
173
+ $WriteStream.Write ($ReadBuffer , 0 , $count )
174
+ $BytesLeft = $BytesLeft - $count
179
175
}
180
- Write-Verbose " Files written to disk. Flushing memory."
181
- $ReadStream.Dispose ()
182
-
183
- # During testing using large binaries, memory usage was excessive so lets fix that
184
- [System.GC ]::Collect()
185
- Write-Verbose " Completed!"
176
+ until ($BytesLeft -eq 0 )
177
+ $WriteStream.Close ()
178
+ $WriteStream.Dispose ()
179
+ }
180
+ Write-Verbose " Files written to disk. Flushing memory."
181
+ $ReadStream.Dispose ()
182
+
183
+ # During testing using large binaries, memory usage was excessive so lets fix that
184
+ [System.GC ]::Collect()
185
+ Write-Verbose " Completed!"
186
186
}
0 commit comments