Concretamente, puede listarlos junto con el pid de cada uno, cerrarlos por su pid/nombre, obtener información, y obtener el pid a partir del nombre.
Código:
/*
=====================================================================================
|| ## ####### ## ## ## ####### ####### ## ## ## ||
|| ## ## ## ## ## ######## ## ## ######## ## ## ||
|| ## ##### ######## ### ## ####### ####### ## #### ||
|| ## ## ## ## ## ## ## ### ## ## ## ||
|| ###### ####### ## ## ####### ####### ### ## ## ## ||
======================================================================================
-------------------------------------------------------------------------------------
|| Name: Process Administration Tool 1.0 ||
|| Description: Programa para listar procesos, matar procesos por id/nombre, ||
|| obtener información sobre un proceso determinado, y obtener el PID ||
|| a través del nombre del Módulo. ||
|| Autor: Lympex ||
|| Date: 11/08/2005 ||
|| HomePage: http://l-bytes.tk ||
|| Contact: lympex[at]gmail[dot]com ||
-------------------------------------------------------------------------------------
D:\>Process.Administration.Tool.1.0.exe
Process Administrator Tool 1.0 - Coded by Lympex
Contact: lympex[at]gmail[dot]com & http://l-bytes.tk
----------------------------------------------------
[!] Argumentos incorrectos
[+] Opciones:
+ Listar procesos: -l
+ Cerrar proceso mediante PID: -kpid pid
+ Cerrar proceso mediante nombre: -kname nombre
+ Informacion sobre un proceso: -i pid
+ Buscar PID: -pid nombre
D:\>
*/
#include <stdio.h>//I/O
#include <windows.h>//Para usar con Tlhelp32.h
#include <Tlhelp32.h>//CreateToolhelp32Snapshot, Process32First, Process32Next,PROCESSENTRY32
#include <string.h>//strlen
/*CIERRA UN PROCESO MEDIANTE SU PID*/
int CierraPID(DWORD pid);
/*devuelde la descripción del error, a partir de su código*/
char *MensajeError(DWORD error_num);
void main(int argc, char *argv[])
{
/*LISTA LOS PROCESOS ACTUALES*/
int ListarProcesos();
/*BUSCA EL PID DE UN PROCESO*/
int BuscaPid(char *nombre);
/*DA INFORMACION SOBRE UN PROCESO*/
int InfoProc(DWORD pid);
//para cojer la salida de algunas funciones
int datos;
printf("\n Process Administrator Tool 1.0 - Coded by Lympex");
printf("\nContact: lympex[at]gmail[dot]com & http://l-bytes.tk");
printf("\n----------------------------------------------------\n");
if(argc<=1)
{
printf("\n[!] Argumentos incorrectos");
printf("\n[+] Opciones:");
printf("\n + Listar procesos: -l");
printf("\n + Cerrar proceso mediante PID: -kpid pid");
printf("\n + Cerrar proceso mediante nombre: -kname nombre");
printf("\n + Informacion sobre un proceso: -i pid");
printf("\n + Buscar PID: -pid nombre\n");
return;
}
//LISTAMOS LOS PROCESOS
if(!strcmp(argv[1],"-l"))
{
printf("\nPROCESO PID");printf("\n---------------------------------\n");ListarProcesos();
printf("\n");
return;
}
//CERRAR PROCESO MEDIANTE PID
if(!strcmp(argv[1],"-kpid"))
{
if(argc<2)
{
printf("\n[!] Debes indicar un PID\n");
}else{
CierraPID(atoi(argv[2]));
}
return;
}
//CERRAR PROCESO SEGÚN EL NOMBRE
if(!strcmp(argv[1],"-kname"))
{
if(argc<2)
{
printf("\n[!] Debes indicar un PID\n");
}else{
datos=BuscaPid(argv[2]);
if(datos!=-1)
{
CierraPID(datos);
}else{
printf("\n[!] No existe el proceso \x22%s\x22\n",argv[2]);
}
}
return;
}
//DA INFORMACIÓN SOBRE UN PROCESO
if(!strcmp(argv[1],"-i"))
{
if(argc<2)
{
printf("\n[!] Debes indicar un PID\n");
}else{
InfoProc(atoi(argv[2]));
}
return;
}
//CERRAR PROCESO SEGÚN EL NOMBRE
if(!strcmp(argv[1],"-pid"))
{
if(argc<2)
{
printf("\n[!] Debes indicar un PID\n");
}else{
datos=BuscaPid(argv[2]);
if(datos!=-1)
{
printf("\n[+] PID de \x22%s\x22 --> %d\n",argv[2],datos);
}else{
printf("\n[!] No existe el proceso \x22%s\x22\n",argv[2]);
}
}
return;
}
printf("\n[!] Argumentos incorrectos\n");
}
/*LISTA LOS PROCESOS ACTUALES*/
int ListarProcesos()
{
char *mid(char *str1, unsigned int inicio, unsigned int fin);
/*cargamos todos los procesos*/
HANDLE proc=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
/*para almacenar los datos de cada proceso*/
PROCESSENTRY32 datos;
/*para almacenar el código del error*/
int errorn;
/*para bucles*/
unsigned int i;
/*miramos si el handle se ha creado*/
if(proc==INVALID_HANDLE_VALUE)
{
printf("\n[!] Error al crear el handle\n");
return 1;
}
Process32First(proc,&datos);
printf("\n%s",datos.szExeFile);
/*creamos un bucle cada vez que tenemos que mostrar un proceso, para saber cuántos espacios tenemos que colocar,
para mostrar los pid´s alineados*/
for(i=0;i<30-strlen(datos.szExeFile);i++)
{
printf("_");
}printf("%d",datos.th32ProcessID);
for(;;)
{
Process32Next(proc,&datos);
errorn=GetLastError();
if(errorn>0)
{
if(errorn!=ERROR_NO_MORE_FILES)
{
printf("\n[!] Error %d - %s",errorn,MensajeError(errorn));
}
return 0;
}
if(strlen(datos.szExeFile)>=26)
{
printf("\n%s",mid(datos.szExeFile,0,26));
}else{
printf("\n%s",datos.szExeFile);
}
/*creamos un bucle cada vez que tenemos que mostrar un proceso, para saber cuántos espacios tenemos que colocar,
para mostrar los pid´s alineados*/
if(strlen(datos.szExeFile)>=26)
{
for(i=0;i<4;i++)
{
printf("_");
}printf("%d",datos.th32ProcessID);
}else{
for(i=0;i<30-strlen(datos.szExeFile);i++)
{
printf("_");
}printf("%d",datos.th32ProcessID);
}
}
CloseHandle(proc);
return 0;
}
//CIERRA UN PROCESO MEDIANTE SU PID
int CierraPID(DWORD pid)
{
//handle para manejar el proceso
HANDLE proceso;
//abrimos el proceso
if((proceso=OpenProcess(PROCESS_TERMINATE,FALSE,pid))==NULL)
{
//lo anunciamos
printf("\n[!] Error al abrir el proceso con PID=%d # %s",pid,MensajeError(GetLastError()));
return 1;
}
//cerramos el proceso
if(TerminateProcess(proceso,0)==TRUE)
{
printf("\n[+] Proceso cerrado!\n");
}else{
printf("\n[+] Error al cerrar el proceso # %s\n",MensajeError(GetLastError()));
}
CloseHandle(proceso);
return 0;
}
/*BUSCA EL PID DE UN PROCESO*/
int BuscaPid(char *nombre)
{
/*cargamos todos los procesos*/
HANDLE proc=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
/*para almacenar los datos de cada proceso*/
PROCESSENTRY32 datos;
/*para almacenar el código del error*/
int errorn;
/*devuelde la descripción del error, a partir de su código*/
char *MensajeError(DWORD error_num);
/*miramos si el handle se ha creado*/
if(proc==INVALID_HANDLE_VALUE)
{
printf("\n[!] Error al crear el handle\n");
return -1;
}
Process32First(proc,&datos);
if(!strcmp(nombre,datos.szExeFile))
{
return datos.th32ProcessID;
}
for(;;)
{
Process32Next(proc,&datos);
errorn=GetLastError();
if(errorn>0)
{
if(errorn!=ERROR_NO_MORE_FILES)
{
printf("\n[!] Error %d - %s",errorn,MensajeError(errorn));
}
return 0;
}
if(!strcmp(nombre,datos.szExeFile))
{
return datos.th32ProcessID;
}
}
CloseHandle(proc);
return 0;
}
/*DA INFORMACION SOBRE UN PROCESO*/
int InfoProc(DWORD pid)
{
HANDLE proc;
/*estructuras que recojen información del proceso*/
MODULEENTRY32 datos;
/*función que nos indica la descripcion de un error*/
char *MensajeError(DWORD error_num);
printf("\n[+] Creando handle...");
//abrimos el proceso
proc=CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,pid);//con posibilidad para acceder a todo
if(proc==INVALID_HANDLE_VALUE)
{
printf("ERROR - %s",MensajeError(GetLastError()));
return 0;
}printf("OK");
if(Module32First(proc,&datos)==FALSE)
{
printf("\n[!] Error al obtener los datos\n");
CloseHandle(proc);
return 0;
}
//obtenemos los datos
printf("\n[+] Obteniendo datos del pid=%d...",pid);
if(Module32First(proc,&datos)==FALSE)
{
printf("\n[!] Error al obtener los datos\n");
CloseHandle(proc);
return 0;
}printf("OK");
//mostramos la info
printf("\n\n[+] Informacion sobre el proceso con pid=%d",pid);
printf("\n-----------------------------------------------");
printf("\n + Nombre el ejecutable: %s",datos.szModule);
printf("\n + Ruta del ejecutable: %s",datos.szExePath);
printf("\n + PID: %d",datos.th32ProcessID);
printf("\n + Tam. Modulo: %d bytes",datos.modBaseSize);
printf("\n-----------------------------------------------");
printf("\n\n[+] Cerrando handle...");
//cerramos el handle
if(CloseHandle(proc)!=0)
{
printf("OK\n");
}else{
printf("\nERROR - %s\n",MensajeError(GetLastError()));
}
return 0;
}
//devuelde la descripción del error, a partir de su código
char *MensajeError(DWORD error_num)
{
char *lpMsgBuf;
//cojemos el mensaje del error
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error_num,
0,
(LPTSTR) &lpMsgBuf,
0,
NULL
);
return lpMsgBuf;
}
//parte una string desde 'inicio' hasta 'fin'
char *mid(char *str1, unsigned int inicio, unsigned int fin)
{
char *resultado;
unsigned int i;
if(inicio>fin)
{
return NULL;
}
resultado=(char *) malloc((fin-inicio)*sizeof(char));
for(i=inicio;i<fin;i++)
{
resultado[i-inicio]=str1[i];
resultado[(i-inicio)+1]='\0';
}
return resultado;
}
=====================================================================================
|| ## ####### ## ## ## ####### ####### ## ## ## ||
|| ## ## ## ## ## ######## ## ## ######## ## ## ||
|| ## ##### ######## ### ## ####### ####### ## #### ||
|| ## ## ## ## ## ## ## ### ## ## ## ||
|| ###### ####### ## ## ####### ####### ### ## ## ## ||
======================================================================================
-------------------------------------------------------------------------------------
|| Name: Process Administration Tool 1.0 ||
|| Description: Programa para listar procesos, matar procesos por id/nombre, ||
|| obtener información sobre un proceso determinado, y obtener el PID ||
|| a través del nombre del Módulo. ||
|| Autor: Lympex ||
|| Date: 11/08/2005 ||
|| HomePage: http://l-bytes.tk ||
|| Contact: lympex[at]gmail[dot]com ||
-------------------------------------------------------------------------------------
D:\>Process.Administration.Tool.1.0.exe
Process Administrator Tool 1.0 - Coded by Lympex
Contact: lympex[at]gmail[dot]com & http://l-bytes.tk
----------------------------------------------------
[!] Argumentos incorrectos
[+] Opciones:
+ Listar procesos: -l
+ Cerrar proceso mediante PID: -kpid pid
+ Cerrar proceso mediante nombre: -kname nombre
+ Informacion sobre un proceso: -i pid
+ Buscar PID: -pid nombre
D:\>
*/
#include <stdio.h>//I/O
#include <windows.h>//Para usar con Tlhelp32.h
#include <Tlhelp32.h>//CreateToolhelp32Snapshot, Process32First, Process32Next,PROCESSENTRY32
#include <string.h>//strlen
/*CIERRA UN PROCESO MEDIANTE SU PID*/
int CierraPID(DWORD pid);
/*devuelde la descripción del error, a partir de su código*/
char *MensajeError(DWORD error_num);
void main(int argc, char *argv[])
{
/*LISTA LOS PROCESOS ACTUALES*/
int ListarProcesos();
/*BUSCA EL PID DE UN PROCESO*/
int BuscaPid(char *nombre);
/*DA INFORMACION SOBRE UN PROCESO*/
int InfoProc(DWORD pid);
//para cojer la salida de algunas funciones
int datos;
printf("\n Process Administrator Tool 1.0 - Coded by Lympex");
printf("\nContact: lympex[at]gmail[dot]com & http://l-bytes.tk");
printf("\n----------------------------------------------------\n");
if(argc<=1)
{
printf("\n[!] Argumentos incorrectos");
printf("\n[+] Opciones:");
printf("\n + Listar procesos: -l");
printf("\n + Cerrar proceso mediante PID: -kpid pid");
printf("\n + Cerrar proceso mediante nombre: -kname nombre");
printf("\n + Informacion sobre un proceso: -i pid");
printf("\n + Buscar PID: -pid nombre\n");
return;
}
//LISTAMOS LOS PROCESOS
if(!strcmp(argv[1],"-l"))
{
printf("\nPROCESO PID");printf("\n---------------------------------\n");ListarProcesos();
printf("\n");
return;
}
//CERRAR PROCESO MEDIANTE PID
if(!strcmp(argv[1],"-kpid"))
{
if(argc<2)
{
printf("\n[!] Debes indicar un PID\n");
}else{
CierraPID(atoi(argv[2]));
}
return;
}
//CERRAR PROCESO SEGÚN EL NOMBRE
if(!strcmp(argv[1],"-kname"))
{
if(argc<2)
{
printf("\n[!] Debes indicar un PID\n");
}else{
datos=BuscaPid(argv[2]);
if(datos!=-1)
{
CierraPID(datos);
}else{
printf("\n[!] No existe el proceso \x22%s\x22\n",argv[2]);
}
}
return;
}
//DA INFORMACIÓN SOBRE UN PROCESO
if(!strcmp(argv[1],"-i"))
{
if(argc<2)
{
printf("\n[!] Debes indicar un PID\n");
}else{
InfoProc(atoi(argv[2]));
}
return;
}
//CERRAR PROCESO SEGÚN EL NOMBRE
if(!strcmp(argv[1],"-pid"))
{
if(argc<2)
{
printf("\n[!] Debes indicar un PID\n");
}else{
datos=BuscaPid(argv[2]);
if(datos!=-1)
{
printf("\n[+] PID de \x22%s\x22 --> %d\n",argv[2],datos);
}else{
printf("\n[!] No existe el proceso \x22%s\x22\n",argv[2]);
}
}
return;
}
printf("\n[!] Argumentos incorrectos\n");
}
/*LISTA LOS PROCESOS ACTUALES*/
int ListarProcesos()
{
char *mid(char *str1, unsigned int inicio, unsigned int fin);
/*cargamos todos los procesos*/
HANDLE proc=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
/*para almacenar los datos de cada proceso*/
PROCESSENTRY32 datos;
/*para almacenar el código del error*/
int errorn;
/*para bucles*/
unsigned int i;
/*miramos si el handle se ha creado*/
if(proc==INVALID_HANDLE_VALUE)
{
printf("\n[!] Error al crear el handle\n");
return 1;
}
Process32First(proc,&datos);
printf("\n%s",datos.szExeFile);
/*creamos un bucle cada vez que tenemos que mostrar un proceso, para saber cuántos espacios tenemos que colocar,
para mostrar los pid´s alineados*/
for(i=0;i<30-strlen(datos.szExeFile);i++)
{
printf("_");
}printf("%d",datos.th32ProcessID);
for(;;)
{
Process32Next(proc,&datos);
errorn=GetLastError();
if(errorn>0)
{
if(errorn!=ERROR_NO_MORE_FILES)
{
printf("\n[!] Error %d - %s",errorn,MensajeError(errorn));
}
return 0;
}
if(strlen(datos.szExeFile)>=26)
{
printf("\n%s",mid(datos.szExeFile,0,26));
}else{
printf("\n%s",datos.szExeFile);
}
/*creamos un bucle cada vez que tenemos que mostrar un proceso, para saber cuántos espacios tenemos que colocar,
para mostrar los pid´s alineados*/
if(strlen(datos.szExeFile)>=26)
{
for(i=0;i<4;i++)
{
printf("_");
}printf("%d",datos.th32ProcessID);
}else{
for(i=0;i<30-strlen(datos.szExeFile);i++)
{
printf("_");
}printf("%d",datos.th32ProcessID);
}
}
CloseHandle(proc);
return 0;
}
//CIERRA UN PROCESO MEDIANTE SU PID
int CierraPID(DWORD pid)
{
//handle para manejar el proceso
HANDLE proceso;
//abrimos el proceso
if((proceso=OpenProcess(PROCESS_TERMINATE,FALSE,pid))==NULL)
{
//lo anunciamos
printf("\n[!] Error al abrir el proceso con PID=%d # %s",pid,MensajeError(GetLastError()));
return 1;
}
//cerramos el proceso
if(TerminateProcess(proceso,0)==TRUE)
{
printf("\n[+] Proceso cerrado!\n");
}else{
printf("\n[+] Error al cerrar el proceso # %s\n",MensajeError(GetLastError()));
}
CloseHandle(proceso);
return 0;
}
/*BUSCA EL PID DE UN PROCESO*/
int BuscaPid(char *nombre)
{
/*cargamos todos los procesos*/
HANDLE proc=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
/*para almacenar los datos de cada proceso*/
PROCESSENTRY32 datos;
/*para almacenar el código del error*/
int errorn;
/*devuelde la descripción del error, a partir de su código*/
char *MensajeError(DWORD error_num);
/*miramos si el handle se ha creado*/
if(proc==INVALID_HANDLE_VALUE)
{
printf("\n[!] Error al crear el handle\n");
return -1;
}
Process32First(proc,&datos);
if(!strcmp(nombre,datos.szExeFile))
{
return datos.th32ProcessID;
}
for(;;)
{
Process32Next(proc,&datos);
errorn=GetLastError();
if(errorn>0)
{
if(errorn!=ERROR_NO_MORE_FILES)
{
printf("\n[!] Error %d - %s",errorn,MensajeError(errorn));
}
return 0;
}
if(!strcmp(nombre,datos.szExeFile))
{
return datos.th32ProcessID;
}
}
CloseHandle(proc);
return 0;
}
/*DA INFORMACION SOBRE UN PROCESO*/
int InfoProc(DWORD pid)
{
HANDLE proc;
/*estructuras que recojen información del proceso*/
MODULEENTRY32 datos;
/*función que nos indica la descripcion de un error*/
char *MensajeError(DWORD error_num);
printf("\n[+] Creando handle...");
//abrimos el proceso
proc=CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,pid);//con posibilidad para acceder a todo
if(proc==INVALID_HANDLE_VALUE)
{
printf("ERROR - %s",MensajeError(GetLastError()));
return 0;
}printf("OK");
if(Module32First(proc,&datos)==FALSE)
{
printf("\n[!] Error al obtener los datos\n");
CloseHandle(proc);
return 0;
}
//obtenemos los datos
printf("\n[+] Obteniendo datos del pid=%d...",pid);
if(Module32First(proc,&datos)==FALSE)
{
printf("\n[!] Error al obtener los datos\n");
CloseHandle(proc);
return 0;
}printf("OK");
//mostramos la info
printf("\n\n[+] Informacion sobre el proceso con pid=%d",pid);
printf("\n-----------------------------------------------");
printf("\n + Nombre el ejecutable: %s",datos.szModule);
printf("\n + Ruta del ejecutable: %s",datos.szExePath);
printf("\n + PID: %d",datos.th32ProcessID);
printf("\n + Tam. Modulo: %d bytes",datos.modBaseSize);
printf("\n-----------------------------------------------");
printf("\n\n[+] Cerrando handle...");
//cerramos el handle
if(CloseHandle(proc)!=0)
{
printf("OK\n");
}else{
printf("\nERROR - %s\n",MensajeError(GetLastError()));
}
return 0;
}
//devuelde la descripción del error, a partir de su código
char *MensajeError(DWORD error_num)
{
char *lpMsgBuf;
//cojemos el mensaje del error
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error_num,
0,
(LPTSTR) &lpMsgBuf,
0,
NULL
);
return lpMsgBuf;
}
//parte una string desde 'inicio' hasta 'fin'
char *mid(char *str1, unsigned int inicio, unsigned int fin)
{
char *resultado;
unsigned int i;
if(inicio>fin)
{
return NULL;
}
resultado=(char *) malloc((fin-inicio)*sizeof(char));
for(i=inicio;i<fin;i++)
{
resultado[i-inicio]=str1[i];
resultado[(i-inicio)+1]='\0';
}
return resultado;
}
Espero que a alguien le ayude











Autor




En línea



