Tuesday, March 19, 2013

Programming Basics #3

Classes

Getting a hang of classes, like a redditor...

1:  #include <iostream>  
2:  #include <string>  
3:    
4:  using namespace std;  
5:  int pauser;  
6:  #define pause cin >> pauser;  
7:    
8:  class cat  
9:  {  
10:  public:  
11:       string color;  
12:       string name;  
13:       float weight;  
14:       int happylevel;  
15:       void scare();  
16:       void play();  
17:       cat();  
18:       cat(string, string);  
19:  };  
20:    
21:  cat::cat()  
22:  {  
23:       color="unknown";  
24:       name="unknown";  
25:       happylevel=0;  
26:  }  
27:    
28:  cat::cat(string colorIn, string nameIn)  
29:  {  
30:       color=colorIn;  
31:       name=nameIn;  
32:       happylevel=0;  
33:  }  
34:    
35:  void cat::scare()  
36:  {  
37:       cout << name << " is terrified! Happylevel - 1!" << endl;  
38:       happylevel-=1;  
39:  }  
40:    
41:  void cat::play()  
42:  {  
43:       cout << name << " chases the laser! Happylevel + 1!" << endl;  
44:       happylevel+=1;  
45:  }  
46:    
47:  int main()  
48:  {  
49:       int choiceCat, choiceAction;  
50:       cat goosey("white", "Goosey");  
51:       cat lucy;  
52:       lucy.color="black";  
53:       lucy.name="Lucy";  
54:       lucy.weight=10.0;  
55:       goosey.weight=5.0;  
56:    
57:       while(choiceCat!=3)  
58:       {  
59:            cout << "Select cat:" << endl;  
60:            cout << "1. Lucy   (Happy Level: " << lucy.happylevel << ")" << endl;  
61:            cout << "2. Goosey  (Happy Level: " << goosey.happylevel << ")" << endl;  
62:            cout << "3. Exit" << endl;  
63:            cin >> choiceCat;  
64:            if (choiceCat==3) break;  
65:            else  
66:            {  
67:                 cout << "\nSelect action:" << endl;  
68:                 cout << "1. Play" << endl;  
69:                 cout << "2. Scare" << endl;  
70:                 cout << "3. Exit" << endl;  
71:                 cin >> choiceAction;  
72:                 if (choiceAction==3) break;  
73:                 if (choiceCat==1)  
74:                 {  
75:                      if (choiceAction==1) lucy.play();  
76:                      if (choiceAction==2) lucy.scare();  
77:                 }  
78:                 if (choiceCat==2)  
79:                 {  
80:                      if (choiceAction==1) goosey.play();  
81:                      if (choiceAction==2) goosey.scare();  
82:                 }  
83:            }  
84:       }  
85:            return 0;  
86:  }  

Output:
 Select cat:  
 1. Lucy   (Happy Level: 0)  
 2. Goosey  (Happy Level: 0)  
 3. Exit  
 1  
   
 Select action:  
 1. Play  
 2. Scare  
 3. Exit  
 1  
 Lucy chases the laser! Happylevel + 1!  
 Select cat:  
 1. Lucy   (Happy Level: 1)  
 2. Goosey  (Happy Level: 0)  
 3. Exit  
 2  
   
 Select action:  
 1. Play  
 2. Scare  
 3. Exit  
 2  
 Goosey is terrified! Happylevel - 1!  
 Select cat:  
 1. Lucy   (Happy Level: 1)  
 2. Goosey  (Happy Level: -1)  
 3. Exit  

Sunday, March 17, 2013

Removing the Fog of Hacking #1

Resetting Windows Passwords via USB/CD

Most of my security brethren know this old trick.  If you forgot your password (or if you're being a shady fella), there's a tool you can download and put on a bootable CD/USB to reset any password on a Windows box.

http://pogostick.net/~pnh/ntpasswd/

I personally have a USB stick shelved just for this purpose, in case I'm traveling/going to a friends house/etc.  You just stick it in a computer, restart it, and boot from it.  Follow the prompts and you're good to go!

Programming Basics #2

Dynamic Memory Allocation

Very basic.  Good examples of 'for' loops and 'if/else if/else' statements:

1:  #include <iostream>  
2:    
3:  using namespace std;  
4:    
5:  int pause;  
6:    
7:  int main()  
8:  {  
9:       int num, total=0;  
10:       int *pDynam;  
11:    
12:       cout << "How many numbers to add: ";  
13:       cin >> num;  
14:       pDynam = new int[num];  
15:    
16:       cout << "Enter numbers below." << endl;  
17:    
18:       for(int i=0;i<num;i++)  
19:       {  
20:            cout << "Number " << i+1 << ": ";  
21:            cin >> pDynam[i];  
22:       }  
23:    
24:       cout << "That's it! You entered ";  
25:    
26:       for(int i=0;i<num;i++)  
27:       {  
28:            if (i == (num-1))  
29:                 cout << "and " << pDynam[i] << ".";  
30:            else if (i == (num-2))  
31:                 cout << pDynam[i] << " ";  
32:            else  
33:                 cout << pDynam[i] << ", ";  
34:       }  
35:    
36:       cout << endl << "Adding..." << endl;  
37:    
38:       for(int i=0;i<num;i++)  
39:       {  
40:            total = total + pDynam[i];  
41:       }  
42:    
43:       cout << "Total is: " << total << "." << endl;  
44:    
45:       cin >> pause;  
46:       return 0;  
47:  }  

The only new technique I used here was line 14, where the array size is dynamic (set at run-time instead of compile time) by the user.

Output:
 How many numbers to add: 5  
 Enter numbers below.  
 Number 1: 5  
 Number 2: 4  
 Number 3: 3  
 Number 4: 2  
 Number 5: 1  
 That's it! You entered 5, 4, 3, 2 and 1.  
 Adding...  
 Total is: 15.  

Programming Basics #1

Pointer Tutorial

Mostly saving this off for later use, in case someone asks me how pointers work, which seems to happen pretty frequently...

Source:
1:  #include <iostream>  
2:    
3:  using namespace std;  
4:    
5:  int pause;  
6:    
7:  int main()  
8:  {  
9:        int x=1,y=2;  
10:       int *p1, *p2;     // pointers, just hold a memory address  
11:       p1=&x;            // p1 = address of x  
12:       p2=&y;            // p2 = address of y  
13:       int *pholder;     // need this to swap them later  
14:       cout << "p1 = " << p1 << endl;   
15:       cout << "p2 = " << p2 << endl;  
16:       cout << "*p1 = " << *p1 << endl;  
17:       cout << "*p2 = " << *p2 << endl << endl;  
18:       cout << "Switching..." << endl << endl;  
19:       pholder=p1;          // save p1 (which is a memory address)  
20:       p1=p2;               // p1 (addr. of x) = p2 (addr. of y)  
21:       p2=pholder;          // p2 (addr. of y) = p1 (addr. of x)  
22:       cout << "p1 = " << p1 << endl;  
23:       cout << "p2 = " << p2 << endl;  
24:       cout << "*p1 = " << *p1 << endl;  
25:       cout << "*p2 = " << *p2 << endl;  
26:    
27:       cin >> pause;  
28:       return 0;  
29:  } 

Output:
 p1 = 003FFD9C  
 p2 = 003FFD90  
 *p1 = 1  
 *p2 = 2  
   
 Switching...  
   
 p1 = 003FFD90  
 p2 = 003FFD9C  
 *p1 = 2  
 *p2 = 1  

Blog Beginnings

I've wanted to do this for a while now.  I love learning, and instead of spamming my Facebook page or Reddit with mildly interesting things that few people care about, I'm going to keep it local. 

Most of it is going to be programming/security related tips/tricks that I find cool or useful.  I'm in no way an expert programmer... I can usually get done what needs to be done, but it's nothing super elegant.

I'm very open to suggestions/tips/tricks/solutions on how to do something better, faster, or easier, so if you have any, throw 'em at me!