Executing PHP code stored in SQL DB - php

I've read a few posts about this that suggest using eval but then say how its dangerous (I don't know why) but I don't think it will work for my current need.
My issue is that I have a blog post db table which has a column that contains all the content of the post. In this content I want to include a poll that lets the user vote a or b. This requires a database call to get the current votes to display to the user after they have voted . I'm guessing javascript would be possible but then anyone could look at the source code and see the query I'm making to the database which is risky. I have the php code working fine I just need a way to process it after code has been obtained from the DB

Related

I have found a $_GET['var'] == an SQL statement - What are the risks?

I have discovered that some old (2009) code that was written for a website, did, under certain circumstances on a search query save the SQL as a a $_GET variable!
When the search was carried out, the details are POSTED and then sanitized, and the results are paginated with the LIMIT clause in MySQL. If there is more than one page (ie +30 results) the pages are anchor links in the HTML with a GET var containing the SQL statement.
I know, this is absolutely not the way to do this. It's old code I've just seen it by chance. This needs to be fixed.
So I've fixed it, sanitized it and used an alternative method to reload the SQL, BUT:
My question is thus:
The page outputs the data relating to thumbnail images, all data is output as named array var (the original clause is a SELECT * clause), so if someone does abuse the GET variable, the page itself will only output the columns named,
I have managed to DELETE rows from the DB using the GET abuse, I would like to think the abuse is only effective if the result is not involving any returned output (such as DELETE) but I don't know; so given that the user can input anything into the GET clause but only get the displayed output of what's coded (ie named columns in a 30 row array) -- what other abuses can this gaping hole be open to?
Further details: The code is MySQLi
A tool like SQLMAP can probably take over the entire server and do with it whatever the user wants.
Having an unsanitized database input isn´t even hacking anymore, it´s waiting for someone to run a script on your machine and basically own it from that point on.
What the attacker can do depends on your database configuration and database user access. If you create a new user with a permission to only SELECT that one specified table, and use that user for that particular script, the harm it can do is reading data from that table.
Still this is bad practice. Never use it.

Recreate a database using existing php code

So I have an old website which was coded over an extended period of time but has been inactive for 3 or so years. I have the full PHP source to the site, but the problem is I do not have a backup of the database any longer. I'm wondering what the best solution to recreating the database would be? It is a large site so manually going through each PHP file and trying to keep track of which tables are referenced is no small task. I've tried googling for the answer but have had no luck. Does anyone know of any tools that are available to help extract this information from the PHP and at least give me the basis of a database skeleton? Otherwise, has anyone ever had to do this? Any tips to help me along and possibly speed up the process? It is a mySQL database I'm trying to use.
The way I would do it:
Write a subset of SQLi or whatever interface was used to access the DB to intercept all DB accesses.
Replace all DB accesses with the dummy version of yours.
The basic idea is to emulate the DB so that the PHP code runs long enough to activate the various DB accesses, which in turn will allow you to analyze the way the DB is built and used.
From within these dummy functions:
print the SQL code used
regenerate just enough dummy results to let the rest of the code run, based on the tables and fields mentioned in the query parameters and the PHP code that retrieves them (you won't learn much from a SELECT *, but you can see what fields the PHP code expects to get from it)
once you have understood enough of the DB structure, recreate the tables and let the original code work on them little by little
have the previous designer flogged to death for not having provided a way to recreate the DB programatically
There are currently two answers based on the information you provided.
1) you can't do this
PHP is a typeless language. you could check you sql statements for finding field and table names. but it will not complete. if there is a select * from table, you can't see the fields. so you need to check there php accesses the fields. maybe by name or by index. you could be happy if this is done by name, because you can extract the name of the fields. finally the data types will missing. also missing: where are is an index on, what are primary keys, constrains etc.
2) easy, yes you can!
because your php is using a modern framework with contains a orm. this created the database for you. a meta information are included in the php classes/design.
just check the manual how to recreate the database.

Is this a good blog structure?

Im programming my own simple blog with comments in php and mysql. I have one database, and I have one table for posts, (called posts) and for each post I make I manually create a new table called comments1,comments2,comments3, etc. Each blog post has an id and when I retrieve the comments for a post I use a query like:
SELECT * FROM `comments".$id."`"
When I add user text to a comment I use:
htmlspecialchars(mysql_real_escape_string($_POST['name']));
Is this structure ok or is there a better way I'm missing?
Also, would creating a different database for comments be a better practice than grouping it with the posts database? Does having 2 dbconnect functions in one file slow down performance by a lot? And one last worry: how do i make absolutely sure that my php files aren't served to the user as plaintext, because then they could see db login info and such.
Thanks for the help.
No, this is TERRIBLE structure.
Make one comments table and store all comments in there, along with corresponding post_id.
Add an index on post_id and you'll be able to quickly get all comments for a given post.

Put mysql data into html table with edit buttons

I'm attempting to make a ticket system for out smaller company. I've made a submission form that saves to a mySQL database. I can save the data into the table and pull the date into a html table. I've been searching all day, however, to figure out how to put in a cell that will allow me to click a button, link, or whatever to change that row to completed. I have a field in the database for it, but I don't know how to get that cell into the table or how to get the link to understand which row I'm talking about. I've been searching Google for awhile and only getting how to make a html table with mySQL data or how to INSERT into a mySQL table.
You'll need 3 things: an UPDATE statement, the field in the row you want to update, and some unique way of identifying which row (entry) you want to update.
You'll probably want to create a link something similar to:
<a href='/path/to/update.php?id=myUniqueIdentifier'>complete</a>
and on the php page you'll want to use the $_GET['id'] passed (make sure to use msyql_real_escape_string()) to UPDATE your row and set your completed flag to whatever you want.
Lynda.com has a solid MySQL essentials video that has something very similar to what you are looking for. It would definitely be a good starting point for building your own custom solution. Or I'm sure you could just use their user interface to interact with mysql inside the browser.
I have no affiliation with Lynda.com - I just used their site to learn almost everything i know about programming.

Display latest search results from MySQL with PHP

Google unfortunately didn't seem to have the answers I wanted. I currently own a small search engine website for specific content using PHP GET.
I want to add a latest searches page, meaning to have each search recorded, saved, and then displayed on another page, with the "most searched" at the top, or even the "latest search" at the top.
In short: Store my latest searches in a MySQL database (or anything that'll work), and display them on a page afterwards.
I'm guessing this would best be accomplished with MySQL, and then I'd like to output it in to PHP.
Any help is greatly appreciated.
Recent searches could be abused easily. All I have to do is to go onto your site and search for "your site sucks" or worse and they've essentially defaced your site. I'd really think about adding that feature.
In terms of building the most popular searches and scaling it nicely I'd recommend:
Log queries somewhere. Could be a MySQL db table but a logfile would be more sensible as it's a log.
Run a script/job periodically to extract/group data from the log
Have that periodic script job populate some table with the most popular searches
I like this approach because:
A backend script does all of the hard work - there's no GROUP BY, etc made by user requests
You can introduce filtering or any other logic to the backend script and it doesn't effect user requests
You don't ever need to put big volumes of data into the database
Create a database, create a table (for example recent_searches) and fields such as query (the query searched) and timestamp (unix timestamp that the query was made) said, then for your script your MySQL query will be something like:
SELECT * FROM `recent_searches` ORDER BY `timestamp` DESC LIMIT 0, 5
This should return the 5 most recent searches, with the most recent one appearing first.
Create table (something named like latest_searches) with fields query, searched_count, results_count.
Then after each search (if results_count>0), check, if this search query exists in that table. And update or insert new line into table.
And on some page you can just use data from this table.
It's pretty simple.
Ok, your question is not yet clear. But I'm guessing that you mean you want to READ the latest results first.
To achieve this, follow these steps:
When storing the results use an extra field to hold DATETIME. So your insert query will look like this:
Insert into Table (SearchItem, When) Values ($strSearchItem, Now() )
When retrieving, make sure you include an order by like this:
Select * from Table Order by When Desc
I hope this is what you meant to do :)
You simply store the link and name of the link/search in MySQL and then add a timestamp to record what time sb searched for them. Then you pull them out of the DB ordered by the timestamp and display them on the website with PHP.
Create a table with three rows: search link timestamp.
Then write a PHP script to insert rows when needed (this is done when the user actually searches)
Your main page where you want stuff to be displayed simply gets the data back out and puts them into a link container $nameOfWebsite
It's probably best to use a for/while loop to do step 3
You could additionally add sth like a counter to know what searches are the most popular / this would be another field in MySQL and you just keep updating it (increasing it by one, but limited to the IP)

Categories