Never modify WordPress core files directly. You should read up on WP architecture, which is event based. You want to find a correct action and hook into it.
You need wp_login and wp_logout action hooks.
<?php
function your_function() {
// create cookie
}
add_action('wp_login', 'your_function');
function your_function() {
// delete cookie
}
add_action('wp_logout', 'your_function');
?>
These can go in functions.php
, or be encapsulated as a plugin.