Sunday, 8 June 2014

SQL injection

-->

-->
SQL injection is a code injection technique, used to attack data driven applications, in which malicious SQL statements are inserted into an entry field for execution (e.g. to dump the database contents to the attacker).SQL injection must exploit a security vulnerability in an application's software, for example, when user input is either incorrectly filtered for string literal escape characters embedded in SQL statements or user input is not strongly typed and unexpectedly executed. SQL injection is mostly known as an attack vector for websites but can be used to attack any type of SQL database.
SQL injection,
-->
Incorrectly filtered escape characters
This form of SQL injection occurs when user input is not filtered for escape characters and is then passed into a SQL statement. This results in the potential manipulation of the statements performed on the database by the end-user of the application.
The following line of code illustrates this vulnerability:
statement = "SELECT * FROM users WHERE name = '" + userName + "';"
This SQL code is designed to pull up the records of the specified username from its table of users. However, if the "userName" variable is crafted in a specific way by a malicious user, the SQL statement may do more than the code author intended. For example, setting the "userName" variable as:
' or '1'='1
or using comments to even block the rest of the query (there are three types of SQL comments):
' or '1'='1' -- '
' or '1'='1' ({ '
' or '1'='1' /* '
renders one of the following SQL statements by the parent language:
SELECT * FROM users WHERE name = '' OR '1'='1';
SELECT * FROM users WHERE name = '' OR '1'='1' -- ';
If this code were to be used in an authentication procedure then this example could be used to force the selection of a valid username because the evaluation of '1'='1' is always true.
The following value of "userName" in the statement below would cause the deletion of the "users" table as well as the selection of all data from the "userinfo" table (in essence revealing the information of every user), using an API that allows multiple statements:
a';DROP TABLE users; SELECT * FROM userinfo WHERE 't' = 't
This input renders the final SQL statement as follows and specified:
SELECT * FROM users WHERE name = 'a';DROP TABLE users; SELECT * FROM userinfo WHERE 't' = 't';
While most SQL server implementations allow multiple statements to be executed with one call in this way, some SQL APIs such as PHP's mysql_query(); function do not allow this for security reasons. This prevents attackers from injecting entirely separate queries, but doesn't stop them from modifying queries.
Incorrect type handling
This form of SQL injection occurs when a user-supplied field is not strongly typed or is not checked for type constraints. This could take place when a numeric field is to be used in a SQL statement, but the programmer makes no checks to validate that the user supplied input is numeric. For example:
statement := "SELECT * FROM userinfo WHERE id = " + a_variable + ";"
It is clear from this statement that the author intended a_variable to be a number correlating to the "id" field. However, if it is in fact a string then the end-user may manipulate the statement as they choose, thereby bypassing the need for escape characters. For example, setting a_variable to
1;DROP TABLE users
will drop (delete) the "users" table from the database, since the SQL becomes:
SELECT * FROM userinfo WHERE id=1;DROP TABLE users;
Blind SQL injection
Blind SQL Injection is used when a web application is vulnerable to an SQL injection but the results of the injection are not visible to the attacker. The page with the vulnerability may not be one that displays data but will display differently depending on the results of a logical statement injected into the legitimate SQL statement called for that page. This type of attack can become time-intensive because a new statement must be crafted for each bit recovered. There are several tools that can automate these attacks once the location of the vulnerability and the target information has been established.
Conditional responses
One type of blind SQL injection forces the database to evaluate a logical statement on an ordinary application screen. As an example, a book review website uses a query string to determine which book review to display. So the URL http://books.example.com/showReview.php?ID=5 would cause the server to run the query
SELECT * FROM bookreviews WHERE ID = '5';
from which it would populate the review page with data from the review with ID 5, stored in the table bookreviews. The query happens completely on the server; the user does not know the names of the database, table, or fields, nor does the user know the query string. The user only sees that the above URL returns a book review. A hacker can load the URLs http://books.example.com/showReview.php?ID=5 AND 1=1 and http://books.example.com/showReview.php?ID=5 AND 1=2, which may result in queries
SELECT * FROM bookreviews WHERE ID = '5' AND '1'='1';
SELECT * FROM bookreviews WHERE ID = '5' AND '1'='2';
respectively. If the original review loads with the "1=1" URL and a blank or error page is returned from the "1=2" URL, the site is likely vulnerable to a SQL injection attack. The hacker may proceed with this query string designed to reveal the version number of MySQL running on the server: http://books.example.com/showReview.php?ID=5 AND substring(@@version,1,1)=4, which would show the book review on a server running MySQL 4 and a blank or error page otherwise. The hacker can continue to use code within query strings to glean more information from the server until another avenue of attack is discovered or his or her goals are achieved.
Mitigation
Parameterized statements
With most development platforms, parameterized statements that work with parameters can be used (sometimes called placeholders or bind variables) instead of embedding user input in the statement. A placeholder can only store a value of the given type and not an arbitrary SQL fragment. Hence the SQL injection would simply be treated as a strange (and probably invalid) parameter value.
In many cases, the SQL statement is fixed, and each parameter is a scalar, not a table. The user input is then assigned (bound) to a parameter.
Enforcement at the coding level
Using object-relational mapping libraries avoids the need to write SQL code. The ORM library in effect will generate parameterized SQL statements from object-oriented code.
Escaping
A straightforward, though error-prone, way to prevent injections is to escape characters that have a special meaning in SQL. The manual for an SQL DBMS explains which characters have a special meaning, which allows creating a comprehensive blacklist of characters that need translation. For instance, every occurrence of a single quote (') in a parameter must be replaced by two single quotes ('') to form a valid SQL string literal. For example, in PHP it is usual to escape parameters using the function mysql_real_escape_string(); before sending the SQL query:
$query = sprintf("SELECT * FROM `Users` WHERE UserName='%s' AND Password='%s'",
                  mysql_real_escape_string($Username),
                  mysql_real_escape_string($Password));
mysql_query($query);
This function, i.e. mysql_real_escape_string(), calls MySQL's library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a. This function must always (with few exceptions) be used to make data safe before sending a query to MySQL. There are other functions for many database types in PHP such as pg_escape_string() for PostgreSQL. There is, however, one function that works for escaping characters, and is used especially for querying on databases that do not have escaping functions in PHP. This function is: addslashes(string $str ). It returns a string with backslashes before characters that need to be quoted in database queries, etc. These characters are single quote ('), double quote ("), backslash (\) and NUL (the NULL byte). Routinely passing escaped strings to SQL is error prone because it is easy to forget to escape a given string. Creating a transparent layer to secure the input can reduce this error-proneness, if not entirely eliminate it.
Pattern check
Integer, float or boolean parameters can be checked if their value is valid representation for the given type. Strings that must follow some strict pattern (date, UUID, alphanumeric only, etc.) can be checked if they match this pattern.
Database permissions
Limiting the permissions on the database logon used by the web application to only what is needed may help reduce the effectiveness of any SQL injection attacks that exploit any bugs in the web application.
For example on SQL server, a database logon could be restricted from selecting on some of the system tables which would limit exploits that try to insert JavaScript into all the text columns in the database.
deny SELECT ON sys.sysobjects TO webdatabaselogon;
deny SELECT ON sys.objects TO webdatabaselogon;
deny SELECT ON sys.TABLES TO webdatabaselogon;
deny SELECT ON sys.views TO webdatabaselogon;
 

Friday, 9 May 2014

HOW TO CREATE AND SUBMIT A SITEMAP ON BLOGGER Google & Bing

-->


WHAT IS A SITEMAP
A sitemap is simply a directory of all the pages existing on your site, like a table of contents showing the structure of your blog. Sitemaps help search engines crawl your site and index it properly. Search engines will crawl your site if you have a sitemap or not, but this makes the process easier and quicker for them.
Bloggers default XML sitemap only shows recent blog posts – which is about 26 posts. We want search engine spiders to know your blogs structure so that they can easily understand and index it.
HOW TO CREATE A SITEMAP FOR BLOGGER
Creating a site map on Blogger is easy, simply add your blog name to the link below.
Blogger – Default URL http://blogname.blogspot.com/atom.xml?redirect=false&start-index=1&max-results=500
Blogger – Custom URL http://blogname.com/atom.xml?redirect=false&start-index=1&max-results=500
A single sitemap file should not be exceed 50MB which is why we limit it to max results 500. You may need to edit this depending on how big your feed is. Now we’ve to tell search engines that this is your site map. To do this, we add it to your robots.txt file and submit it to Google Webmaster Tools.
ADD BLOGGER SITEMAP TO ROBOTS.TXT FILE
Go to Blogger > Dashboard > Settings > Search Preferences and click edit the custom robots.txt file. Click enable and paste the following adding your blogname.
User-agent: *
Disallow: /search
Allow: /
Sitemap: http://blogname.blogspot.com/atom.xml?redirect=false&start-index=1&max-results=500
The above section means that search engines can index your entire site (Allow: /) apart from your blogs search results (Disallow: /search) which is good because that would be considered duplicate pages.
ADDING SITE MAP TO GOOGLE WEBMASTER TOOLS
Now login to Google Webmaster Tools and select your blog. On the left click Crawl > Sitemaps > Add/Test Sitemap. You’ll see your blog name and a text input box, paste atom.xml?redirect=false&start-index=1&max-results=500, test for errors and submit.
submit-sitemap-to-google
Google will now start to crawl and index your site. You can check what pages have been indexed on Googles Webmaster Tools or by going to Google Search and typing “site:www.BLOG-URL.blogspot.com”, “site:BLOG-URL.blogspot.com”, “site:www.BLOG-URL.com” or “site:BLOG-URL.com”. To submit your sitemap to Bing, login to Bings Webmaster Tools, add your URL, verify ownership and then submit your site map. It may take a few days for your site to be indexed.
HAVE MORE THAN 500 POSTS?
If you have more than 500 posts on your blog, you simply submit another sitemap starting at post number 501 for the next 500 posts like so /atom.xml?redirect=false&start-index=501&max-results=500 and continue this depending on the amount of posts you have. So if I had over 1000 posts, my robots.text file would look like this and I would submit the three separate sitemaps to Googles Webmaster Tool.
User-agent: *
Disallow: /search
Allow: /
Sitemap: http://blogname.blogspot.com/atom.xml?redirect=false&start-index=1&max-results=500
Sitemap:http://blogname.blogspot.com/atom.xml?redirect=false&start-index=501&max-results=500
Sitemap:http://blogname.blogspot.com/atom.xml?redirect=false&start-index=1001&max-results=500

Tuesday, 6 May 2014

Paypal Integration With Asp.Net using Website Payment Method

Paypal Integration With Asp.Net using Website Payment Method

-->





Nowadays PayPal is the most popular payment gateway worldwide because it is totally free to integrate and PayPal does not charge anything for opening an account, you will pay PayPal when you get paid. And the amount is also lower than other payment gateways.



How to integrate with PayPal in Asp.net?, is common question ask by different developers around the world. When you visit official website of PayPal for help, no doubt they (PayPal) are trying to help visitors to learn techniques and  method to integrate it with their application but for newly person who haven't experience with integration unable to understand the way of success.

There are four methods to integrate PayPal like:
·                     Website Payments Standard (HTML)
·                     Postpayment Processing
o                  AutoReturn
o                  Payment Data Transfer (PDT)
o                  Instant Payment Notification (IPN)
·                     PayPal API
o                  Express Checkout
o                  Direct Payment (Website Payments Pro)
·                     Payflow Gateway
But newly person's first choice is "PayPal API" for integration which is wrong. It seems quite easy with other version but after integration when you test  your application using Sandbox test account, it show you version errors and the most common error is display like "Total Order is missing", in fact you already pass total order amount in session as per of description. 

If you are new to PayPal, first learn all of the options that you have with the Website Payments Standard. Then, if you need to add some basic post-payment processing, use Auto-Return or PDT. If not solve your issue then IPN is a more robust option you have.


Next level would involve the PayPal API and implementing the Express Checkout, which is the most flexible PayPal integration solution. And finally, if you long for the ability to directly process credit cards on your website, you’ll pay a monthly fee to PayPal and implement Direct Payment (called Website Payments Pro).


The last item from classification is Payflow Gateway, a different beast. It is a solution aimed specifically at those businesses that have/want an Internet Merchant Account (IMA) and just need the payment gateway. 


Lets start PayPal integration with Asp.net application using Website Payment Method. First follow to set up a new Test account as there are two reasons to test your application before live your application. 
·                     You don’t want to test and play with real money
·                     You want to have access to different types of PayPal accounts
o                  Personal Account  Most people have these; just an account that allows you to use PayPal when paying for stuff on-line. Theoretically, you can use a Personal account to accept money; just know that you’ll be severely constrained – there is a $500 receiving limit per month, and you are only able to accept one time payments using the Website Payments Standard (HTML). The big advantage of a Personal account is that you don’t need to pay any transaction fee when receiving money. If you receive more than $500 in one month, you’ll be prompted to either upgrade to a Premier/Business account or reject the payment.
o                  Premier Account – Step up from a personal account; for anyone who wants to run a personal on-line business. This type of account has all of the integration options. However, most people skip directly from Personal to Business account as Premier account has the same transaction fees (in most cases, 2.9% + $0.30 per transaction) while lacking reporting, multi-user access, and other advanced merchant services of the Business account.
o                  Business Account – It has all of the features of the Premier account plus a few more (ability to operate under your business’s name is one of them). If you are developing a website that needs to accept payments in 99% of situations, you’ll go with this type of account.
To start, visit the PayPal Sandbox and sign-up for a new account. The process is straightforward, and most developers should have no trouble finishing it. However, here are the pictures that will help you navigate through the process:
·                     Go-to Here 





·                     Signing up for a Sandbox account






·                     After setting up test account, let's start website payment method (HTML)
Parameter
Description
cmd
The parameter is obligatory. It must have the _xclick value for an unencrypted request.
business
The parameter is obligatory and represents the seller's e-mail.
item_number
This parameter is an item identifier. This value will not be shown to the user; however, it will be passed to your script at the time of transaction confirmation. If you plan to use PayPal to pay for goods in a cart, then you can pass the cart's identifier in this parameter.
item_name
This is a name of the item that will be shown to the user.
no_shipping
Prompt customer for shipping address.
Default or 0: customer is prompted to include a shipping address.
1: customer is not asked for a shipping address.
2: customer must provide a shipping address.
return
This is the URL where user will be redirected after the payment is successfully performed. If this parameter is not passed, the buyer remains on the PayPal site.
rm
This parameter determines the way information about successful transaction will be passed to the script that is specified in the return parameter. "1" means that no parameters will be passed. "2" means that the POST method will be used. "0" means that the GET method will be used. The parameter is "0" by default.
cancel_return
This is the URL where the user will be redirected when he cancels the payment. If the parameter is not passed, the buyer remains on the PayPal site.
notify_url
This is the URL where PayPal will pass information about the transaction (IPN). If the parameter is not passed, the value from the account settings will be used. If this value is not defined in the account settings, then IPN will not be used.
custom
This field does not take part in the shopping process, it will be simply passed to IPN script at the time of transaction confirmation.
invoice
This parameter is used to pass the invoice number. The parameter is not obligatory, but being passed it must be unique for every transaction.
amount
This parameter represents an amount of payment. If the parameter is not passed, the user will be allowed to enter the amount (this is used for donations).
currency_code
This parameter represents a currency code. Possible values are "USD","EUR","GBP","YEN","CAD" etc. It is "USD" by default.

There are two type of integration techniques under Website Payment Method.
·                     Passing the Aggregate Cart Amount to PayPal
·                     Passing Individual Items to PayPal
A. Passing the Aggregate Cart Amount to PayPal
You may aggregate your entire shopping cart and pass the total amount into PayPal's Buy Now Button code (that is, you will need to post a single name for the entire cart and the total price of the cart's contents as though it were a purchase of a single item).

One drawback of this method is that your buyers will not be able to see the individual items appearing in their carts. In addition, you cannot change PayPal variable names, nor can you add your own variable names.

The code for your PayPal post requires the following 4 hidden variables and an image as the form submit:

Required Variables
Name
Value
business
Email address on your PayPal account
item_name
Name of the item (or a name for the shopping cart)
currency_code
Defines the currency in which the monetary variables (amount, shipping, shipping2, handling, tax) are denoted. Possible values are "USD", "EUR", "GBP", "CAD", "JPY".
amount
Price of the item (the total price of all items in the shopping cart)
image
The image for the button your buyer will press to initiate the PayPal payment process. You can substitute your own image by replacing the src with the URL of your image

This means that the minimum required code for your post to PayPal will look like this:











PayPal offers additional variables to customize your form post. All of the available variables are listed below (variable names must be in lower case):

Available Variables
Name
Value
business
Email address on your PayPal account
quantity
Number of items. This will multiply the amount if greater than one
item_name
Name of the item (or a name for the shopping cart). Must be alpha-numeric, with a 127character limit
item_number
Optional pass-through variable for you to track payments. Must be alpha-numeric, with a 127 character limit
amount
Price of the item (the total price of all items in the shopping cart)
shipping
The cost of shipping the item
shipping2
The cost of shipping each additional item
handling
The cost of handling
tax
Transaction-based tax value. If present, the value passed here will override any profile tax settings you may have (regardless of the buyer's location).
no_shipping
Shipping address. If set to "1," your customer will not be asked for a shipping address. This is optional; if omitted or set to "0," your customer will be prompted to include a shipping address
cn
Optional label that will appear above the note field (maximum 40 characters)
no_note
Including a note with payment. If set to "1," your customer will not be prompted to include a note. This is optional; if omitted or set to "0," your customer will be prompted to include a note.
on0
First option field name. 64 character limit
os0
First set of option value(s). 200 character limit. "on0" must be defined for "os0" to be recognized.
on1
Second option field name. 64 character limit
os1
Second set of option value(s). 200 character limit. "on1" must be defined for "os1" to be recognized.
custom
Optional pass-through variable that will never be presented to your customer. Can be used to track inventory
invoice
Optional pass-through variable that will never be presented to your customer. Can be used to track invoice numbers
notify_url
Only used with IPN. An internet URL where IPN form posts will be sent
return
An internet URL where your customer will be returned after completing payment
cancel_return
An internet URL where your customer will be returned after cancelling payment
image_url
The internet URL of the 150 X 50 pixel image you would like to use as your logo
cs
Sets the background color of your payment pages. If set to "1," the background color will be black. This is optional; if omitted or set to "0," the background color will be white

PayPal allows you to post extended variables if you change this "cmd" input:


To



By making the above change to the "cmd" input, you can also use the variables below:

Extended Variables
Name
Value
email
Customer's email address
first_name
Customer's first name. Must be alpha-numeric, with a 32 character limit
last_name
Customer's last name. Must be alpha-numeric, with a 64 character limit
address1
First line of customer's address. Must be alpha-numeric, with a 100 character limit
address2
Second line of customer's address. Must be alpha-numeric, with a 100 character limit
city
City of customer's address. Must be alpha-numeric, with a 100 character limit
state
State of customer's address. Must be official 2 letter abbreviation
zip
Zip code of customer's address
night_phone_a
Area code of customer's night telephone number
night_phone_b
First three digits of customer's night telephone number
day_phone_a
Area code of customer's daytime telephone number
day_phone_b
First three digits of customer's daytime telephone number

Note: To specify shipping & handling amounts that differ from the default shipping amounts set in your Profile, please go to your Profile, edit your Shipping Calculations, and click the "allow transaction-based shipping override" checkbox.
B. Passing Individual Items to PayPal
If your custom or third party shopping cart can be configured to pass individual items to PayPal, information about the items will be included in the buyers' and sellers' History logs and notifications. To include information about the items, you will post HTML form elements to a new version of PayPal's Shopping Cart flow. This process is much like the one described in Section #1 "Passing Aggregate Cart Amount to PayPal" with the following exceptions:
·                     Set the "cmd" variable to "_cart" 
o                   
o                  (Replace above required HTML line with below line)
o                 
·                     Add a new variable called "upload"
o                 
·                     Define item details
For each of the following item-specific parameters, define a new set of values that correspond to each item that was purchased via your custom cart. Append "_x" to the variable name, where x is the item number, starting with 1 and increasing by one for each item that is added.


Name
Value
item_name_x
(Required for item #x) Name of item #x in the cart. Must be alpha-numeric, with a 127 character limit
item_number_x
Optional pass-through variable associated with item #x in the cart. Must be alpha-numeric, with a 127 character limit
amount_x
(Required for item #x) Price of the item #x
shipping_x
The cost of shipping the first piece (quantity of 1) of item #x
shipping2_x
The cost of shipping each additional piece (quantity of 2 or above) of item #x
handling_x
The cost of handling for item #x
on0_x
First option field name for item #x. 64 character limit
os0_x
First set of option value(s) for item #x. 200 character limit. "on0_x" must be defined in order for "os0_x" to be recognized.
on1_x
Second option field name for item #x. 64 character limit
os1_x
Second set of option value(s) for item #x. 200 character limit. "on1_x" must be defined in order for "os1_x" to be recognized.