Assign a string to CommandText in IL using Reflexil - cil

I need to edit a query for an application developed some time ago. But I don't have the source code, only the compiled dll's. Following is the source decompiled by Telerik JustDecompile.
conCl.Conn();
conCl.Con.Open();
SqlCommand com = conCl.Com;
string[] strArrays = new string[] { "SELECT * FROM TBL_USER WHERE u_name = '", user.Replace("'", "''"), "' AND u_pass = '", password.Replace("'", "''"), "' and u_IsActive = 1 " };
com.CommandText = string.Concat(strArrays);
SqlDataReader sqlDataReader = conCl.Com.ExecuteReader();
The resulting IL in Reflexil is:
off op operand
set code
18 ldfld System.Data.SqlClient.SqlConnection ANZFrameWorkDAL.ConCls::Con
23 callvirt System.Void System.Data.SqlClient.SqlConnection::Open()
28 nop
29 ldloc.0
30 ldfld System.Data.SqlClient.SqlCommand ANZFrameWorkDAL.ConCls::Com
35 ldc.i4.5
36 newarr System.String
41 stloc.s -> (4) (System.String[])
43 ldloc.s -> (4) (System.String[])
45 ldc.i4.0
46 ldstr SELECT * FROM TBL_USER WHERE u_name = '
51 stelem.ref
52 nop
53 ldloc.s -> (4) (System.String[])
55 ldc.i4.1
56 ldarg.1
57 ldstr '
62 ldstr ''
67 callvirt System.String System.String::Replace(System.String,System.String)
72 stelem.ref
73 nop
74 ldloc.s -> (4) (System.String[])
76 ldc.i4.2
77 ldstr ' AND u_pass = '
82 stelem.ref
83 nop
84 ldloc.s -> (4) (System.String[])
86 ldc.i4.3
87 ldarg.2
88 ldstr '
93 ldstr ''
98 callvirt System.String System.String::Replace(System.String,System.String)
103 stelem.ref
104 nop
105 ldloc.s -> (4) (System.String[])
107 ldc.i4.4
108 ldstr ' and u_IsActive = 1
113 stelem.ref
114 nop
115 ldloc.s -> (4) (System.String[])
117 call System.String System.String::Concat(System.String[])
122 callvirt System.Void System.Data.SqlClient.SqlCommand::set_CommandText(System.String)
127 nop
128 ldloc.0
129 ldfld System.Data.SqlClient.SqlCommand ANZFrameWorkDAL.ConCls::Com
134 callvirt System.Data.SqlClient.SqlDataReader System.Data.SqlClient.SqlCommand::ExecuteReader()
Now what I want is:
com.CommandText = "Select * form tbl_user where u_name = 'admin'"
I tried to load a string after offset 115 as
opcode = ldstr operand = select * form tbl_user where u_name = 'admin'
but the output became
strArrays.CommandText = string.Concat((string[])"select * form tbl_user where u_name = 'admin'");
so i deleted my change and add the same string after offset 117 as:
opcode = ldstr operand = select * form tbl_user where u_name = 'admin'
the output changed and feels somewhat near to what I want but still not correct. the output became:
string.Concat(strArrays).CommandText = "select * form tbl_user where u_name = 'admin'";
What I want is:
com.CommandText = "select * from tbl_user where u_name = 'admin'"
I also tried to add callvirt opcode after offset 122 but i was unable to find set_CommandText method in System.Data.SqlClient.SqlCommand when the loaded .NET framework was 4.6.1
How can I do this? Please, any help would be appreciated. THANKS

Most of the method is just the string concatenation so you can remove it. You can replace all the instructions from offset 35 to 117 inclusive, with a single ldstr that contains your string.

Related

Loop through all cells in a table from Outlook email

I have a standardized email that is sent to me that contains a table (11R x 3C) of which I only require the information from a couple of specific cells.
The table format from the email as follows.
1 |<Empty> |<Empty> |<Empty> |
2 | <Useless info> |
3 | <Impt Info> |
4 |Name: |NameID |<Empty> |
5 |Email: |EmailID |<Empty> |
6 |Contact: |ContactID|<Empty> |
7 |Comment: |CommentID|<Empty> |
8 | <Useless Info> |
9 | <Useless Info> |
10 | <Useless Info> |
11 | <Useless Info> |
Of the table, I am only interested in values of <Impt Info>, NameID, EmailID, ContactID and CommentID.
I've tried looping through the table using debug.print as a Word table object but for some reason it sees the entire table as a single cell. Could I be assigning the table object wrongly or simply using the wrong codes?
Below is the code I've tried to use:
Sub test()
Dim objMail As Outlook.MailItem
Dim objWordDocument As Word.Document
Dim objTable As Word.Table
Dim objExcelApp As Excel.Application
Dim objExcelWorkbook As Excel.Workbook
Dim objExcelWorksheet As Excel.Worksheet
Dim I As Long
Dim SavePath As String
Dim SaveName As String
'Create a new excel workbook
Set objExcelApp = CreateObject("Excel.Application")
Set objExcelWorkbook = objExcelApp.Workbooks.Add
objExcelApp.Visible = True
'Get the table(s) in the selected email
Set objMail = Outlook.Application.ActiveExplorer.Selection.item(1)
Set objWordDocument = objMail.GetInspector.WordEditor
SavePath = "C:\Users\John.Grammaticus\Desktop\Test\"
SaveName = objMail.SenderName & " " & objMail.Subject
Set objTable = objWordDocument.Tables(1)
For Each C In objTable.Range.Cells
Debug.Print C.Range.Text
Next C
objTable.Range.Copy
Set objExcelWorksheet = objExcelWorkbook.Sheets(1)
objExcelWorksheet.Paste
objExcelWorkbook.SaveAs FileName:=SavePath & " " & SaveName
objExcelWorkbook.Close
End Sub
The current code exports the values into an Excel and I could potentially just manipulate from Excel instead. However, I would like to eventually pump the info directly into an Access DB. Hence the need to draw out specific values.
Try using InStr function MSDN
Example
Option Explicit
Public Sub Example()
Dim Item As Outlook.MailItem
Dim vText As Variant
Dim sText As String
Dim vItem As Variant
Dim i As Long
If Application.ActiveExplorer.selection.Count = 0 Then
MsgBox "No Item selected!", vbCritical, "Error"
End If
For Each Item In Application.ActiveExplorer.selection
sText = Item.Body ' Email Body
vText = Split(sText, Chr(13)) ' Chr(13) = Carriage return
'// Check each line of text in the message body down loop
For i = UBound(vText) To 0 Step -1
'// InStr([start,]mainString, SearchedString[, compare])
If InStr(1, vText(i), "Name:") > 0 Then
'// Split vItem : & :
vItem = Split(vText(i), Chr(58)) ' Chr(58) = :
Debug.Print Trim(vItem(1)) 'Print on Immediate Window
End If
Next
Next
End Sub
Or use horizontal tab Chr(9)
See Character Chart
'Dec|Hex|Oct| Char | Description
'-------------------------------
'0 0 000 null
'1 1 001 start of heading
'2 2 002 start of text
'3 3 003 end of text
'4 4 004 end of transmission
'5 5 005 enquiry
'6 6 006 acknowledge
'7 7 007 bell
'8 8 010 backspace
'9 9 011 horizontal tab
'10 A 012 new line
'11 B 013 vertical tab
'12 C 014 new page
'13 D 015 carriage return
'14 E 016 shift out
'15 F 017 shift in
'16 10 020 data link escape
'17 11 021 device control 1
'18 12 022 device control 2
'19 13 023 device control 3
'20 14 024 device control 4
'21 15 025 negative acknowledge
'22 16 026 synchronous idle
'23 17 027 end of trans. block
'24 18 030 cancel
'25 19 031 end of medium
'26 1A 032 substitute
'27 1B 033 escape
'28 1C 034 file separator
'29 1D 035 group separator
'30 1E 036 record separator
'31 1F 037 unit separator
'32 20 040 space
'33 21 041 !
'34 22 042 "
'35 23 043 #
'36 24 044 $
'37 25 045 %
'38 26 046 &
'39 27 047 '
'40 28 050 (
'41 29 051 )
'42 2A 052 *
'43 2B 053 +
'44 2C 054 ,
'45 2D 055 -
'46 2E 056 .
'47 2F 057 /
'48 30 060 0
'49 31 061 1
'50 32 062 2
'51 33 063 3
'52 34 064 4
'53 35 065 5
'54 36 066 6
'55 37 067 7
'56 38 070 8
'57 39 071 9
'58 3A 072 :
'59 3B 073 ;
'60 3C 074 <
'61 3D 075 =
'62 3E 076 >
'63 3F 077 ?
'64 40 100 #
'65 41 101 A
'66 42 102 B
'67 43 103 C
'68 44 104 D
'69 45 105 E
'70 46 106 F
'71 47 107 G
'72 48 110 H
'73 49 111 I
'74 4A 112 J
'75 4B 113 K
'76 4C 114 L
'77 4D 115 M
'78 4E 116 N
'79 4F 117 O
'80 50 120 P
'81 51 121 Q
'82 52 122 R
'83 53 123 S
'84 54 124 T
'85 55 125 U
'86 56 126 V
'87 57 127 W
'88 58 130 X
'89 59 131 Y
'90 5A 132 Z
'91 5B 133 [
'92 5C 134 \
'93 5D 135 ]
'94 5E 136 ^
'95 5F 137 _
'96 60 140 `
'97 61 141 a
'98 62 142 b
'99 63 143 c
'100 64 144 d
'101 65 145 e
'102 66 146 f
'103 67 147 g
'104 68 150 h
'105 69 151 i
'106 6A 152 j
'107 6B 153 k
'108 6C 154 l
'109 6D 155 m
'110 6E 156 n
'111 6F 157 o
'112 70 160 p
'113 71 161 q
'114 72 162 r
'115 73 163 s
'116 74 164 t
'117 75 165 u
'118 76 166 v
'119 77 167 w
'120 78 170 x
'121 79 171 y
'122 7A 172 z
'123 7B 173 {
'124 7C 174 |
'125 7D 175 }
'126 7E 176 ~
'127 7F 177 DEL

Convert big string to decimal in vb.net

Hello i have big string value which is md5 of something now i need to convert it into the decimal value
for example
Dim md5_s As String = "6F05AF42533432A5513610FE839ACC86"
now i need output same like the online converters to this
"54 70 48 53 65 70 52 50 53 51 51 52 51 50 65 53 53 49 51 54 49 48 70
69 56 51 57 65 67 67 56 54 "
is it possible i don't want spaces in the above converted decimal ?
vb.net help please
Okay here i tried and got it n is my approach works fine will this fine n work always right
Dim t As String
Dim a As String = "6F05AF42533432A5513610FE839ACC86"
For Each c As Char In a
t &= Convert.ToInt32(c)
Next
TextBox1.Text = t
will this one is right ?
result is same what i am looking for as
5470485365705250535151525150655353495154494870695651576567675654
so i assume this is right huh ?
I'm not quite sure this is what you are really looking for but this is what you asked for
For count = 0 To md5_s.Length - 1
Dim tempChar As String = md5_s.Substring(count, 1)
Console.Write(Asc(tempChar))
Next
What is more likely what you want is something like this
Private Function HexToByteArray(ByVal hex As [String]) As Byte()
Dim NumberChars As Integer = hex.Length
Dim bytes As Byte() = New Byte(NumberChars / 2 - 1) {}
For i As Integer = 0 To NumberChars - 1 Step 2
bytes(i / 2) = Convert.ToByte(hex.Substring(i, 2), 16)
Next
Return bytes
End Function
either way ... hope this helps

Unsigned Byte flip with *.bin file [duplicate]

This question already has answers here:
16 bit unsigned short byte flip in VB.NET
(2 answers)
Closed 8 years ago.
I need example in code, how to byte flip whole binary file. VB.NET
Example
02 00 0D 78 10 20 40 80 F1 F2 F4 F8 1F 2F 4F 8F
Flip Operation
00 02 78 0D 20 10 80 40 F2 F1 F8 F4 2F 1F 8F 4F
Whole, binary file using OpenFileDialog (OFD)
You just need to loop all items of the array and swap each bytes.
Dim bytes() As Byte = {&H2, &H0, &HD, &H78, &H10, &H20, &H40, &H80, &HF1, &HF2, &HF4, &HF8, &H1F, &H2F, &H4F, &H8F}
For i As Integer = 0 To bytes.Length - 1 Step 2
bytes(i) = bytes(i) Xor bytes(i + 1)
bytes(i + 1) = bytes(i + 1) Xor bytes(i)
bytes(i) = bytes(i) Xor bytes(i + 1)
Next
If you want, use a temp variable
For i As Integer = 0 To bytes.Length - 1 Step 2
Dim tempByte As Byte = bytes(i)
bytes(i) = bytes(i+1)
bytes(i + 1) = tempByte
Next
erm, you could write as you go, something like,
Dim file = dialog.FileName
Using output = New BinaryWriter(New FileStream( _
file, _
FileMode.Append, _
FileAccess.Write, _
FileShare.Read))
Using input = New BinaryReader(New FileStream( _
file, _
FileMode.Open, _
FileAccess.Read, _
FileShare.ReadWrite))
Dim bufferLength = 2
While bufferLength = 2
Dim buffer = input.ReadBytes(2)
bufferLength = buffer.Length
If bufferLength = 2 Then
output.Write(buffer(1))
output.Write(buffer(0))
Else If bufferLength = 1 Then
output.Write(buffer(0))
End If
End While
End Using
End Using

Find all permutations from 1-9 and A-F

I am trying to find all possible permutations from the following conditions:
Number range 1-99
Letter range A-F
32 Digit long string
What would you recommend to make my life easier? Tried to search about permutations in vb, but just can't find them, and I don't know why but it doesn't seem such an hard task as that :s
Samples:
9A E5 4B CA BD 93 DE 2E 01 00 00 01 00 00 00 00
6E C7 9A CF CB A7 67 D9 17 EE 6B 70 F0 5E E4 32
64 86 00 EA 91 71 65 67 1F CE FE EB B1 CC 07 84
63 C0 8A AD F7 9F 5D F3 06 01 00 07 00 00 00 00
51 16 15 7C 56 9F 0A FF 55 1C 20 91 58 CD AA CF
48 61 56 FF 41 6E 49 F8 45 70 49 FE 54 75 52 1B
45 BA B8 B7 42 52 E3 77 03 00 00 03 00 00 00 00
40 D0 F4 04 BF AF 2B 99 02 00 00 02 00 00 00 00
40 30 90 00 3F 7C 83 3E 68 98 D5 D5 6D D9 A3 E9
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
FE A1 CE 6D A6 82 A9 D1 00 00 00 00 00 00 00 00
Thanks for helpin!
EDIT:
Here's my code
Public Class Form1
Dim c As Integer
Dim p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, a As String
Dim combo As String
Dim random As Random
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
c = -1
While c <= 99
c = c + 1
If c < 10 Then
a = "0" & c
Else : a = c
End If
p1 = a
p2 = 2
combo = p1 + " " + p2 + " " + p3 + " " + p4 + " " + p5 + " " + p6 + " " + p7 + " " + p8 + " " + p9 + " " + p10 + " " + p11 + " " + p12 + " " + p13 + " " + p14 + " " + p15 + " " + p16 + " " + vbNewLine
RichTextBox1.AppendText(combo + vbNewLine)
End While
End Sub
End Class
Judging from your example, you mean 0-9 (not 1-9) and A-F
Alex K. neatly provides a direction to solve this:
You would ignore the "numbers and letters" since that is a hexadecimal
notation of bytes (AF == 175) There are 16 bytes, each of which can
hold 0 to 255 so you have
340,282,366,920,938,463,463,374,607,431,768,211,455 possible
combinations.
Although it is theoretically possible to get all of these permutations, your computer needs about 34,028,236,692,093,846,346,337,460,743 GB. I don't think that much memory exists in total
If it were a relatively tiny number, we could do this simply with the following method:
Sub purgatory()
Dim counter As Long
Dim output As String
counter = 0
For counter = 0 To 2147483647#
output = String(16 - Len(Hex(counter)), "0") & Hex(counter)
MsgBox output
Next counter
End Sub
But the max value of a long in VBA7 is 2,147,483,648 (4 Bytes / 2), or 8 bytes with VB net. Which means that 12 bytes and one bit will always be 0. This could be solved with a few nested for loops.
I think the solution to your problem is best answered by JNevill
16 combinations of 00-FF... That is insanity...
IMPORTANT NOTE: If you run the code I posted, it's important to first familiarize yourself with Ctr+Pause/Break. Quickest way out of purgatory ;)

Wrong sum calculation of objects in array

I received code with an object (we'll call it Obj) with a property named Number. An array of these objects is defined with 40 objects in indexes 0..39. I don't need object at index 0.
The code has a static variable saving the sum of all Number's of Obj's in the array. This variable is initialized only once. Right after the Number values themselves are defined.
It usually works, but sometimes I log an exception (which I haven't been able to reproduce) with values of different objects and I see there the value of NumberAll is wrong (I've recently logged 1722 and 2134. Not that it should matter - but the only common prime factor for them is 2. should be 1554, if you were wondering, you can check me below :) ).
EDIT: The exception is an Index was outside the bounds of the array on the loop brought in the code below - a direct effect of this issue.
I've tried two methods of summing, brought below.
Relevant code follows. There is nowhere else in the code that a value is assigned to NumberAll.
Static NotFirstTime As Boolean, NumberAll As Integer
If NotFirstTime = False Then GoTo Data
sPoint: 'Code and more code
' ...
' ...
breakValue = someValue Mod NumberAll
Sum = 0
I = 1
Try
Do
If Sum + myArray(I).Number >= breakValue Then
Exit Do
End If
Sum = Sum + myArray(I).Number
I = I + 1
Loop
Catch ex As Exception
'log error and variable data - breakValue is larger than Sum will ever get
'hence the loop reaches the point of trying myArray(40) which doesn't exist
End Try
' ...
Data:
NotFirstTime = True
myArray(1).Number = 37
myArray(2).Number = 34
myArray(3).Number = 44
myArray(4).Number = 31
myArray(5).Number = 59
myArray(6).Number = 26
myArray(7).Number = 33
myArray(8).Number = 28
myArray(9).Number = 20
myArray(10).Number = 13
myArray(11).Number = 92
myArray(12).Number = 65
myArray(13).Number = 71
myArray(14).Number = 22
myArray(15).Number = 22
myArray(16).Number = 42
myArray(17).Number = 26
myArray(18).Number = 26
myArray(19).Number = 33
myArray(20).Number = 34
myArray(21).Number = 22
myArray(22).Number = 19
myArray(23).Number = 85
myArray(24).Number = 72
myArray(25).Number = 47
myArray(26).Number = 40
myArray(27).Number = 47
myArray(28).Number = 54
myArray(29).Number = 48
myArray(30).Number = 44
myArray(31).Number = 37
myArray(32).Number = 34
myArray(33).Number = 44
myArray(34).Number = 9
myArray(35).Number = 57
myArray(36).Number = 37
myArray(37).Number = 19
myArray(38).Number = 13
myArray(39).Number = 68
NumberAll = 0
'Only one of the following methods is used. They seem to give the same result. The bug exists in both.
'Method one
For I = 1 To 39 'E מספר המסכתות
NumberAll = NumberAll + myArray(I).Number
Next I
'Method two
Dim myList As List(Of Obj) = New List(Of Obj)(myArray)
NumberAll = myList.Sum(Function(b) b.Number)
NumberAll -= myArray(0).Number
GoTo sPoint