Sunday, April 20, 2008

Automated Shellcode Analysis

Working with Lenny Zeltser in his SANS Malware Analysis course, I was able to cobble together a script to automate shellcode analysis preparation. The analysis involves wrapping the shellcode in a shell function, then compiling it into a small program. There are several ways to do this, including using an empty C main function (Lenny) and injecting the shellcode into a husk executable (iDefense). I didn't want to use the injection method since it falls prey to two problems. 1) The husk is always the same size, only the shellcode changes (might be good for diff comparisons). I wanted something smaller. 2) The shellcode must fit into the blank space in the husk, which opens it up to possible overflow attack or, at least, shellcode size limitation. Compiling yourself avoids this problem.

Here's the script I hacked together. With a little work, I plan to put it to a CGI frontend with a return of the disassembled output.

Shellcode Analysis Script

bt shellcodetest # cat shellcode_analyzer.sh
#!/bin/sh
# a little script to clean up shellcode, parse it, convert to
little-endian, and compile into the smallest wrapper possible.
# based on instruction provided by Lenny Zelster.

# hacked together by
# Andrew Hunt
# 4/19/08
# Copyright 2008 Creative Commons Share-Alike
# http://creativecommons.org/licenses/by-sa/3.0/us/

# this script is very alpha, assuming you will call with a text file
argument. text file should have the unicode pasted in it,
# like `./shellcode_analyzer.sh shellcode.unicodeorhex.strings.file`.
# it comes without any warranties or promises.
#
# user needs to replace the script path and script for unescaping
unicode/hex/other with the path to their own script or an unescaping
routine.
# some replacement suggestions if you don't have a script...
#
# perl -pe 's/\\x(..)/chr(hex($1))/ge'
# perl -pe 's/[\%\\]u(..)(..)/chr(hex($2$1))/ge'

cat $1 | perl -pe "s/\'\+\'//g" | perl -pe 's/\"\+\"//g' | perl -pe
's/\"//g' | perl -pe "s/\'//g" | perl /mnt/sda1/scripts/unescape.pl >
/tmp/test5.bin

cat /tmp/test5.bin | hexdump | awk '{print $2 $3 $4 $5 $6 $7 $8 $9}' |
perl -pe 's/(..)(..)/print("\"\\x".$2."\\x".$1."\"\n")/ge' | grep -P
'^\"'>/tmp/test6.hex


echo "unsigned char shellcode[]="> /tmp/test7.c
cat /tmp/test6.hex >> /tmp/test7.c
echo ";" >> /tmp/test7.c
echo "int main(){}" >> /tmp/test7.c

gcc -c -o shellcode-compiled /tmp/test7.c
objdump -D shellcode-compiled > shellcode.disasm
rm -f /tmp/test*

# optional
less shellcode.disasm

Friday, February 29, 2008

Phish Me

Last night's NOVASec meeting was an interesting affair. After the presentation by Stratum Security, there was a lively discussion about targeted attacks and how unprepared many organizations are in facing this threat. Intrepidus Group founder Aaron Higbee introduced me to his phishme.com site. Looking over the service, it delivers an essential user training and social engineering testing function by allowing the penetration tester to develop custom targeted emails against a client. It tracks the deliveries, who opens the emails and who clicks the baited links inside, generating a graphical report for delivery to management. This is a great way to gauge the effectiveness of user awareness training programs and identify susceptible users that need retraining. Great product, Intrepidus!

Saturday, February 23, 2008

Idea for End-point Javascript Obfuscation Blocking

I was fortunate enough to attend a presentation by Daniel Peck, of CaffieneMonkey fame, on the characteristics of the javascript obfuscation attack. What struck me the most about the presentation were the graphs Mr. Peck included comparing the object characteristics of malicious scripts. While most scripts have a high number of interfacing calls (doc.write, writeln, print, alert, etc) with rather short 'string' content in their tags to direct the content loading, malicious scripts have relatively few interfacing objects with HUGE strings objects (upwards of 80-90% of the script). Over the graphs of scanned sites he showed, it seems clear this is consistent across malicious v non-malicious sites. If this statistical analysis could be integrated into a plug-in, it would make for a rudimentary, yet effective barrier to obfuscated iframes and droppers. The plug-in would have to prevent script execution based on a user-defined ratio or percentage of calls/string content.

With a skeleton plug-in and the statistical analysis code in CaffieneMonkey open source, integration of the two should be possible.

Sunday, December 23, 2007

Getting Started

I finally decided to stop spending my time as a web administrator and let Google do the legwork for me. I'd rather be accepting malicious connections on my honeypot anyway and not worrying about whether my content engine is hacked. I'll be transitioning my existing content here in the coming weeks.

Wednesday, June 13, 2007

Reconstituting Base64 Attachments

Originally from ISC's Pedro Bueno, http://isc.sans.org/diary.html?storyid=2955&dshield=a5b4c2b44d94b5810c38069ca8f981d5

perl -MMIME::Base64 -e 'print decode_base64(join("", <>))' badfile.exe.file

Friday, May 4, 2007

Creating a Void11 Counter-Offensive Wi-Bomb on Auditor

Requirements:

  • Auditor ISO (http://mirror.switch.ch/ftp/mirror/auditor/ )
  • Laptop with available hard-drive for installation. Recommend 256+ MB RAM and P4 or better CPU. Must have PCMCIA slot. On-board NIC for external connectivity and management if desired.
  • SMC 2532W-B Intersil Prism-based WiFi card. Also has external antennae jacks.
  • 9dBi omni- or dual semi-directional patch antennas

Boot up Auditor and perform a permanent installation to the hard drive. Create these files on the system, then run the installation file. Test by executing '/etc/init.d/void11 start' with the SMC card inserted. Reboot and verify operation with a test of a "rouge" AP and an independent client attempting to connect. Best performed when you allow the client to connect, do a perpetual ping, then turn on your Wi-bomb appliance and watch it die.

You can later integrate known-good access points into the appliance by creating a matchlist and adjusting the OPTIONS parameter in the 'void11' script to include "-l /path/to/matchilst". See William Hidalgo's excellent writeup for more inforamtion on formatting the matchlist file.

References:

William Hidalgo's well-written article on using Void11 as a counter-offensive tool to protect networks : "Void11 Rouge Access Point Counter Offense" (http://remote-exploit.org/research/void11rougeaccesspoint.html )

Scripts:

void11_installer.sh

#!/bin/sh
cp -f void11 /etc/init.d/
chmod 755 /etc/init.d/void11
cp -f void11.cron /etc/cron.daily/void11
chmod 755 /etc/cron.daily/void11
ln -s /etc/init.d/sysklogd /etc/rc.boot/S65syslog
ln -s /etc/init.d/void11 /etc/rc.boot/S99void11
ln -s /etc/init.d/void11 /etc/rc6.d/K15void11
ln -s /etc/init.d/void11 /etc/rc0.d/K15void11
ln -s /etc/init.d/sysklogd /etc/rc0.d/K10syslog
ln -s /etc/init.d/sysklogd /etc/rc0.d/K10syslog
touch /var/log/void11
chmod 600 /var/log/void11
echo Now that installation is complete, run Void11 by issuing
echo
echo /etc/init.d/sysklogd start
echo /etc/init.d/void11 start

voider.sh

#!/bin/sh
#rm -Rf /etc/pcmcia
#cp -R /etc/pcmcia-hostap /etc/pcmcia
rm -f /etc/pcmcia/wlan-ng*
killall -HUP cardmgr
cardctl eject
sleep 1
cardctl insert
sleep 2
iwpriv wlan0 hostapd 1
iwconfig wlan0 mode master
sleep 1
void11_hopper > /dev/null &
void11_penetration -t 1 -d 10 wlan0

void11.cron

#!/bin/sh

test -x /usr/local/bin/void11_hopper || exit 0
test -x /usr/local/bin/void11_penetration || exit 0
/etc/init.d/void11 restart

void11

#!/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

OPTIONS="-t 1 -d 10 wlan0"

PIDFILE=/var/run/void11_penetration.pid
RIDFILE=/var/run/void11_hopper.pid
DAEMON=/usr/local/bin/void11_penetration
RAEMON=/usr/local/bin/void11_hopper

case "$1" in
start)
echo -n "Starting WiFi blackout service: void11"

if [ ! -x $RAEMON ]; then
echo "void11_hopper script missing - not starting"
exit 1
fi
if [ ! -x $DAEMON ]; then
echo "void11_penetration binary missing - not starting"
exit 1
fi
rm -f /etc/pcmcia/wlan-ng*
killall -HUP cardmgr
cardctl eject
sleep 1
cardctl insert
sleep 2
iwpriv wlan0 hostapd 1
iwconfig wlan0 mode master
sleep 1
$RAEMON > /dev/null &
$DAEMON $OPTIONS > /var/log/void11 &
echo "."
;;

stop)
echo -n "Stopping WiFi blackout service: void11"
killall void11_penetration
sleep 2
killall void11_hopper
sleep 2
echo "."
;;

reload)
$0 restart
;;

restart|force-reload)
$0 stop
sleep 2
$0 start
;;

*)
echo "Usage: /etc/init.d/void11 {start|stop|reload|restart|force-reload}" >&2
exit 1
;;
esac

exit 0

Tuesday, March 27, 2007

Better WHOIS Lookups

Origin AS/WHOIS lookups via scripts, use DNS-based lookups at Team Cymru:

nslookup -type=TXT 31.108.90.216.origin.asn.cymru.com