ad

Tuesday, 8 March 2016

Port scanner, TCP/IP, Firewall & the Bouncer


Thought writing little something about what port scanners, TCP/IP and Firewall in general.

Read this. Don't skip!

When we talk about port it's necessary we also know what packet is. A pack it a little portion of a file that's transferred from one computer to another, and when this pack is transferred it's given destination and source IP address along with a port number. When a computer on receiving end receives this packet their firewall checks for rules set for that port or the IP. Firewall basically is like an entry point on your computer, everything that goes in or out goes through it.

See every app runs on different port. Must've heard of HTTP and FTP? no? Joined internet today or what?


FTP (File Transfer Protocol) - Port 21
SMTP (Simple Mail Transfer Protocol) - Port 25
Login (Login Host Protocol) - Port 49
HTTP (Hypertext Transfer Protocol) - Port 80 But I still don't understand??!
Right that's expected. Lets get a little more practical now. Your friend's computer wont open any webpage. He brings the computer to you and asks if you can fix. You check if it has internet. You do some other check but still cant figure out the problem.
--

What about checking whether firewall has port 80 opened? as you see, port 80 is used to make HTTP connections. It's possible that firewall is blocking it. Did you guys notice how workplace restricts your access to pornographic websites? Yes, firewalls list of rules are blocking those IP addresses.

So what's firewall again?
It's just a dude with list check-list in hand making sure who can come in, and how can go out. Understood? right! get a snicker!


TCP/IP(that's on scary acronym.. gonna drive me sane!) Don't worry.
Transmission control Protocol(TCP) is a language/protocol that ensures your packets are sent from one computer to another securely. You know when data is broken into packets it's a mess! thanks to TCP it's pasted back together into same order it when it goes to receiving end.

Okay..

This almighty TCP seem to know it all.. how does it know where to send data to? Well its friend Internet Protocol does! It contains the destination and source address and knows where the packet goes.

Alright? what's my IP address then? Click here to find out your IP address http://ipchicken.com/

Hmm.. What about TCP, can I see it too? Nop. But you can make sure it's running. If you send a packet using ping command followed by loopback address(this is virtual address for testing) it should return responses. Meaning TCP/IP is working properly. But you really shouldn't worry much about it because computers already comes with this stack installed.

What's port scanner?
As you seen above, different programs run on different protocol. If someone creates a program that checks for open ports on your computer and finds a vulnerability for that port then you're screwed isn't it. That's why people tell to keep un-used ports closed all the time. Check this PDF to find out common vulnerabilities on ports. http://www.tenable.com/sites/drupal...s/Vulnerability Reporting by Common Ports.pdf



Didn't understand anything? Well atleast you put some effort I guess? It's time you learn "basics of how Internet works".

Goodluck! any questions feel free to ask!

Monday, 7 March 2016

Codeigniter - How to fetch data from database and display

Show Data form Database to user
​
You must know some basics of Codeigniter. Please read these two tutorials:
http://www.webdevtown.com/2016/02/mvc-model-view-controller-seriously.html
http://www.webdevtown.com/2016/02/codeigniter-beginners-series-lesson-2.html

Once you've gone through these two tutorials it's time to learn to retrieve information from database and show it on page. We'll just get all the records off database and show it.

Connect
First make sure to connect to database in '/var/www/application/config/database.php'

Set route
Once you've connected lets set default route to 'show_data'. Write $route['default_controller'] = 'show_data'. So whenever use loads your website default controller will run.

Create controller
Create a file named /var/www/application/application/Show_data.php make sure it starts with capital letter. Lets create a controller class. This class will run when user opens your site main page, if no method name is given in URL index method will be called.


PHP:
class Show_stats extends CI_Controller{
        // This is default method that will run if no method name is given in url.
        public function index(){
        }
}
 

Model
Lets create model which will retrieve data from database. Create file /var/www/application/models/Stats_model.php and create a new model class and load database using constructor so it's loaded as soon as class is instantiated.

PHP:
class Stats_model extends CI_Model{
        // We're loading database connection here.
        function __construct(){
                $this->load->database();
        }
}
 

View
Create new file /var/www/application/views/show_data.php. This will is where we will pass data from that we get from model class.

Retrieve data in model.
We created model class earlier but didn't retrieve any data. Lets do it now. Create new function and retrieve all data from database.
PHP:
class Stats_model extends CI_Model{
        function __construct(){
                $this->load->database();
        }

        public function get_stats(){
                // $this->db is an Object that has get method which accepts table name.
                $query = $this->db->get('site_stats');

                // We're returning all the data using $query->result_array();
                return $query->result_array();
        }
}

Loading model into Controller
We created controller earlier but it doesn't do anything. We need to load model, invoke it's method to get the data and the pass it to views to show it to user.
PHP:
class Show_stats extends CI_Controller{

        function __construct(){
                // Running class CI_Controllers constructor.
                parent::__construct();

                // We load the model class.
                $this->load->model('stats_model');
        }


        public function index(){
                // We invoke get_stats method of stats_model class which retrieves data from database, and put them into an associative array $data[].
                $data['stats'] = $this->stats_model->get_stats();

                // We load view and pass it the $data array where we just put the retrieved values.
                $this->load->view('stats', $data);
        }

}
 

Show data in views

Go back to views and run simple foreach statement to show the data to user.

PHP:
<?PHP foreach($stats as $all_stats): ?>
        <b>
                <?PHP echo $all_stats['title']; ?>
        </b>
<?PHP endforeach; ?>
 
That's it guys! now open your localhost and you should see the data. Make sure to popular database table before you do it.

Friday, 26 February 2016

CodeIgniter Beginners Series - Lesson 2 Controller | Routes and View


Welcome to codeigniter beginners series day 2. Today we'll be installing codeIgniter, go over some very basic codeigniter concepts, we'll try loading some basic pages and make changes to it then learn about routes, passing data and setup a route for our new page aswell.

Lets get started. First go head and install codeIgniter, here's a tutorial I wrote on installing codeIgniter on linux. http://www.webdevtown.com/2016/02/installing-codeigniter-on-linux-ubuntu.html If you're on windows it's still the same steps you'll follow to install. Here's codeIgniter official documentation its installation. http://www.codeigniter.com/user_guide/installation/index.html

With that aside, lets move on to more useful stuff. Open 'localhost' on your browser, make sure apache server is running. You should see a basic welcome page. This is codeigniter's default route. Means anyone who loads localhost will see this page.

Some code!

Lets open our controller folder and see what's inside Welcome.php controller file. I'm assuming you've read MVC if you haven't then you should do so to understand this tutorial. Read it here http://www.webdevtown.com/2016/02/mvc-model-view-controller-seriously.html Alright, we're in Welcome.php file, this a a file that is loaded by default. Whenever someone opens localhost, this file is run. This file contains a class that extends from CI_Controller class, means all CI_Controller's method and properies are available to this class. If we want to create a controller we just give our class a name and extend it form CI_Controller class.

General URL structure is like this. domain/CONTROLLER_CLASS/CONTROLLER_CLASS_METHOD/PARAMETERS. In our case controller class is Welcome, method is index and parameter is welcome by default unless user gives one.

class Welcome extends CI_Controller{
function index($page = 'welcome'){
$this->load->view($page);
}
}



Proceeding, we're $this->load->view('welcome'), when the index method is run, it will run the view method giving it welcome argument, welcome is name of our view page which is inside view folder. If user provided no argument in URL welcomepage would be loaded by default, else whatever page user asked for in argument.


What're routes?

Route is a the route to controller. It means what controller to load depending on what information user provides in URL. As I mentioned about typical URL consists on following division domain/CONTROLLER_CLASS/CONTROLLER_CLASS_METHOD/PARAMETERS. Lets create an test URL. http://localhost/index.php/data/names/jon. Now this url is suppose to return information about user jon. As, jon being parameter, names the method and data the controller class name. Lets say you don't like this long URL you want jon's information when user types http://localhost/user/jon this is where routes will come into play. We'll tell our route file, whenever use types /user/USERNAME in URL, run the data class, username method and give it arguement. Note you can put your data into $data() array and pass it to view like so, first put data in $data array, $data['some_info] = $information; then pass it to view, $this->load->view('page_name', $some_info), and then in view file 'page_name' you can show that variable's value like echo $some_info.

So here's my controller, that takes runs data controller, username method in it and passes it brown argument. Then we pass that brown argument to view to display it. Not that in real sanario we'd also get user info off data base. But since it's just 2nd lesson we'll just pass users' name to view. http://localhost/index.php/data/username/brown.













 Lets back off a little and see how we can just use http://localhost/user/jon and still get this working. Switch to routes.php file scroll down and delete pre existing entries. They're at the very bottom after all the commentary. They look like $route['default_controller'] = 'welcome.php'; Delete that too lets create an entry. Note that codeigniter uses *wildcard symbols or keywords for url matching. For example, (:any) means any URL. jon/brown will mean one or another in URL.. if any of it matches, direct user to specified URL.

Our route will look like this $route['user/(:any)'] = "data/username/$1"; It says, whenever user types user/ANY_USERNAME redirect user to data/username/$1 $1 being the argument's value. When user is directed to that page, our controller takes over and process the information showing us the view page.

This is really important content used in almost all frameworks, being as important as it is it requires quite a bit of practice. Best way to practice is to look at different website URL, and make them user friendly using routes.

In next tutorial we'll be learning about model.

Thursday, 25 February 2016

How to hire a good coder/programmer

Coding as we all are well aware is a complex stressful and time consuming process for any coder out there which is why it's extremely important that both coders and the client work together. Hiring someone and let them do the job is never the route to follow, whether it's a retail, pest control, site administration or anything for that matter requires both parties to work together to develop a good end product.

If you're a coder it's really important that you know exactly what you're capable of(never sell yourself short though) and what you're not. Being greedy, taking client's money and NOT delivering the product on time is nothing but a reputation destroyer, just as good word spreads quick bad word spreads quicker. The important factor that we all can agree upon here is communication, as tiring and time consuming coding is, it as a coder is your responsibility to be the first one to get the conversation started and update the client as often as possible so if there's any issue or change in plans the client wont be as surprised. The client too will think twice before telling you to make major changes.

Here's a few thing to ask the coder before you hire him:
- Portfolio
- If worked on similar project
- Rough deadline
- TOS(Extremely important, discuss what they intend to do if project is not finished on time, how they plan on making changes, if there's an additional fee etc..).
- Logical process coder plans on taking. How to plan on achieving what you require.
- Name
- Phone number
- Address


Here's a few thing to ask the client as coder:
- Name
- Phone number
- Address
- NDA(Non disclosure agreement, if there's any)
- Availability(times they're available for discussion)
- Budget(Make it clear what they pay in advance and what after project completion).

It's also very important that you have clear idea of your own project. Being totally illiterate to technology wont help. You should almost always ask the coder certain somewhat technical questions. They should be able to answer it in a simple way that you understand, If you're having trouble understanding it's either you didn't study about your own project or this coder's not the right person that will update you frequently in non technical manner that you understand and make decisions if necessary.

Pricing is an interesting yes quite simple to guess, all it takes is a word with different coders asking for price and figure out whichever makes most sense to you. Do note that there's hundreds of things that determine the price for example coding style, deadline, experience(even shitty coders but with lots of experience can charge you alot), maintenance and whatnot..

Last thing's just like everything if you plan properly everything will go right, hurrying in any investment is never a good idea. Luck aside, most of the you'll end up losing money and time and give up, not because coder didn't do the job properly but because you chose the wrong service that wasn't really suitable for your project.

CodeIgniter Beginners Series - Starting up

CodeIgniter Beginner Series - Starting up




If you've been developing web application in PHP for a while now and have a good working knowledge of OOP this may be the right time to pick up a framework that makes you life easier. If your goal is to learn then forget about framework and keep focusing on developing stuff from scratch but if you're past that point where you feel tired rewriting code and just want to be super productive and make money then you got to use a framework. Learning curve is everywhere so don't expect it to like writing your 'Hello world!".

This is a Codeigniter tutorial series and in this series I'll from beginner to intermediate tutorials covering almost everything that you need to know. Things that are not so important, I won't even touch. If you're past intermediate projects and feel the need to learn it advanced concepts, you're free to as you'll have solid foundation to understand any new concept or pick any new framework.

Before we proceed it's important that you understand what MVC is because Codeigniter is based on it. You can read about MVC here http://www.webdevtown.com/2016/02/mvc-model-view-controller-seriously.html. Today's task is nothing more than reading and understanding MVC, I won't teach you anything, we won't do any code. It's all MVC, search and read till you understand. Once MVC concept is somewhat clear then come back here tomorrow. I'll be teaching some basic stuff like CodeIgniter installation, loading pages and understand what routes are and how they work.

See you tomorrow! We'll ignite some code!

Monday, 22 February 2016

How to Block Tor Traffic

How to block Tor Traffic

Blocking Tor Ip's is pretty straight forward. I'll give you step by step process to follow to block Tor IP's but before that I'll give you guys a break description of how Tor Network work.

Tor is a network of relays/servers. When you try connecting to a certain website it goes through these replays, your computer knows about the first replay it's connected to, and the website you're connecting to knows about the last relay. These relays are other people's computers, they install the software and become relay.

Exit relay is the last relay that your data goes through before the request is sent to the site you're trying to connect. ALL THESE EXIT RELAY IPs ARE PUBLISHED.

To detect Tor IP all you need to do is check whether the IP connecting to your server matched any IP in that Tor exit relay list. If it does match, means it's coming off a Tor exist relay.

For example, here's a TOR relay https://globe.torproject.org/#/relay/1C90D3AEADFF3BCD079810632C8B85637924A58E and its IP address is 62.210.82.44 which mean if you query Relay database with that IP you should get positive response back with some data. If it returns negative response, means it's not a Tor IP address.


Lets get practical. Visit this website and put a random IP address. You can put your too and press enter. https://check.torproject.org/cgi-bin/TorBulkExitList.py It will show the list of Exit nodes that are allowed to connect to your computer/server. Generally you'd send your server's IP address to this website with port and it will show you the list of exit relays that can cannot to your server. Then just iterate through these IPs and if the suspicious IP/IP address trying to connect to your server exists in that list. It means it's a Tor computer address.

Here's an example.
https://check.torproject.org/cgi-bi/TorBulkExitList.py?ip=IP_ADDRESS&port=PORT_NUMBER

Example with info:
https://check.torproject.org/cgi-bi/TorBulkExitList.py?ip=216.239.34.10&port=80


That said and explained do make sure that you aren't blocking the legit user, someone using Tor network doesn't automatically make them an abuser, and it's important to keep in mind that someone could be using their PC to connect to website in general way but also use the same IP for Tor, meaning if you query their IP in Tor relay IP's it will say it's a Tor IP even though user is visiting your site without going through Tor relays.

Wednesday, 17 February 2016

CodeIgniter Documentation Example Code Error | Explained and Fixed

CodeIgniter Documentation sample code error. 404 Page Not Found The page you requested was not found.


First of all go have a look at CodeIgniter Static pages documentation.
http://www.codeigniter.com/user_guide/tutorial/static_pages.html

Here's their Example of Controller class Pages:
class Pages extends CI_Controller {
   public function view($page = 'home') 
      {
          if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php'))
          {
             // Whoops, we don't have a page for that!
            show_404();
         }

       $data['title'] = ucfirst($page); // Capitalize the first letter

       $this->load->view('templates/header', $data);
       $this->load->view('pages/'.$page, $data);
       $this->load->view('templates/footer', $data);
   }
}

Now as you can see the 'APPPATH' CONSTANT on line 3 is path to the 'application' folder and it's defined in our main index.php file.

If you go and create template folder, put files in, same with pages and whatnot and run it, it wont run. It'll instead invoke show_404() function saying page doesn't exist. Specifically tell us, "APPPATH.'/views/pages/'.$page.'.php'" doesn't exist. It's really a pain because it does indeed exist even after setting up everything correctly and granting full perms.

So after searching in Google for about 4-5 hours and trying to resolve it myself I decided to check the exact value of that 'APPPATH' CONSTANT. That's how it's defined: "define('APPPATH', $application_folder.DIRECTORY_SEPARATOR);".

if ( ! is_dir(BASEPATH.$application_folder.DIRECTORY_SEPARATOR))
{
    header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
    echo 'Your application folder path does not appear to b$
    exit(3); // EXIT_CONFIG
}


It seem perfectly fine at first glance but what's that 'DIRECTORY_SEPERATOR' CONSTANT doing there? well I guess it's just adding a directory separator '/'. Perfectly fine, right? Now if you've been following their documentation they've a Directory separator '/' before 'views' aswell, '! file_exists(APPPATH.'/views/pages/'.$page.'.php'', which means APPPATH will add one directory separator, and if you follow the documentation they've added another directory separator in total two separators, making file_exist function return false and thence error message!!

This is what we're basically asking file_exists() function to check. Does /var/www/application/.'/views/pages/'.$page.'.php'. exist? It'll always return false due to 2 slashes! To fix the issue you need to remove the clash before views.