Monday, January 26, 2015

Netstat, ports and taskkill

The netstat command in windows I found very useful for identifying problems with Apache not running. Most of the time I found the actual problem was that something else had already bonded on port 80.
Simply run netstat -ano in the command line (as an administrator of course) and end the process that is causing the hiccup. be warned sometimes this is a system process. You may want to find out more about the process first using the task manager and good old fashioned google searching.

For more info on how to do any of this visit:
http://security.fnal.gov/handouts/IdentifyingOpenPortsWinXP-2003.pdf

Taskkill
The taskkill command is nice if you have a task that just will not close or you opened up a billion of the same thing and you only have a remote connection you don't want to loose. There are other uses for it but I have mostly used it for those things.
If you know the PID (which you can get using the task manager or the aforementioned netstat) or the imagename (which you can use if it is multiple instances of the same)
taskkill /IM imagename /T /F
or
taskkill /PID processid /T /F
The /T kills and child processes which were started by it and the /F forcefully terminates it

Monday, January 12, 2015

PHP and SQL


So I used to use these daily (MySQL more than anything and PHP mostly I just looked at and told the dev team it looks like your bug is around here so reading more than writing) and there were a few things I had to look up every time because I'm forgetful. Before I start posting a bunch of other Random Tech Info I wanted a post for my original reason for making the blog! Unfortunately I have less use of it now but for anyone else who may need it here it is!

PHP
Time Zones I commonly needed:
America/Denver
America/Los_Angeles
America/New_York
Full list is available here:

http://php.net/manual/en/timezones.america.php

Error Reporting:
Nothing is more frustrating than bad code, except bad code that doesn't load because of a WAMP errorloging:
[add pic of file here]
Change E_ALL to E_ERROR in the php.ini file. In my opinion the only time a production server should report all errors is in testing or when troubleshooting. Otherwise you are using resources that can be used elsewhere in production.

Other types of "Bad code"
Bad code isn't just code with bugs in it. It is also code that is written that is hard to understand, or code that is written so complicated that it takes forever to run.
Sometimes though it is not as easy as simply "fixing" the code as it is changing the way things are done (for example calculating a number every time versus storing the number in the database once it is calculated and then checking to see if it needs recalculating because other numbers have changed or not because nothing has changed).

Long store short, Sometimes this is needed:
max_execution_time = 300
that is 5 minutes. I don't recommend going higher ever as (from what I have heard) other vulnerabilities may exist and that gives hackers a 5 minute window in that area.

MySQL
Max Allowed Packets-
Most commonly used when restoring large databases (I think the WAMP build was way outdated).
Packets larger than max_allowed_packet are not allowed.
The max_allowed_packet variable can be set globally by running a query.
However, if you do not change it in the my.ini file (as dragon112 suggested), the value will reset when the server restarts, even if you set it globally.
To change the setting for everyone until the server restarts:
SET GLOBAL max_allowed_packet=1073741824;
OR
Change in the my.ini file. Include the single line under [mysqld] in your file
max_allowed_packet=500M
now restart the MySQL service and you are done.
[add pic of file]
http://opensource-soa.blogspot.com/2008/08/change-mysql-maxallowedpacket-variable.html

Creating a TEMP table
CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS (SELECT * FROM table1)
You can get much more in-depth with selecting different columns etc.

Creating table from scratch with unique id's in sequential order to insert data manually
I hated doing this as it involved a lot of hand work until I learned how to do it this way.
SELECT  column1,
        column2,
        column3,
        @curRow := @curRow + 1 AS row_number
FROM    table
JOIN    (SELECT @curRow := 0) r;

Intervals are very useful so you can find things that are most recent or in a certain date range:
SELECT * FROM table
WHERE datetime BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW();

For SQL Server Identity Insert:
SET IDENTITY_INSERT IdentityTable ON
INSERT IdentityTable(TheIdentity, TheValue) VALUES (3, 'First Row')
SET IDENTITY_INSERT IdentityTable OFF

Using Variables
I didn't have to do this a lot but I had to look it up every time. Essential I had to create a table using information from other tables (if you ever have to do this I am sorry, consult your programmer about just grabbing the information he needs from the places it actually exists... and don't let him BS you he can make it work). I had to make unique ID's for a table and this was the simplest way I could find. Let me know if someone has a better.
First declare your variable.
DECLARE @i AS int;
Then set it to a starting point:
SET @i= 0;
Then your select query:
SELECT @i+1, column1, column2 FROM table1 JOIN table2 on table1.columnA = table2.columnB WHERE columnC = (something);