-
Notifications
You must be signed in to change notification settings - Fork 46
/
tsharkdecoder.cpp
185 lines (162 loc) · 5.02 KB
/
tsharkdecoder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* This file is part of 3GPP Decoder project.
* Copyright (C) 2015 Prashant Panigrahi [email protected]
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tsharkdecoder.h"
TSharkDecoder::TSharkDecoder()
{
}
TSharkDecoder::~TSharkDecoder()
{
}
/* Decoding starts here
*/
void TSharkDecoder::startDecoder(QString strEncodedData, QString strProtocol)
{
QString strData;
QString strTsharkPath;
strData = preformatData(strEncodedData);
strTsharkPath = getTsharkPath();
format_file_for_text2pcap(strData);
call_text2pacp(strTsharkPath);
call_tshark(strTsharkPath, strProtocol);
clean_output();
}
/* Make the incoming packets in HEX format.
* The packets are encoded in this format:
* ab - ab
* a - 0a
* abcd - ab cd
*/
QString TSharkDecoder::preformatData(QString strEncodedData){
QString strReturnData;
QStringList strListData;
strListData = strEncodedData.split(" ");
for(int i = 0; i < strListData.length(); i++ )
{
if(strListData[i].length() == 2)
{
strReturnData.append(" ");
strReturnData.append(strListData[i]);
}
else if(strListData[i].length() == 1)
{
strReturnData.append(" 0");
strReturnData.append(strListData[i]);
}
else if(strListData[i].length() > 2)
{
for(int j = 0; j < strListData[i].length(); j = j+2)
{
QStringRef strTempData = strListData[i].midRef(j, 2);
strReturnData.append(" ");
strReturnData.append(strTempData);
}
}
}
return strReturnData;
}
/* Get the tshark path on Windows.
* No support for Windows XP at this moment
*/
QString TSharkDecoder::getTsharkPath()
{
QString strWiresharkLoc;
strWiresharkLoc = "C:\\Progra~1\\Wireshark\\";
QFile wiresharkpath("config.txt");
if(!wiresharkpath.exists())
{
return strWiresharkLoc;
}
else
{
if (wiresharkpath.open(QIODevice::ReadOnly | QIODevice::Text)){
QTextStream stream(&wiresharkpath);
QString strLine;
while (!stream.atEnd()){
strLine = stream.readLine();
if(strLine.contains("wireshark:"))
{
strWiresharkLoc = strLine.remove(0,10);
if(strWiresharkLoc.contains("86"))
{
strWiresharkLoc = "C:\\Progra~2\\Wireshark\\";
}
}
}
}
}
return strWiresharkLoc;
}
/*Create a Textfile which text2pcap can understand.
* 0000 are added to the beginning of the HEX data
* to make it text2pacp type
*/
void TSharkDecoder::format_file_for_text2pcap(QString strData)
{
QFile textFile("textdata.txt");
if (textFile.open(QIODevice::ReadWrite)) {
QTextStream stream(&textFile);
stream << "0000";
stream <<strData;
qDebug() << strData;
}
}
/* Make a PCAP file from the textdata.
* Function calls text2pack.exe from wireshark folder.
*/
void TSharkDecoder::call_text2pacp(QString strTsharkPath)
{
QString command= strTsharkPath + "text2pcap.exe -q -l 147 textdata.txt decode_temp.pcap";
system(qPrintable(command));
qDebug() << command;
}
/* Call Tshark to decode the PCAP file.
* RRC messages are decoded directly
* NAS messages embeded are also parsed by Tshark.
*/
void TSharkDecoder::call_tshark(QString strTsharkPath, QString strProtocol)
{
QString command;
command = command.append(strTsharkPath);
command = command.append("tshark.exe -o \"uat:user_dlts:\\\"User 0 (DLT=147)\\\",\\\"");
command = command.append(strProtocol);
command = command.append("\\\",\\\"0\\\",\\\"\\\",\\\"0\\\",\\\"\\\"\" -r decode_temp.pcap\ -V > decode_output_temp.txt");
qDebug() << command;
system(qPrintable(command));
system("del textdata.txt decode_temp.pcap");
}
/* After decode the first 15 lines are useless data for us.
* These lines can be removed for a clean presentation
*/
void TSharkDecoder::clean_output()
{
QFile f("decode_output_temp.txt");
if(f.open(QIODevice::ReadWrite | QIODevice::Text))
{
QString s;
QTextStream t(&f);
int i = 0;
while(!t.atEnd())
{
QString line = t.readLine();
if(i>=15)
s.append(line + "\n");
i++;
}
f.resize(0);
t << s;
f.close();
}
}