The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. February 12, 2012, 06:12:57 PM

Login with username, password and session length


Pages: [1] 2 3
  Print  
Author Topic: How To Have A Secure MySQL Database On Apache  (Read 2431 times)
webinfoguy25
Rookie
**
Offline Offline

Posts: 32


View Profile
« on: September 27, 2009, 11:58:45 AM »

I have a client that has a need for their zen cart shopping cart site coded in php, to have a secure mail form as well as have the internal search function be secure too.  I do not know what I should do to ensure the best operability, and security. The last company who did the site, made some custom alterations that included cgi-bin stuff, like a redirected admin in sh, as well as a manager interface that redirected too.  Not sure if this is something that was secure enough because the site was hacked, hence the reason they changed to having me run things.  I havent had much problems with zen cart being hacked, but I wanted to run it by you guys to see if you knew of anything else I could do to prevent things.

Thanks,
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 9792



View Profile
« Reply #1 on: September 27, 2009, 01:33:45 PM »

You must put the whole thing behind a firewall. Forward only pot 80 and 443 requests (if HTTPS is required) and NOTHING ELSE. FTP, MySQL, named etc etc all have understood and well exploited attack vectors. Use an encrypted VPN tunnel to get past the wall and then talk to your box that way. I got hacked *nasty* in about '02 because my so-called hardened *nix boxes had a port open with some vector I was unawares of. Bad news. Never again.

If you want to discuss this more, ping back here and I'll describe exactly what I do, which is what I'd label as cost-sensitive highest security.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
webinfoguy25
Rookie
**
Offline Offline

Posts: 32


View Profile
« Reply #2 on: September 27, 2009, 01:54:02 PM »

Yes please, if you could elaborate on this subject I would be most appreciative.  I have a client who is paranoid, and I don't blame him because it appears the previous guy working on the site was taking some extra security measures that didn't work.  So I want to make sure things are gravy.

Thanks,

in the cgi-bin it has some files called vdsbackup, and I am unsure what that is, so if you can tell me about this too great if not, then just let me in on the hyper-sensitive security steps needed for this site please.

Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 9792



View Profile
« Reply #3 on: September 27, 2009, 02:50:33 PM »

OK:

Put either IPCop or pfSense on a box that is what actually touches the internet. If you are non-dedi then this will be more problematic. If your customer really wants to do this right, tell him that you will need to do this. It's a fundamental piece of strong security.

Port-forward only 80 and 443 as I've described ... but port forward them to something else like 82 and 445 - which is where you'll listen on the box. Deny everything else at the firewall.

Get yourself either an openVPN (road warrior) or fixed-IP VPN tunnel to the IPCop or pfSense box. This will allow you unrestricted access to everything behind the wall, while still keeping the public face of the wall secure. Use, at the very least, 3DES and a stupidly-non dictionary password. Do not give it out or use it anywhere else.

If you must keep credit cards, then you're still not 100% in PCI compliance. If you're really really paranoid and want to do it right, this is what I did: I put up another fire wall behind my firewall and my CC storage box behind that. The actual web renderers do not store any CC information at all. All CC information is kept in a MySQL database behind the second wall. This information is accessed via port 80 only through a simple get and set Apache/PHP script. There is no access to the box other than that for my web renderers. I have to ssh into the firewall, then ssh into the CC box to get there myself.

Information on the CC Storage box is all encrypted, but the box itself does not contain the capability to decrypt it - it is simply a storage device. All information is stored in a double-salted, double encrypted by the renderers. So if someone physically stole my CC box, they'd still have nothing. If they stole my renderers, they'd have the encryption routines, but not the data. Another step: the actual encryption key is stored in a root-only directory in a root-only file (chmod 400). There is a root-level cron job that opens that file and then uses a special web request to push the encryption key into RAM via apc_store. The point of this, is that if someone managed to gain some kind of access to the box, there's no instance of the key in any of my code: only an apc_fetch command to get it. In other words, you can't even write a script to ask for it, because the memory that holds the key is attached to Apache - you'd have to be another web page to get it (note in the next paragraph how I deny that possibility). Or hack my root password, but good luck with that. The pointers in the database are encrypted as well, so the actual row looks like 128 bytes (2 fields of 64) of hex and nothing else. The get method grabs a pointer from it's own database, encrypts it with salt based on the actual record it wants - it asks for and receives that data from the CC Storage device. Then using that encryped ID as salt, it can decrypt the actual CC information to throw the request at my merchants. This is all done in code and nothing is ever put on the disk.

Do not use telenet but rather SSH to access your boxes, even though they are behind your wall. Use an account OTHER than root just on the off chance that someone was sniffing you, they'd not get your access to the key files.

Precisely permission your MySQL boxes for ONLY local-machine access. This can include phpMyIDE, but have that presented on a port OTHER than 82 or whatever you're serving actual web pages on, so that it cannot be accessed at all from the outside world - only via your tunnel.

Shut off all unnecessary services. You no longer need much of anything running other than system stuff, Apache, FTP (which you might consider SFTP, but since you'd only be able to access it across an encrypted tunnel, this is probably overkill) MySQL and sshd.

Put something like: Include /usr/local/apache2/conf/sitefiles/*.inc
into your httpd.conf. Then lock down the httpd.conf so it is not editable, even by the root (just to keep things consistent). Then in the lower directory, make files for each site you want to have running on the box - make them small, tight and extremely obvious. The benefit here is that we want Apache to do EXACTLY what we have specified and NOTHING ELSE. You should be able to look at your Apache files and know EXACTLY what they're doing. Now, for an extra little bit of coolness, all of my sites mod_rewrite into a single, main.php file (unless they are suffixed as .css, .js, .pdf, .jpeg, .jpg, gif & .png). Then at the top of my main.php, if the inbound URL is not exactly what I expect, it is simply dumped. That's not all however: each URL is actually translated against a table of URLs for what I know that site will do. This is both for SEO and for security. If the URL is not in the table, I simply return the home page. I've had people tell me I could get a duplicate content penalty for this, but they are FOS. The point here is that I want immediate control of every URL that comes at me ... I NEVER want Apache to serve something (a page, at least) without I've had the chance to look it over first.

So:
   http://www.mydomain.com/a-keyword-stuffed-dir/a-keyword-stuffed-url.html
will be translated to:
   /simpleInclude.php?file=contentFile.php&menu=3.2.7&header=blue

Note that this gives me both extraordinary control over what I want the surfer/spider to see, with extraordinarily capable URLs that I'd *NEVER* want the outside world to see. And lastly, if I don't have the exactly inbound URL in the list, nothing happens. Period.

I think that ought to get your gears spinning. Good luck!
/p
« Last Edit: September 27, 2009, 03:00:25 PM by perkiset » Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
webinfoguy25
Rookie
**
Offline Offline

Posts: 32


View Profile
« Reply #4 on: September 27, 2009, 06:06:46 PM »

You got my gears spinning alright, but you are talking to a newbie to security. I know how to use putty, and that about it.  I actually don't think they will want to have me housing any boxes with their website on it, unless I know what I am talking about I can't convince them.  I don't want to be held responsible, so I hope this is intuitive.  How much time does it take to set this all up?

Thanks nice post.
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 9792



View Profile
« Reply #5 on: September 27, 2009, 06:28:25 PM »

There's a pretty significant amount to learn there, particularly since your puTTY remark tags you as a Windowster, rather than nixHead. That's even more problematic.

The biggest single thing you should do is work with the ISP to put your box behind a firewall, then access any services *other* than HTTP via a VPN tunnel. That will get you an awful lot further than you are today. So your steps will be:
  • Contact the ISP. Find out what services THEY offer for putting his/her site behind a firewall. If he's dedi or CoLo then you'll want to discuss with them what their pre-packaged solutions look like, or what it costs to put another machine in front of your existing machine.
  • If they have no firewall capability at all, then you may need to address going to another ISP.
  • Once you have the box coming up behind a firewall, then you'll need to build a Virtual Private Network (VPN) to get through the wall and into the box. Then, if it's a Windows box, simple VNC will let you control it - if it's *nix, you'll want to use ssh and control it from a command line rather than getting a GUI going. No sense wasting the cycles or RAM if you don't need it.
  • Configure the firewall to port-forward requests to port 80 (HTTP) into the server.

This will get you about 1000% stronger than you are today, but as you can see - there's some stuff to learn. As you do your research, ping back and we can all assist.
« Last Edit: September 27, 2009, 07:27:01 PM by perkiset » Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
webinfoguy25
Rookie
**
Offline Offline

Posts: 32


View Profile
« Reply #6 on: September 27, 2009, 07:05:10 PM »

Ok will do.  Thanks,
Logged
webinfoguy25
Rookie
**
Offline Offline

Posts: 32


View Profile
« Reply #7 on: September 27, 2009, 08:10:09 PM »

Oh and one more thing.  To make this understandable to the client, how would categorize the level of importance, or strength these measures are?  Like what steps would make something

minimum level of security?, medium?, large?, extra large?

I guess what I am trying to say is: I am trying to give him all his options so he has to bear the decision, and not me.  Devilish
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 9792



View Profile
« Reply #8 on: September 28, 2009, 08:44:50 AM »

(Depth of effort == Amount of Paranoia + 1)

The most important first step is the firewall. That's really a gigantic step forward and the thing that will make the most fundamental and profound difference.

Other than that, it's about where his paranoia lives. Sorry, kinda hard to rate things beyond that.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #9 on: September 28, 2009, 08:54:07 AM »

Not to undermine Perks, but I just want to point out that there is nothing inherently more secure about having a dedicated firewall box as opposed to just running a solid firewall (like pfSense or openWall/M0n0wall, etc.) on your LAMP server. It's possible to completely secure a LAMP server without jumping through that many hoops as well. I'm only saying this because I have a bunch of cost-sensitive clients and while Perk's approach is what I recommend to every one of them, most opt for a slightly more straightforward, single-box solution.

Actually, what I've been doing lately is selling them a box with like 16 cores and tons of RAM, and then configuring virtual machines in an entire "virtual backoffice" setup. That way all their systems are segregated in the way Perks describes, without having to invest in new hardware very often and with the inbuilt ability to upgrade merely by flipping another switch.

EDIT: just be sure if you sell them on the virtualization route (and it shouldn't be hard, it is the wave of the future after all, just toss around the words "elastic cloud" and they'll melt like butter) that you buy a beefy enough box, preferably with multiple gigabit+ NICs.
« Last Edit: September 28, 2009, 09:03:00 AM by vsloathe » Logged

hai
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 9792



View Profile
« Reply #10 on: September 28, 2009, 12:56:43 PM »

No undermining, certainly. But if you have FTP and SSH and (really bad) SAMBA and HTTP and SMTP and and and ports/services open on your box for *any* purpose you are inherently less secure.

The virtuals solution you provide is absolutely another way of doing it - in fact it's almost exactly the same except that it's virtual machines and not physical. The only deal there is that one way or another, the management of that box will publicly accessible ergo, less secure. And, of course, you're paying a processor penalty for having one box running the virtuals as well as all the processes inside - in some cases, a complete duplication of effort against one processor set.

My philosophy after getting my ass handed to me, was to simply eliminate all potential vectors by physical separation - if you *can not* touch a box (there) then it's not a worry. If it can be touched (there) then it will be a worry.

All that said, price is an issue for a lot of folks. The value of my stomach acid far outstrips the cost of my rig.

And now back to you WIG: the way that I make this cost effective is to not do it over and over again. I have lots of boxes behind my firewalls so I have an economy of scale that works for me. I do the hosting et al for my clients so I charge them and take responsibility rather than doing it elsewhere. But the up front cost, knowledge and stress of THAT is also beyond a lot of people.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #11 on: September 28, 2009, 01:02:27 PM »

Quote
The only deal there is that one way or another, the management of that box will publicly accessible ergo, less secure.

eh, only technically. I can pretty much set it up exactly as you've described if I want. I mean the Apache server and the pfSense server may *technically* be the same box, but as far as any human is concerned they couldn't be more distinct from one another.
Logged

hai
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 9792



View Profile
« Reply #12 on: September 28, 2009, 01:10:45 PM »

I guess I'm not sure how you'd do it ... I assumed that both pfSense and Apache would need to be in separate virtuals, with a public-facing IP on the pfSense box talking to internal addresses on the Apache box. But all that said, both of those will need to reside on a box running ubuntu or something that you'll need to talk to, no?
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #13 on: September 28, 2009, 01:14:44 PM »

Nah, I would use a hypervisor. ESXi is decent but I use Xen for production systems unless the client specifically wants a paid solution (in which case, it's ESXi and VMWare's virtualsphere or wtfever it's called).

You can make your hypervisor so that it isn't even aware that you have an ethernet NIC, much less open ports.

EDIT: for those unfamiliar with virtualization concepts, a hypervisor is a "host OS" on which all your guest OSes reside. The key difference is that a hypervisor is really just setup to run virtual machines and nothing else. It doesn't need anything but a Kernel, and the Xen kernel is a stripped-down brick when it comes to security. Alternatively, you can use any linux kernel you want on Xen (or other OSes if you use ESXi or similar). The box that runs the VMs may as well not exist, it's just there for when you need to switch a VM on or off, and you can restrict that so that only physical access to the machine has those privs.
« Last Edit: September 28, 2009, 01:17:17 PM by vsloathe » Logged

hai
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 9792



View Profile
« Reply #14 on: September 28, 2009, 01:20:51 PM »

Ah ... so the superstructure box can be locked down to local, physical control - that's good. I getcha.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1] 2 3
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!