albtechportal

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Wednesday, 27 November 2013

Web Administration Scripts

Posted on 05:22 by Unknown
During the past month or so, I've also been dealing with an aggressive DDOS (that's a "distributed denial of service") attack on my server, one that's been a huge pain, as you might expect. What's odd is that with multiple domains on the same server, it's one of my less-popular sites that seems to have been the target of the attacks.
So, that's the jumping off point for this article's scripts: analyzing log files to understand what's going on and why.
To start, a handy check is to see how many processes are running, because my DDOS was characterized by a ridiculous number of comment and search scripts being triggered—hundreds a minute. How to check?
The ps command offers a list of running processes at any given time, but for many versions, all you see is the Web server "httpd" without any further details. The -C cmd flag narrows down output only to those processes, like this:

: ps -C httpd
PID TTY TIME CMD
20225 ? 00:13:21 httpd
28162 ? 00:00:01 httpd
...
5681 ? 00:00:00 httpd
5683 ? 00:00:00 httpd <defunct>
]]>
(Note the "defunct" process that's about to vanish.)
So one easy test is to see how many httpd processes are running:

$ ps -C httpd | wc -l
108
That seems like a lot, but this server is hosting several sites, including the super-busy AskDaveTaylor.com tech-support site, which sees more than 100k hits/day. So how does this vary over time? Hmm...still working on the command line:

$ while /bin/true
> do
> ps -C httpd | wc -l
> sleep 5
> done
108
107
103
99
94
91
87
84
91
121
120
116
So there's a max of 121 and a min of 87. But, what if I actually want to analyze this and get min, max and average over a longer period of time? Here's how I solve it:

#!/bin/sh
# Calculates the number of processes running that matches
# a set pattern over time, producing min, max and average.
min=999; max=0; average=0; tally=0; sumtotal=0
pattern="httpd" # ps -C pattern
while /bin/true
do
count=$(ps -C $pattern | wc -l)
tally=$(( $tally + 1 ))
if [ $count -gt $max ] ; then
max=$count
fi
if [ $count -lt $min ] ; then
min=$count
fi
sumtotal=$(( $sumtotal + $count ))
average=$(( $sumtotal / $tally ))
echo "Current ps count=$count: min=$min, max=$max, tally=$tally
↪and average=$average"
sleep 5 # seconds
done
exit 0
Notice in the script that I'm not falling into the trap of calculating the average by having a running average and somehow factoring in the latest value as a diminishing additive, but instead I use a sumtotal variable that keeps having the latest processor count added. That divided by tally is always the average, although at some point this probably would be greater than MAXINT (2**32) and would start to produce bad results. On a modern computer, however, that should take a while. (And the quantum, the period of time between iterations, also can be adjusted. Five seconds might be too granular for a process that's going to be run for hours or even days.)
The following are the first few lines of output. Notice how the minand max vary as the different values are calculated:

sh processes.sh
Current ps count=132: min=132, max=132, tally=1 and average=132
Current ps count=128: min=128, max=132, tally=2 and average=130
Current ps count=124: min=124, max=132, tally=3 and average=128
Current ps count=123: min=123, max=132, tally=4 and average=126
If I let the script run for a longer period of time, the values become a bit more varied:

Current ps count=90: min=76, max=150, tally=70 and average=107
During the 15 minutes or so that I ran the script, an average of 107 "httpd" processes were running, with a minimum of 76 and a max of 150.
Armed with that information, another script could keep an eye on things via a cron job, like this:

#!/bin/sh
# DDOS - keep an eye on process count to
# detect a blossoming DDOS attack
pattern="httpd"
max=200 # avoid false positives
admin="d1taylor@gmail.com"
count="$(ps -C $pattern | wc -l)"
if [ $count -gt $max ] ; then
echo "Warning: DDOS in process? Current httpd count =
↪$count" | sendmail $admin
fi
exit 0
That's a superficial solution, however, and it has two problems: 1) what I'd really like is to be able to identify the potential DDOS based on processor count and watch to see if it's sustained over the next few invocations of the script, and 2) once it's triggered, if it is a DDOS, in addition to everything else, I'll also start drowning in e-mail from this script saying essentially the same thing each time. Not good.
What the script needs is contextual memory so it can differentiate between a sudden spike in traffic and a persistent DDOS attack. In the former case, the script might trigger positive, then the next time it runs, it's all within acceptable limits again. In the latter case, once the attack starts, it'll probably just accelerate.
That's the opposite of the e-mail non-repeat condition though, because in the latter case, I want to know that the e-mail has been sent and not send it again within, say, a 60-minute window.
I'll dig in to both of those situations another time. For now, I need to get back to my server and keep bringing things back on-line, program by program, to try to avoid any problems. Stay tuned!

Source
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in Linux | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Review: Seagate 600 480GB SSD
    Seagate Joins the Fray It’s been quite an interesting turn of events over the past couple years in the storage industry. Whereas practical...
  • Top 10 Ways to Customize Your Desktop
    1 Expand You spend a lot of waking hours at your computer, so why not make it a little prettier (and more productive)? Here are 10 ways to...
  • CCBoot - LAN Boot Software for Windows
    LAN Boot Solution Background LAN boot is a technology based on IP (Internet Protocol), UDP (User Datagram Protocol), DHCP (Dynamic ...
  • ‘Strata’ for iOS and Android game review
    There are games that are fun. There are games that look great. And then there are games that do both. Strata is one such game that h...
  • Adobe Photoshop CS6 Extended 13.0 & Plugins + Textures
    Adobe Photoshop CS6 Extended 13.0 & Plugins + Textures | 3.5 GB Adobe Photoshop CS6 Extended software delivers even more imaging magi...
  • Intel NUC DC53427RKE / HYE Review
    Manufacturer: Intel UK Price (as reviewed): £308.32 (inc VAT) US Price (as reviewed): $539.99 (ex TAX) Preferred Partner Price: £308.32...
  • ASUS R9 270X DirectCU II TOP 2 GB
    AMD's new Radeon R9 270X draws its lineage more from the Radeon HD 7800 series than any other. The R9 270X is, for all intents and purp...
  • Corsair Raptor M40 Review
    Manufacturer: Corsair UK price (as reviewed): £44.99 (inc VAT) US price (as reviewed): $59.99 (ex Tax) Along with the Raptor M30, Corsai...
  • Call of Duty: Ghosts Review
    Developer: Infinity Ward Publisher: Activision Platforms: PC, X360, PS3, PS4, Xbox One Price: £39.99 Reviewing a Call of Duty game is a ...
  • How to remotely install apps on your smartphone
    You can download and install apps to your iPhone and Android phone without being anywhere near it. That sorcery is this? It isn't sorce...

Categories

  • Android
  • Apple
  • Audio
  • Blogger
  • C/C++
  • Cabling
  • Cameras
  • Cases
  • CISCO
  • Cooling
  • CPU
  • Desktop
  • DNS
  • Ebook
  • Fiber Optic
  • Gadgets
  • Game
  • Google
  • Graphic Card
  • Hardware
  • HDD
  • HTC
  • HTMLCSS
  • Hyper-V
  • Intel
  • iOS
  • iPad
  • Iphone
  • IT
  • jQuery
  • Laptop
  • Linux
  • Mac
  • MacTut
  • Microsoft
  • Mobile
  • Mouse
  • Networking
  • News
  • Nexus
  • Nokia
  • Nvidia
  • OS
  • PERIPHERALS & COMPONENTS
  • Photoshop
  • Printers
  • Programming
  • Projectors
  • PS4
  • Ram
  • RedHat
  • Review
  • Samsung
  • Scanners
  • Seagate
  • Security
  • Server2008
  • Server2012
  • Servers
  • Smartphone
  • Software
  • Sony
  • Storage
  • Tablets
  • TechNews
  • Template
  • Tutorials
  • TV
  • Ubuntu
  • Voip
  • Webdesign
  • Webiste
  • WebServer
  • Win7
  • Win8
  • Windows Phone
  • Wordpress
  • Workstation
  • XBOX

Blog Archive

  • ▼  2013 (495)
    • ►  December (35)
    • ▼  November (332)
      • DesktopOK 3.71 + Portable
      • Lynda - Up and Running with iOS SDK with Ron Lisle
      • SIM-free BlackBerry Z10 now available for $199
      • Hands-on with the Fitbit Force
      • Lenovo IdeaCentre Erazer X700 Gaming PC Review
      • LG G2 to get the Android KitKat update in Q1, 2014
      • Image shows Nokia Lumia 929 in white; leak reveals...
      • Watch Out for Seagate Drives Allegedly Sold Under ...
      • Starting a Website? Get a Domain Name and Hosting ...
      • Our Favorite Android, iOS and Windows Phone Apps o...
      • iBuypower Chimera 4SE FX Ultimate: AMD Gaming PC
      • How to Buy a Business Desktop
      • Google Nexus phones are vulnerable to rebooting vi...
      • Android 4.4 KitKat now seeding to Korean LG G2
      • Best For Music: PS4 or Xbox One?
      • Fix your Mac yourself with the new TechTool Pro 7!
      • How to buy the right sound bar to go with your TV
      • Understanding LSI SandForce SF3700 Series Controller
      • ASUS R9 270X DirectCU II TOP 2 GB
      • FIFA rolls out iOS and Android apps ahead of 2014 ...
      • How to Protect Wi-Fi network From Hackers
      • Recover Deleted Files From External Hard Drive
      • Best Video Editing Apps for Android Users-Must have
      • iPhone 6 Release Date,Full Specifications-whats new
      • How to r00t on server : Free E-Book
      • Samsung Galaxy S5 will not have OIS camera
      • Create Windows 8 Bootable Pendrive
      • Top 5 things to look for in a PC monitor
      • 5 things to look for in an 802.11ac router and ada...
      • 7 ways to maximise your Wi-Fi speeds
      • How to make your own Android ROM
      • VMWare Fusion 6 Opens Windows On Your Mac
      • Control iTunes & Apple TV From The Palm Of Your Ha...
      • Use Your Computer, Don’t Let It Use You: Five “U”s...
      • What’s Trending? Find Out With These Real-Time Dat...
      • Cool Websites & Tools – Remote Website Sharing, Lo...
      • Fascinating Earth: 5 Citizen Science Projects For ...
      • Brix on BRIX – Colin talks to Newegg about the lat...
      • Samsung Galaxy S5 Coming Early 2014 With 64-Bit Ex...
      • Get Your (Linux) Game On: Steam Autumn Sale Highli...
      • Firefox debuts new UI that looks like Chrome, but ...
      • World’s fastest wireless network hits 100 gigabits...
      • Intel Unveils 72-Core x86 Knights Landing CPU for ...
      • United States falls to 31st place in global broadb...
      • Xbox One vs. PS4: How They Stack Up Today
      • Got a Defective Xbox? Here’s How Microsoft Will Ma...
      • ASUS Releases “Faster Than Titan” ROG Mars 760 Vid...
      • How to get Android and iOS apps for free or on the...
      • How to remotely install apps on your smartphone
      • Community driven Core Apps convergence
      • Introduction & Packaging
      • Fix: Change PC Settings does not open in Windows 8...
      • How to make your Android look like it's running iOS 7
      • Switching Monitor Profiles
      • Web Administration Scripts
      • Top 10 apps from last week: Courses123, 500 Firepa...
      • Does the idea of a modular phone appeal to you?
      • How to Boost and Increase your Internet Speed
      • Ubuntu Linux server with ARM processor rolled out ...
      • 6 steps for setting up a small business server room
      • The best home backup plan options - Part 3: Extern...
      • The best home backup plan options - Part 4: Cloud ...
      • The best home backup plan options - Part 5: A comp...
      • How to Backup Files On Your Computer - Part 1: The...
      • The best home backup plan options - Part 2: Same m...
      • How To: Crucial Ballistix Elite Memory Installatio...
      • iBuypower Reveals First Steam Machine Prototypes
      • Kingston ships HyperX Predator 2800MHz Memory for ...
      • Samsung ChatON for Android now supports SMS and MM...
      • Grand Theft Auto: San Andreas coming to Android, i...
      • Google bakes photo goodies deep inside KitKat, but...
      • Oops -- YouTube's new commenting system actually i...
      • WD My Cloud Personal Cloud Server Review
      • Acer Unleashes Affordable, Touchscreen Chromebook
      • Apple iPhone 5s makes up a larger percentage of th...
      • Microsoft giving a free game to owners of Xbox One...
      • Samsung Galaxy S4 Google Play edition scores Andro...
      • Apple iPad mini 2 vs Google Nexus 7
      • Sony resetting some PSN passwords as a 'precaution...
      • Chromecast stand-in CheapCast now beams browser ta...
      • Galaxy S5 rumors, the Snapdragon 805 chip, and wil...
      • What about the Wii U?
      • Red Mac Pro Designed by Jony Ive, Marc Newson Fetc...
      • HTC One GPE getting Android 4.4 KitKat
      • BlackBerry ousts marketing and operations chiefs, ...
      • Recuva Data Recovery Software: Recover deleted fil...
      • Mass-market HTC One in Gold goes official
      • Assassin's Creed: Pirates launches for phones and ...
      • WP to put pressure on iOS in enterprise market
      • New Tegra 4-powered Nook leaked from GFXBench
      • Apple confirms PrimeSense buyout, paves way for mo...
      • Pen computing returns: Revenge of the stylus
      • 3D Systems Sense review: a 3D scanner for the mass...
      • Intel Demonstrates First 64-bit Android Tablet
      • DOOO for iPhone Makes Gorgeous To-Do Lists With Te...
      • Chinese supercomputer twice as fast as closest US ...
      • Best 802.11ac routers
      • Mac vs. PC: Is an Apple computer really better tha...
      • Android 4.4 Kit Kat Update Rolling Out to AT&T Mot...
      • Review: Seagate 600 480GB SSD
    • ►  October (12)
    • ►  September (27)
    • ►  August (2)
    • ►  July (10)
    • ►  June (42)
    • ►  May (35)
Powered by Blogger.

About Me

Unknown
View my complete profile