Email mit SMTP-Auth

Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
Benutzeravatar
rolaf
Beiträge: 3843
Registriert: 10.03.2005 14:01

Re: Email mit SMTP-Auth

Beitrag von rolaf »

Und in die Zeile von HeX0R (den X0Richsten aller Hexer)

Code: Alles auswählen

   Answers(7)\Question + "To: " + MailName + " <" + MailTo + ">"     + #CRLF$
kann gleich noch der Absender Name komplettiert werden.

MailName muß dann natürlich auch an die Procedure übergeben werden.
Zuletzt geändert von rolaf am 07.08.2010 20:42, insgesamt 1-mal geändert.
:::: WIN 10 :: PB 5.73 :: (x64) ::::
Benutzeravatar
HeX0R
Beiträge: 2954
Registriert: 10.09.2004 09:59
Computerausstattung: AMD Ryzen 7 5800X
96Gig Ram
NVIDIA GEFORCE RTX 3060TI/8Gig
Win10 64Bit
G19 Tastatur
2x 24" + 1x27" Monitore
Glorious O Wireless Maus
PB 3.x-PB 6.x
Oculus Quest 2
Kontaktdaten:

Re: Email mit SMTP-Auth

Beitrag von HeX0R »

DrFalo hat geschrieben:Und in die Zeile von Hexor

Code: Alles auswählen

   Answers(7)\Question + "To: " + MailName + " <" + MailTo + ">"     + #CRLF$
kann gleich noch der Absender Name komplettiert werden.

MailName muß dann natürlich auch an die Procedure übergeben werden.
Sodele, nun ersetzt du nur noch das O in meinem Nick mit einer Null und alle sind zufrieden. :twisted:
Benutzeravatar
rolaf
Beiträge: 3843
Registriert: 10.03.2005 14:01

Re: Email mit SMTP-Auth

Beitrag von rolaf »

HeX0R hat geschrieben:Sodele, nun ersetzt du nur noch das O in meinem Nick mit einer Null und alle sind zufrieden. :twisted:
Jawohl Eure H0heit. Eins von den 30 Bieren gestern schlecht gewesen? :lol:
Und wieso wird Nino nicht von dir angemeckert, Hexor steht doch da och. :mrgreen:
Zuletzt geändert von rolaf am 07.08.2010 20:45, insgesamt 1-mal geändert.
:::: WIN 10 :: PB 5.73 :: (x64) ::::
Benutzeravatar
HeX0R
Beiträge: 2954
Registriert: 10.09.2004 09:59
Computerausstattung: AMD Ryzen 7 5800X
96Gig Ram
NVIDIA GEFORCE RTX 3060TI/8Gig
Win10 64Bit
G19 Tastatur
2x 24" + 1x27" Monitore
Glorious O Wireless Maus
PB 3.x-PB 6.x
Oculus Quest 2
Kontaktdaten:

Re: Email mit SMTP-Auth

Beitrag von HeX0R »

DrFalo hat geschrieben: Jawohl Eure H0heit. Eins von den 30 Bieren gestern schlecht gewesen? :lol:
HeHeHe,
eher die neunstündige Rückreise von Olkiluoto und das furchtbare finnische Bier.
DrFalo hat geschrieben: Und wieso wird Nino nicht von dir angemeckert, Hexor steht doch da och. :mrgreen:
Ich war vom Supercupgewinn der Bayern noch etwas benebelt, deswegen hab ich wohl nur noch das letzte Posting genauer betrachtet ;)
Benutzeravatar
Kiffi
Beiträge: 10621
Registriert: 08.09.2004 08:21
Wohnort: Amphibios 9

Re: Email mit SMTP-Auth

Beitrag von Kiffi »

DrFalo hat geschrieben:Und wieso wird Nino nicht von dir angemeckert, Hexor steht doch da och. :mrgreen:
alte Petze!
Hygge
Benutzeravatar
rolaf
Beiträge: 3843
Registriert: 10.03.2005 14:01

Re: Email mit SMTP-Auth

Beitrag von rolaf »

Kiffi hat geschrieben:alte Petze!
Ja gebts mir, lasst euren Frust an mich ab. :mrgreen:
Wer will nochmal, wer hat noch nicht. Heut im Dutzend billiger. :lol:
:::: WIN 10 :: PB 5.73 :: (x64) ::::
fnor
Beiträge: 2
Registriert: 03.05.2012 21:03

Re: Email mit SMTP-Auth

Beitrag von fnor »

Hi,

auch wenn der Code sicher dieselben Fehler hat, wie bereits weiter vorn kritisiert, wollte ich ihn doch mal posten. Er ist schon einen Schluck älter, realisiert aber die Anmeldung auf SMTP-Servern auf 3 verschiedene Weisen. 2 mal mit unverschlüsseltem Passwort und CRAM-MD5 mit Passworthash.

Code: Alles auswählen

EnableExplicit

#auth_plain = "PLAIN"
#auth_login = "LOGIN"
#auth_md5 = "CRAM-MD5"
#auth_sha1 = "SCRAM-SHA-1" ;not supported

#server = "smtp.server.de"
#port = 25
#auth = #auth_md5
#user = "user"
#pass = "pass"
#from = "adress"
#to = "adress"
#subject = "subject"
#text = "text"

#max_len = 65536

Procedure my_poke_s(*pointer, string.s)
 
 Protected *temp = AllocateMemory(Len(string) + 1)
 
 PokeS(*temp, string, Len(string), #PB_Ascii)
 CopyMemory(*temp, *pointer, Len(string))
 FreeMemory(*temp)

EndProcedure

Procedure send_string(connection.l, string.s)
 
 Protected *buffer = AllocateMemory(Len(string) + 4)
 
 PokeS(*buffer, string, Len(string), #PB_Ascii)
 
 PokeC(*buffer + Len(string), 13) ; add the additional sring end
 PokeC(*buffer + Len(string) + 1, 10)
 
 ;WriteStringN(1, "-> " + string)
 
 SendNetworkData(connection, *buffer, Len(string) + 2)
 
EndProcedure

Procedure.s receive_string(connection.l)
 
 Protected *buffer = AllocateMemory(#max_len)
 Protected length.l
 Protected string.s
 
 length = ReceiveNetworkData(connection, *buffer, #max_len)
 
 string = PeekS(*buffer, length, #PB_Ascii)
 ;WriteStringN(1, "<- " + string)
 ProcedureReturn string
 
EndProcedure

Procedure.s encode_base_64(string.s)
 
 Protected length.l
 Protected out_length.l = 64 + 2 * Len(string)
 Protected *in_buffer = AllocateMemory(Len(string) + 1)
 Protected *out_buffer = AllocateMemory(out_length)
 
 PokeS(*in_buffer, string, Len(string), #PB_Ascii)
 length = Base64Encoder(*in_buffer, Len(string), *out_buffer, out_length)
 ProcedureReturn PeekS(*out_buffer, length, #PB_Ascii)
 
EndProcedure

Procedure.s decode_base_64(string.s)
 
 Protected length.l
 Protected out_length.l = 64 + 2 * Len(string)
 Protected *in_buffer = AllocateMemory(Len(string) + 1)
 Protected *out_buffer = AllocateMemory(out_length)
 
 PokeS(*in_buffer, string, Len(string), #PB_Ascii)
 length = Base64Decoder(*in_buffer, Len(string), *out_buffer, out_length)
 ProcedureReturn PeekS(*out_buffer, length, #PB_Ascii)
 
EndProcedure

Procedure.a connect_plain(connection.l, user.s, pass.s)
 
 Protected out.a = 0
 Protected string.s
 Protected *buffer
 Protected *code
 Protected length.l
 
 send_string(connection, "AUTH " + #auth_plain)
 string = receive_string(connection)
 If (Left(string, 3) = "334")
  ; string contains null -> encode_base_64 not useable
  *buffer = AllocateMemory(2 + Len(user) + Len(pass))
  my_poke_s(*buffer + 1, user)
  my_poke_s(*buffer + 2 + Len(user), pass)
  *code = AllocateMemory(64 + 2 * (2 + Len(user) + Len(pass)))
  length = Base64Encoder(*buffer, 2 + Len(user) + Len(pass), *code, 64 + 2 * (2 + Len(user) + Len(pass)))
  send_string(connection, PeekS(*code, length, #PB_Ascii))
  string = receive_string(connection)
  If (Left(string, 3) = "235")
   out = 1
  EndIf
 EndIf
 
 ProcedureReturn out
 
EndProcedure

Procedure.a connect_login(connection.l, user.s, pass.s)
 
 Protected out.a = 0
 Protected string.s
 
 send_string(connection, "AUTH " + #auth_login)
 string = receive_string(connection)
 If (Left(string, 3) = "334")
  send_string(connection, encode_base_64(user))
  string = receive_string(connection)
  If (Left(string, 3) = "334")
   send_string(connection, encode_base_64(pass))
   string = receive_string(connection)
   If (Left(string, 3) = "235")
    out = 1
   EndIf
  EndIf
 EndIf
 
 ProcedureReturn out
 
EndProcedure

#ipad = $36
#opad = $5c
#block_size = 64

Procedure xor_block(*block, value.a)
 
 Protected i.l
 
 For i = 0 To (#block_size - 1)
  PokeA(*block + i, PeekA(*block + i) ! value)
 Next

EndProcedure

Procedure hex_string_2_bin(string.s, *bin)
 
 Protected size.l
 Protected i.l
 
 size = Round(Len(string) / 2, #PB_Round_Down)
 For i = 0 To (size - 1)
  PokeA(*bin, Val("$" + Mid(string, i * 2 + 1, 2)))
  *bin = *bin + 1
 Next
 
EndProcedure

Procedure.s hmac_md5(key.s, text.s)
 
 Protected *i_key
 Protected *o_key
 Protected *front_part
 Protected *back_part
 Protected *text = AllocateMemory(#block_size)
 Protected fingerprint.s
 Protected *fingerprint
 
 *i_key = AllocateMemory(#block_size)
 my_poke_s(*i_key, key)
 xor_block(*i_key, #ipad)
 *back_part = AllocateMemory(#block_size + Len(text))
 CopyMemory(*i_key, *back_part, #block_size)
 my_poke_s(*back_part + #block_size, text)
 fingerprint = MD5Fingerprint(*back_part, #block_size + Len(text))
 *fingerprint = AllocateMemory(16)
 hex_string_2_bin(fingerprint, *fingerprint)
 
 *o_key = AllocateMemory(#block_size)
 my_poke_s(*o_key, key)
 xor_block(*o_key, #opad)
 *front_part = AllocateMemory(#block_size + Len(fingerprint)) 
 CopyMemory(*o_key, *front_part, #block_size)
 CopyMemory(*fingerprint, *front_part + #block_size, 16)
 fingerprint = MD5Fingerprint(*front_part, #block_size + 16)
 ProcedureReturn fingerprint
 
EndProcedure

Procedure.s hmac_sha1(key.s, text.s)
 
 Protected *i_key
 Protected *o_key
 Protected *front_part
 Protected *back_part
 Protected *text = AllocateMemory(#block_size)
 Protected fingerprint.s
 Protected *fingerprint
 
 *i_key = AllocateMemory(#block_size)
 my_poke_s(*i_key, key)
 xor_block(*i_key, #ipad)
 *back_part = AllocateMemory(#block_size + Len(text))
 CopyMemory(*i_key, *back_part, #block_size)
 my_poke_s(*back_part + #block_size, text)
 fingerprint = SHA1Fingerprint(*back_part, #block_size + Len(text))
 *fingerprint = AllocateMemory(20)
 hex_string_2_bin(fingerprint, *fingerprint)
 
 *o_key = AllocateMemory(#block_size)
 my_poke_s(*o_key, key)
 xor_block(*o_key, #opad)
 *front_part = AllocateMemory(#block_size + Len(fingerprint)) 
 CopyMemory(*o_key, *front_part, #block_size)
 CopyMemory(*fingerprint, *front_part + #block_size, 20)
 fingerprint = SHA1Fingerprint(*front_part, #block_size + 20)
 ProcedureReturn fingerprint
 
EndProcedure

Procedure.a connect_md5(connection.l, user.s, pass.s)
 
 Protected out.a = 0
 Protected string.s
 Protected challenge.s
 Protected *buffer
 
 send_string(connection, "AUTH " + #auth_md5)
 string = receive_string(connection)
 If (Left(string, 3) = "334")
  challenge = Mid(string, 5, Len(string) - 6)
  send_string(connection, encode_base_64(user + " " + hmac_md5(pass, decode_base_64(challenge))))
  string = receive_string(connection)
  If (Left(string, 3) = "235")
   out = 1
  EndIf
 EndIf
 
 ProcedureReturn out
 
EndProcedure

Procedure.a connect(connection.l, auth.s, user.s, pass.s)
 
 Protected out.a = 0
 Protected string.s
 Protected position.l
 
 string = receive_string(connection)
 If (Left(string, 3) = "220")
  If (FindString(string, "ESMTP", 1))
   send_string(connection, "EHLO program.purebasic.com")
   string = receive_string(connection)
   If (Left(string, 3) = "250")
    position = FindString(string, "AUTH", 1)
    If position
     If FindString(string, auth, position + 4)
      Select auth
       Case #auth_plain
        If (connect_plain(connection, user, pass))
         out = 1
        EndIf
       Case #auth_login
        If (connect_login(connection, user, pass))
         out = 1
        EndIf
       Case #auth_md5
        If (connect_md5(connection, user, pass))
         out = 1
        EndIf
      EndSelect
     EndIf
    EndIf
   EndIf
  EndIf
 EndIf
 
 ProcedureReturn out
 
EndProcedure

Procedure.a send_content(connection.l, from_adress.s, to_adress.s, subject.s, text.s)
 
 Protected out.a = 0
 Protected string.s
 
 send_string(connection, "MAIL FROM:<" + from_adress + ">")
 string = receive_string(connection)
 If (Left(string, 3) = "250")
  send_string(connection, "RCPT TO:<" + to_adress + ">")
  string = receive_string(connection)
  If (Left(string, 3) = "250")
   send_string(connection, "DATA")
   string = receive_string(connection)
   If (Left(string, 3) = "354")
    send_string(connection, "From:<" + from_adress + ">")
    send_string(connection, "To:<" + to_adress + ">")
    send_string(connection, "Subject: " + subject)
    send_string(connection, text)
    send_string(connection, ".")
    string = receive_string(connection)
    If (Left(string, 3) = "250")
     out = 1
    EndIf
   EndIf
  EndIf
 EndIf
 
 ProcedureReturn out
 
EndProcedure

Procedure.a send_mail(server.s, port.c, auth.s, user.s, pass.s, from_adress.s, to_adress.s, subject.s, text.s)
 
 Protected out.a = 0
 Protected connection.l
 Protected string.s

 ;CreateFile(1, "out.txt")
 If InitNetwork()
  connection = OpenNetworkConnection(server, port, #PB_Network_TCP)
  If connection
   If connect(connection, auth, user, pass)
    If send_content(connection, from_adress, to_adress, subject, text)
     out = 1
    EndIf
   EndIf
   send_string(connection, "QUIT")
   string = receive_string(connection)
   CloseNetworkConnection(connection)
  EndIf
 EndIf
 ;CloseFile(1)
 
 ProcedureReturn out

EndProcedure

send_mail(#server, #port, #auth, #user, #pass, #from, #to, #subject, #text)
Benutzeravatar
kunstlust
Beiträge: 259
Registriert: 12.04.2012 23:47
Wohnort: Hannover
Kontaktdaten:

Re: Email mit SMTP-Auth

Beitrag von kunstlust »

@fnor

Ich habe deine letzten Code mal ausprobiert und bekommen von "Norman Endpoint Protection" sofort die Meldung, das er ein Throjaner /win32/SB/EmailWorm gefunden hat.
Ich wollte nur mal drauf hinweisen, das dies sicher keine Einzelfall ist und wohl mehr am Prinzip als am Code liegt, was ich schade finden, mir gefällt es sonst sehr gut. :mrgreen:
Benutzeravatar
jpd
Beiträge: 380
Registriert: 14.02.2005 10:33

Re: Email mit SMTP-Auth

Beitrag von jpd »

Hi kunstlust,

Habe die Datei zur Analyse von Norman weitergeleitet...
SB hat zugeschlagen :-)
gehe davon aus das mit dem nächsten Update behoben ist.

verwendest du schon NPRO 9.1?

Ciao
jpd
PB 5.10 Windows 7 x64
Benutzeravatar
kunstlust
Beiträge: 259
Registriert: 12.04.2012 23:47
Wohnort: Hannover
Kontaktdaten:

Re: Email mit SMTP-Auth

Beitrag von kunstlust »

jpd hat geschrieben:Hi kunstlust,

Habe die Datei zur Analyse von Norman weitergeleitet...
SB hat zugeschlagen :-)
gehe davon aus das mit dem nächsten Update behoben ist.

verwendest du schon NPRO 9.1?

Ciao
jpd
Danke, ja die 9.1
Antworten